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
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();
}
?>
Reset Password