Click here to Skip to main content
15,921,841 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi team

I am trying to create a reset password where users can be able get an otp and email. I am using phpmailer to do this, problem now i am not getting an email and need some help

What I have tried:

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

require 'vendor/autoload.php';

// Create a new PHPMailer instance
$mail = new PHPMailer(true);

try {
    // Server settings
    $mail->Host       = '';
    $mail->SMTPAuth   = true;
    $mail->Username   = '';
    $mail->Password   = '';
    $mail->SMTPSecure = 'tls';
    $mail->Port       = 587;

    // Recipients
    $mail->setFrom('', 'Gcobani');
    $mail->addAddress('', 'Your Name'); // Add a recipient

    // Content
    $mail->isHTML(true); // Set email format to HTML
    $mail->Subject = 'Test Email';
    $mail->Body    = 'This is a test email';

    // Send the email
    echo 'Message has been sent';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";

// javascript
$(document).ready(function() {
  // Retrieve the email address from the URL
  var urlParams = new URLSearchParams(;
  var email = urlParams.get('email');

  // Display the email address in the form

  // Submit the form with the OTP code
  $('#reset-password').submit(function(event) {
    var otp = $('#otp').val();
      url: 'reset-password.php',
      method: 'POST',
      data: { email: email, otp: otp },
      success: function(response) {
        if (response == 'success') {
          // Display a success message and redirect the user to the login page
          alert('Your password has been reset. Please log in with your new password.');
          window.location.href = 'login.php';
        } else {
          // Display an error message if the OTP code is incorrect
          alert('The OTP code you entered is incorrect. Please try again.');

// 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 class="card-body">
          <form id="reset-password-form">
            <div class="form-group">
              <label for="email">Email</label>
              <input type="email" class="form-control" id="email" name="email" required>
            <div class="form-group">
              <label for="otp">OTP Code</label>
              <input type="text" class="form-control" id="otp" name="otp" required>
            <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 class="form-group">
              <label for="confirm-password">Confirm Password</label>
              <input type="password" class="form-control" id="confirm-password" name="confirm-password" required>
            <button type="submit" class="btn btn-primary">Reset Password</button>

Rebecca2002 24-Apr-23 5:30am    
I can't help you with javascript or otp codes for that matter but your mail is build wrong your not sending anything to anyone

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900