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
JavaScript
$(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
PHP
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
Email
New Password
Confirm Password
Reset Password
Tuhin PaulRank #7 · 71.9K reputationPosted Apr 29, 2023, 8:16 PM EDT
The script seems to be mostly correct, but the error message is not very clear. Here are a few things you can check:
1. Check that you have included the PHPMailer library correctly at the top of your script:
use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\SMTP; use PHPMailer\PHPMailer\Exception;2. Check that the password for the Outlook email account is correct and that you have entered it in the script correctly:
$mail->Password = "***"; // SMTP password3. Check that the email address entered in the HTML form is valid:
if(!filter_var($recipient_email, FILTER_VALIDATE_EMAIL)) { throw new Exception('Invalid email address.'); }4. Check that your SMTP server configuration is correct:
$mail->Host = 'smtp-mail.outlook.com'; // Enter your SMTP server here $mail->SMTPSecure = 'tls'; $mail->Port = 587; // Port number depends on your SMTP server configuration5. Check that you have enabled less secure apps in your Outlook account settings.
If none of these steps solve the issue, you may need to provide more information about the error message you are receiving.
Rajkiran SwainRank #165 · 14.4K reputationPosted Apr 28, 2023, 6:45 AM EDT
if(isset($_POST['email']) && !empty($_POST['email'])){ $recipient_email = $_POST['email']; // Validate the email address if(!filter_var($recipient_email, FILTER_VALIDATE_EMAIL)) { throw new Exception('Invalid email address.'); } $mail->addAddress($recipient_email, 'Recipient Name'); } else { throw new Exception('Recipient email address is not set or empty.'); }try { // Send the email if (!$mail->send()) { throw new Exception('Failed to send email.'); } 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(); }Guest UserRank #877 · 2.5K reputationPosted Apr 28, 2023, 7:11 AM EDT
Hi Swain
I have update the scripting code, but i am still getting the same error from this scritp. Below is my latest script code.
use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\SMTP; use PHPMailer\PHPMailer\Exception; // Create a new PHPMailer instance /* Create a new PHPMailer object. Passing TRUE to the constructor enables exceptions. */ $mail = new PHPMailer(TRUE); try { // Server settings $mail->SMTPDebug = SMTP::DEBUG_OFF; $mail->isSMTP(); $mail->Host = 'smtp-mail.outlook.com'; // Enter your SMTP server here $mail->SMTPAuth = true; $mail->Username = "[email protected]"; $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']; // Validate the email address if(!filter_var($recipient_email, FILTER_VALIDATE_EMAIL)) { throw new Exception('Invalid email address.'); } $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 try { // Send the email if (!$mail->send()) { throw new Exception('Failed to send email.'); } 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(); } } catch (Exception $e) { // Output an error message if the email failed to send echo 'Message could not be sent. Mailer Error: ', $e->getMessage(); } ?>