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:

<?php
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->isSMTP();
    $mail->Host       = 'smtp.webmail.com';
    $mail->SMTPAuth   = true;
    $mail->Username   = 'gcobani.mkontwana@agilelimitless.org.za';
    $mail->Password   = '';
    $mail->SMTPSecure = 'tls';
    $mail->Port       = 587;

    // Recipients
    $mail->setFrom('gcobani.mkontwana@agilelimitless.org.za', 'Gcobani');
    $mail->addAddress('your-email@example.com', '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
    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>
PHP




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

  // Display the email address in the form
  $('#email').val(email);

  // Submit the form with the OTP code
  $('#reset-password').submit(function(event) {
    event.preventDefault();
    var otp = $('#otp').val();
    $.ajax({
      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>
        <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>
            <div class="form-group">
              <label for="otp">OTP Code</label>
              <input type="text" class="form-control" id="otp" name="otp" required>
            </div>
            <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>
            <div class="form-group">
              <label for="confirm-password">Confirm Password</label>
              <input type="password" class="form-control" id="confirm-password" name="confirm-password" required>
            </div>
            <button type="submit" class="btn btn-primary">Reset Password</button>
          </form>
        </div>
      </div>
    </div>
  </div>
</div>
HTML

Posted
Comments
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