我想创建一个忘记密码功能,该功能允许用户通过获取包含临时密码或令牌的电子邮件来重置密码。但是,尽管其他功能运行良好,但我无法发送电子邮件。
我认为该邮件未发送是因为我正在使用localhost,但是当我将其启用时,它仍然无法发送邮件。
更新查询功能正常,但无法继续发送邮件。我收到的错误是“发生了某些错误!请重试!”这是因为它无法执行$ mail-> send()函数。
这是表格。
<input class="form-control" id="email" placeholder="Your Email Address"><br>
<input type="button" class="btn btn-primary" value="Reset Password">
这是我的PHP代码。
<?php
use PHPMailer\PHPMailer\PHPMailer;
require_once "functions.php";
if (isset($_POST['email'])) {
$conn = new mysqli('localhost', 'root', '', 'db_peony');
$email = $conn->real_escape_string($_POST['email']);
$sql = $conn->query("SELECT id FROM db_account WHERE email='$email'");
if ($sql->num_rows > 0) {
$token = generateNewString();
$conn->query("UPDATE db_account SET token='$token',
tokenExpire=DATE_ADD(NOW(), INTERVAL 5 MINUTE)
WHERE email='$email'
");
require_once "PHPMailer/PHPMailer.php";
require_once "PHPMailer/Exception.php";
$mail = new PHPMailer();
$mail->addAddress($email);
$mail->setFrom("siti.afiqah@validatedlabs.com", "admin");
$mail->Subject = "Reset Password";
$mail->isHTML(true);
$mail->Body = "
Hi,<br><br>
In order to reset your password, please click on the link below:<br>
<a href='
website/resetPassword.php?email=$email&token=$token
'>website/resetPassword.php?email=$email&token=$token</a><br><br>
Kind Regards,<br>
My Name
";
if ($mail->send()){
exit(json_encode(array("status" => 1, "msg" => 'Please Check Your Email Inbox!')));
}
else{
exit(json_encode(array("status" => 0, "msg" => 'Something Wrong Just Happened! Please try again!')));
}
} else
exit(json_encode(array("status" => 0, "msg" => 'Please Check Your Inputs!')));
}
?>
提前谢谢!