moon_coder
moon_coder

Reputation: 94

unable to send token and email value in mail using phpmailer for forgot password

I want send the the value of $email and $token along with the mail to the sender but it is not sending it. This is the link which I'm getting in my email is:

http://localhost/admin-dashboard/resetPassword.php?email=$femail&token=$token

This does not showing the value of $token and $femail. Can anyone help??

$token = "qwertyuiopasdfghjklzxcvbnm1234567890jksdhfljdhfajlsdbhkfdajsfhaljsdfhb";
                    $token = str_shuffle($token);
                    $token = substr($token, 0,10);
//                  $femail = '';

                    $stmt_i = $conn->prepare("UPDATE users SET token=?, tokenExpire=DATE_ADD(NOW(), INTERVAL 5 MINUTE) WHERE email=?");
                    $stmt_i->bind_param("ss", $token, $femail);
                    $stmt_i->execute();
                    $result = $stmt_i->affected_rows;
//                    echo $result;



                    //Load Composer's autoloader
                    // require '.../vendor/autoload.php';

                    require '../PHPMailer/src/PHPMailer.php';
                    require '../PHPMailer/src/Exception.php';

                    require '../PHPMailer/src/SMTP.php';

                    $mail = new PHPMailer(true);
//                  echo $femail.$token;
                    try {
                        //Server settings
//                      $mail->SMTPDebug = 2;
                        $mail->isSMTP();                                      
                        $mail->Host = ' smtp.gmail.com';
                        $mail->SMTPAuth = true;

                        $mail->Username = 'myusername';
                        $mail->Password = 'mypassword';

                        $mail->Port = 587; 
                        $mail->SMTPSecure = 'tls';
                        //Recipients
                        $mail->setFrom('email', 'name');
                        $mail->addAddress($femail);     // Add a recipient

                        //Content
                        $mail->isHTML(true);
                        $mail->Subject = 'Reset Password';`enter code here`                     $mail->Body    = '<h3>Click the link to reset your password</h3><br><a href= "http://localhost/admin-dashboard/resetPassword.php?email=$femail&token=$token">http://localhost/admin-dashboard/resetPassword.php?email=$femail&token=$token</a><br><h3>Regards<br>Moon</h3>';

//                      $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

                        $mail->send();
                        echo 'Message has been sent';
                    } catch (Exception $e) {
                        echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
                    }

Upvotes: 0

Views: 560

Answers (1)

Synchro
Synchro

Reputation: 37710

Aside from the fact that you commented out the line that does the work, you're using the wrong type of quotes - single quotes do not support variable interpolation. To avoid collision of quote types within the string (e.g. where you quote attributes), either use single quotes, or escape the double quotes:

$mail->Body = "<h3>Click the link to reset your password</h3><br><a href= 'http://localhost/admin-dashboard/resetPassword.php?email=$femail&token=$token'>http://localhost/admin-dashboard/resetPassword.php?email=$femail&token=$token</a><br><h3>Regards<br>Moon</h3>";

or:

$mail->Body = "<h3>Click the link to reset your password</h3><br><a href= \"http://localhost/admin-dashboard/resetPassword.php?email=$femail&token=$token\">http://localhost/admin-dashboard/resetPassword.php?email=$femail&token=$token</a><br><h3>Regards<br>Moon</h3>";

Note that using localhost as the host name in these URLs will not work for anyone except you - you need a real host name in there, and you should be using TLS (i.e. https URLs).

Also, remove the leading space from the email host name:

$mail->Host = 'smtp.gmail.com';

Upvotes: 1

Related Questions