Guest User

Guest User

  • Tech Writer
  • 2.1k
  • 472.4k

not receiving email when form is submitted for otp

May 5 2023 9:59 AM

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

<script>
  $(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.");
  }
}

  });
});

</script>

// otp-confirmation.php

<?php
// Start the session
session_start();

// Include the PHPMailer library
require_once(__DIR__ . '/sendEmails/vendor/autoload.php');

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

// Check if the password reset form is submitted
if (isset($_POST['submit'])) {

    // Get the email from the form
    $email = $_POST['email'];

    // Include the database connection file
    require_once "dbconn.php";

    // Check if the email exists in the database
    $sql = "SELECT * FROM users WHERE email = ?";
    $stmt = $conn->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,<br><br>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.";
    }
}


?>

 


Answers (1)