0

I am trying to create a logic to reset password for user to receive an otp on their email. The issue i dont receive an email, there is no error when debugging and dont know why i am unable to get this to work.I am using phpmailer library and have use this library before and was able to receive an email without any issue, perhaps i am missing something from the jquery side. I also notice the values remain on the browser when sending an no alerts is triggered. Kindly please assist and will appreciate it.

`// html code
<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="reset-password">
            <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>`

// jquery code

$(document).ready(function(){
    $("#reset-password").submit(function(event){
        event.preventDefault();
        var formData = $(this).serialize();
        $.ajax({
            url: "reset-password.php",
            type: "POST",
            data: formData,
            success: function(data){
                if(data == "success"){
                    alert("Password reset successfully.");
                    window.location.href = "login.php";
                }else if(data == "error"){
                    alert("Invalid email or OTP code.");
                }else if(data == "password_mismatch"){
                    alert("New password and confirm password do not match.");
                }else{
                    alert("Something went wrong. Please try again later.");
                }
            }
        });
    });
`});
;`

// php code
<?php
    // Get the directory of the current script
$currentDir = __DIR__;

// Construct the path to the PHPMailer files
$phpMailerDir = $currentDir . '/../vendor_emails/phpmailer/phpmailer';

// Include the PHPMailer Autoload file
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
$mail = new PHPMailer();

// Set the mailer to use SMTP
$mail->isSMTP();

// Set the SMTP host
$mail->Host = 'smtp.webmail.com';

// Set the SMTP port
$mail->Port = 587;

// Enable SMTP authentication
$mail->SMTPAuth = true;

// Set the SMTP username and password
$mail->Username = 'gcobani.mkontwana@****.org.za';
$mail->Password = '****';

// Set the email address that the message will be sent from
$mail->setFrom('ggcobani@gmail.com', 'Gcobani Mkontwana');

// Set the email address that the message will be sent to
$mail->addAddress($email);

// Set the subject line for the email
$mail->Subject = 'Password Reset';

// Generate the OTP code
$otp = rand(100000, 999999);

// Set the body of the email
$mail->Body = 'Your OTP code is: ' . $otp;

// Attempt to send the email
if (!$mail->send()) {
    // If the email failed to send, output an error message
    echo 'Message could not be sent. Mailer Error: ' . $mail->ErrorInfo;
} else {
    // If the email was sent successfully, update the database with the OTP code and redirect the user to the reset password page
}

?>`

<?php
extract($_POST);
    // Get the directory of the current script
$currentDir = __DIR__;

// Construct the path to the PHPMailer files
$phpMailerDir = $currentDir . '/../vendor_emails/phpmailer/phpmailer';

// Include the PHPMailer Autoload file
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
$mail = new PHPMailer();

// Set the mailer to use SMTP
$mail->isSMTP();

// Set the SMTP host
$mail->Host = 'smtp.webmail.com';

// Set the SMTP port
$mail->Port = 587;

// Enable SMTP authentication
$mail->SMTPAuth = true;

// Set the SMTP username and password
$mail->Username = 'gcobani.mkontwana@agielimitless.org.za';
$mail->Password = 'MkontwanaG';

// Set the email address that the message will be sent from
$mail->setFrom('ggcobani@gmail.com', 'Gcobani Mkontwana');

// Set the email address that the message will be sent to
$mail->addAddress($_POST["email"]);

// Set the subject line for the email
$mail->Subject = 'Password Reset';

// Generate the OTP code
$otp = rand(100000, 999999);

// Set the body of the email
$mail->Body = 'Your OTP code is: ' . $otp;

// Attempt to send the email
if (!$mail->send()) {
    // If the email failed to send, output an error message
    echo 'Message could not be sent. Mailer Error: ' . $mail->ErrorInfo;
} else {
    // If the email was sent successfully, update the database with the OTP code and redirect the user to the reset password page
}

?>
  • It looks like you are trying to set a `From` address, that does not match the actual SMTP account you are using to send your mail. That is a thing you should absolutely **not** do, it will increase the likelihood of receiving email providers classifying your mail as spam, or even discarding it completely. See also, [PHP mail function doesn't complete sending of e-mail](https://stackoverflow.com/q/24644436/1427878), section titled _"Don't use a faux From: sender"_ – CBroe Apr 24 '23 at 07:04

1 Answers1

0

You have to pass the correct variable to the addAddress method in PHPMailer.

Since you are using POST method, one of the ways to fix the problem is to change the line

$mail->addAddress($email);

to

$mail->addAddress($_POST["email"]);

Alternatively, add the following line at the top of your PHP script:

extract($_POST);

If you added the line above, then you can use $mail->addAddress($email);

Ken Lee
  • 6,985
  • 3
  • 10
  • 29
  • i have change using post, still i am not receiving any email. or perhaps the issue is on path i am using for my phpmailer. "C:\wamp64\www\obaju\distribution\vendor_emails"(autoload.php) is on this path. Let me share current script so can see my newest changes – MyLegend2020 Wilson Apr 24 '23 at 04:08
  • The normal path of the autoload.php is like vendor/autoload.php and you should have `require 'vendor/autoload.php';` Please let us know how **did** you install the phpmailer (by composer ?) – Ken Lee Apr 24 '23 at 04:25
  • composer require phpmailer/phpmailer – MyLegend2020 Wilson Apr 24 '23 at 04:37
  • manage to fix the issue, now the issue i am getting. Message could not be sent. Mailer Error: Invalid address: (to): email – MyLegend2020 Wilson Apr 24 '23 at 16:27