skip to Main Content

I am using PHP Mailer to automate password reset functionality (with a token) and the email is not being sent when passing the data through. It will only send if I hard code an email in the address variable. Here’s a sample of the code below:

$link = "<a href='https://www.mywebsite.com/admin/reset-password.php?key=".$emailId."&token=".$token."'>Click To Reset password</a>";

    require_once('PHPMailerAutoload.php');
     
    $mail = new PHPMailer();

    try {
    //Server settings

    $mail->SMTPDebug = SMTP::DEBUG_SERVER;
    $mail->SMTPDebug = 4; 
    $mail->isSMTP();
    $mail->Host = 'relay-hosting.secureserver.net';
    $mail->SMTPAuth = false;
    $mail->SMTPAutoTLS = false; 
    $mail->Port = 25; 


    //Recipients
    $mail->setFrom('[email protected]', 'MyWebsite Admin');
    $mail->addAddress($emailId);     //Add a recipient
    $mail->addReplyTo($emailId);


    //Content
    $mail->isHTML(true);                                  //Set email format to HTML
    $mail->Subject = 'MyWebsite password reset';
    $mail->Body    = '<h2>Hello!</h2><p>Forgotten your password?</p>
    <p>You have been sent this email because we received a request to reset the password to your MyWebsite account.</p>
    <p>To do this, please click the button below to enter a new password of your choice.</p>    
    <p>Regards,</p>
    <p>MyWebsite Support</p><p>'.$link.'</p>';
    $mail->AltBody = 'You have been sent this email because we received a request to reset the password to your MyWebsite account.';

    $mail->send();
    //echo 'An email was sent to ' . $emailId;
    include "password-reset-message.php";
    } catch (Exception $e) {
        echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
    }
 

  }else{
    echo "Invalid Email Address. Go back";
  }

If I change this field from:

$mail->addAddress($emailId);     //Add a recipient

To this:

$mail->addAddress('[email protected]');     //Add a recipient

Then that works…

2

Answers


  1. The problem is probably due to $emailId not recieving its value. Try "echo $emailId" right before this part of your code and see if it has the email. Also try hardcoding the $emailId like this: $emailId = ‘[email protected]’;

    Login or Signup to reply.
  2. The reason why the email is not being sent when passing the data through is because the email address in the $emailId variable is not valid. The mail server is rejecting the email because it does not recognize the email address.

    To fix this issue, you need to make sure that the email address in the $emailId variable is valid. You can do this by using a function like filter_var() to validate the email address.

    $link = "<a href='https://www.mywebsite.com/admin/reset-password.php?key=".$emailId."&token=".$token."'>Click To Reset password</a>";
    
        require_once('PHPMailerAutoload.php');
         
        $mail = new PHPMailer();
    
        try {
        //Server settings
    
        $mail->SMTPDebug = SMTP::DEBUG_SERVER;
        $mail->SMTPDebug = 4; 
        $mail->isSMTP();
        $mail->Host = 'relay-hosting.secureserver.net';
        $mail->SMTPAuth = false;
        $mail->SMTPAutoTLS = false; 
        $mail->Port = 25; 
    
    
        //Recipients
        $emailId = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
        if($emailId){
            $mail->setFrom('[email protected]', 'MyWebsite Admin');
            $mail->addAddress($emailId);     //Add a recipient
            $mail->addReplyTo($emailId);
    
    
        //Content
        $mail->isHTML(true);                                  //Set email format to HTML
        $mail->Subject = 'MyWebsite password reset';
        $mail->Body    = '<h2>Hello!</h2><p>Forgotten your password?</p>
        <p>You have been sent this email because we received a request to reset the password to your MyWebsite account.</p>
        <p>To do this, please click the button below to enter a new password of your choice.</p>    
        <p>Regards,</p>
        <p>MyWebsite Support</p><p>'.$link.'</p>';
        $mail->AltBody = 'You have been sent this email because we received a request to reset the password to your MyWebsite account.';
    
        $mail->send();
        //echo 'An email was sent to ' . $emailId;
        include "password-reset-message.php";
        }else{
            echo "Invalid Email Address. Go back";
        }
     
    
      } catch (Exception $e) {
            echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search