Hi Team
I dont have errors i have debugged the application, basically what i am trying to do. When before submiting a form, the form should hold until a user receive an email has an otp, so that can insert and continue. Problem i am experience i am not receiving an email.Maybe its something i am missing, setup everything from the phpmailer because it is working for registering users to the site.
// forgot-password.php
PHP
$(document).ready(function() {
$('#forgot-password').validate({
rules: {
email: {
required: true,
email: true
},
'new-password': {
required: true,
minlength: 6
},
'confirm-password': {
required: true,
equalTo: '#new-password'
}
},
messages: {
email: {
required: 'Please enter your email address',
email: 'Please enter a valid email address'
},
'new-password': {
required: 'Please enter a password',
minlength: 'Your password must be at least 6 characters long'
},
'confirm-password': {
required: 'Please enter the same password as above',
equalTo: 'Your passwords do not match'
}
},
errorElement: 'div',
errorPlacement: function(error, element) {
// Add the Bootstrap "alert" class to the error message
error.addClass('alert alert-danger');
// Add the error message after the input element
error.insertAfter(element);
},
success: function(label) {
// Remove the Bootstrap "alert-danger" class from the label
label.removeClass('alert-danger');
// Add the Bootstrap "alert-success" class to the label
},
submitHandler: function(form) {
// Generate a random OTP
var otp = Math.floor(100000 + Math.random() * 900000);
// Store the email and new password in the session
sessionStorage.setItem('email', $("#email").val());
sessionStorage.setItem('newPassword', $("#new-password").val());
sessionStorage.setItem('otp', otp);
// Prompt the user to enter the OTP
var enteredOtp = prompt("Please enter the OTP sent to your email:");
// If the entered OTP matches the generated OTP, submit the form
if (enteredOtp == otp) {
// Submit the form to otp-confirmation.php
form.action = 'otp-confirmation.php';
form.submit();
} else {
alert("Invalid OTP. Please try again.");
}
}
});
});// otp-confirmation.php
PHP
prepare($sql);
$stmt->bind_param("s", $email);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows == 1) {
// Generate a random OTP and store it in the session
$otp = rand(100000, 999999);
$_SESSION['otp'] = $otp;
$_SESSION['email'] = $email;
// Send an email to the user with the OTP
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->Host = 'smtp.outlook.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = '***';
$mail->setFrom('[email protected]', 'Your Name');
$mail->addAddress($email);
$mail->Subject = 'Password reset OTP';
$mail->Body = 'Dear user,
Your OTP to reset your password is: ' . $otp;
$mail->AltBody = 'Dear user, your OTP to reset your password is: ' . $otp;
$mail->isHTML(true);
if (!$mail->send()) {
$error = 'Error: ' . $mail->ErrorInfo;
} else {
// Redirect the user to the OTP verification page
header("Location: verify_otp.php");
exit();
}
} else {
// If the email does not exist in the database, display an error message
$error = "Email not found. Please try again.";
}
}
?>
Rajkiran SwainRank #165 · 14.4K reputationPosted May 6, 2023, 9:59 AM EDT
It seems like you are trying to implement a forgot password functionality that sends an OTP to the user's email address for verification. However, it seems like the email is not being received by the user.
There could be several reasons why the email is not being received:
The email could be getting filtered as spam by the email provider. Make sure to check the spam folder of the email account.
The SMTP settings might not be configured correctly. Double-check the SMTP settings, including the hostname, port, and authentication details.
The email address might not be valid. Make sure to check that the email address is correct and without any typos.
The email might not be sent due to an error in the PHP code. Make sure to check that the PHP code is correct and there are no errors.
To debug the issue, you can try the following steps:
Check the spam folder of the email account.
Check the SMTP settings, including the hostname, port, and authentication details.
Try sending an email manually to the same email address using the same SMTP settings and see if it is received.
Check the PHP code for any errors or syntax issues.
Also, make sure to test the functionality on different email providers to ensure that the issue is not specific to a particular email provider.