我需要一些帮助我正在尝试创建一个允许用户将 otp 发送到他们的电子邮件地址的表单,但现在我遇到了这个问题“消息无法发送。邮件错误:收件人电子邮件地址未设置或空的”。我不想为自己创建电子邮件,因为这会阻止用户不接收电子邮件到他们的帐户。相反,我希望他们在提交表格时,无论他们使用的电子邮件帐户是什么,都应该允许他们通过链接将他们的 otp 帐户接收到他们的电子邮件,重定向他们以使用 otp 验证。
<?php
```
`session_start();
// Get the directory of the current script
$currentDir = __DIR__;
$phpMailerDir = 'C:\wamp64\www\obaju\distribution\sendEmails\vendor\phpmailer\phpmailer';
require_once $phpMailerDir . '\src\PHPMailer.php';
require_once $phpMailerDir . '\src\SMTP.php';
require_once $phpMailerDir . '\src\Exception.php';
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'];
$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();
}
?>
<!DOCTYPE html>
<html>
<head>
<!-- CSS -->
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Obaju : e-commerce template</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="robots" content="all,follow">
<!-- Bootstrap CSS-->
<link rel="stylesheet" href="vendor/bootstrap/css/bootstrap.min.css">
<!-- Font Awesome CSS-->
<link rel="stylesheet" href="vendor/font-awesome/css/font-awesome.min.css">
<!-- Google fonts - Roboto -->
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,700">
<!-- owl carousel-->
<link rel="stylesheet" href="vendor/owl.carousel/assets/owl.carousel.css">
<link rel="stylesheet" href="vendor/owl.carousel/assets/owl.theme.default.css">
<!-- JS -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<!-- owl carousel-->
</head>
<body>
<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="forgot-password" method="post" action="otp-confirmation.php">
<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="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>
<!-- JavaScript files-->
<script src="vendor/jquery/jquery.min.js"></script>
<script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
<script src="vendor/jquery.cookie/jquery.cookie.js"> </script>
<script src="vendor/owl.carousel/owl.carousel.min.js"></script>
<script src="vendor/owl.carousel2.thumbs/owl.carousel2.thumbs.js"></script>
<script src="js/front.js"></script>
<script src="js/deletion_shopping_cart.js"></script>
<script src="js/add_to_cart.js"></script>
<script src="js/forgot-password.js"></script>
</body>
</html>`
```
// jquery 验证 /* *@作者:Gcobani Mkontwana *@日期:2023 年 4 月 20 日 *为用户重置密码。 **/
$(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.");
window.location.href = "dashboard.php";
}
}
});
});
});