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
}
?>