Guest User

Guest User

  • Tech Writer
  • 2.1k
  • 467.5k

Message could not be sent. Mailer Error: Recipient email address

Apr 28 2023 9:58 AM

Hi Team

I have a logic to send an email but i did not hard coded to send an email for users to get an otp. Rather it uses $_POST['email'], but now the logic is not quite close working and need some help to resolve this. Let me share my logic for do this, maybe i am missing some few steps. 

 

// jquery code

$(document).ready(function(){
    $("#forgot-password").submit(function(event){
        event.preventDefault();
        var formData = $(this).serialize();
        $.ajax({
            url: "forgot-password.php",
            type: "POST",
            data: formData,
            success: function(data){
                if(data == "success"){
                    alert("Password reset successfully.");
                  
                }
                
            }
            
        });
    });
});

// php code

<?php
session_start();

// Get the directory of the current script
$currentDir = __DIR__;

$phpMailerDir = 'vendor\phpmailer\phpmailer';

require_once $phpMailerDir . '\src\PHPMailer.php';
require_once $phpMailerDir . '\src\SMTP.php';
require_once $phpMailerDir . '\src\Exception.php';

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

// Create a new PHPMailer instance
/* Create a new PHPMailer object. Passing TRUE to the constructor enables exceptions. */
$mail = new PHPMailer(TRUE);

try {
    // Server settings
    $mail->SMTPDebug = SMTP::DEBUG_OFF;
    $mail->isSMTP();
    $mail->Host = 'host-name'; // Enter your SMTP server here
    $mail->SMTPAuth = true;
    $mail->Username = "gcira";
    $mail->Password = ""; // SMTP password
    $mail->SMTPSecure = 'tls';
    $mail->Port = 587; // Port number depends on your SMTP server configuration

    // Sender and recipient details
    if(isset($_POST['email']) && !empty($_POST['email'])){
        $recipient_email = $_POST['email'];
        $mail->addAddress($recipient_email, 'Recipient Name');
    } else {
        throw new Exception('Recipient email address is not set or empty.');
    }

    // Email content
    $mail->isHTML(false);
    $mail->Subject = 'Password Reset';
    $otp = rand(100000, 999999);
    $_SESSION['otp'] = $otp; // Store the OTP in a session variable
    $mail->Body = 'Your OTP code is: ' . $otp;

    // Send the email
    if (!$mail->send()) {
        echo "Mailer Error: " . $mail->ErrorInfo;
    } else {
        // Redirect the user to the OTP verification page
        //header('Location: otp-confirmation.php');
        //exit;
    }

} catch (Exception $e) {
    // Output an error message if the email failed to send
    echo 'Message could not be sent. Mailer Error: ', $e->getMessage();
}
?>







    <!-- owl carousel-->
    
  </head>
  <body>
<div class="container">
  <div class="row justify-content-center">
    <div class="col-md-6">
      <div class="card mt-5">
        <div class="card-header">
          Reset Password
        </div>
        <div class="card-body">
          <form id="forgot-password" method="post" action="otp-confirmation.php">
            <div class="form-group">
              <label for="email">Email</label>
              <input type="email" class="form-control" id="email" name="email" required>
            </div>
            <div class="form-group">
              <label for="new-password">New Password</label>
              <input type="password" class="form-control" id="new-password" name="new-password" required>
            </div>
            <div class="form-group">
              <label for="confirm-password">Confirm Password</label>
              <input type="password" class="form-control" id="confirm-password" name="confirm-password" required>
            </div>
            <button type="submit" class="btn btn-primary">Reset Password</button>
          </form>
        </div>
      </div>
    </div>
  </div>
</div>
   
</body>
</html>

 


Answers (3)