我在 PHP 中创建了一个密码重置功能。
它工作得很好............除了,由于某种原因,我无法设置收件人的电子邮件地址:“ TO ”
代码以这种方式工作:
(a) 要求用户提供他的登录名/用户名 (b) php 向数据库发送一个 sql 查询;(c) 如果找到用户名,php 获取电子邮件地址,并通过电子邮件发送重置链接 (d) 此重置链接附有唯一的“令牌” (e) 用户点击他的链接电子邮件,并被重定向到他重置密码的新页面
一切正常............除了电子邮件结构本身。电子邮件包括: TO、CC、SUBJECT、BODY 和 HEADERS。
一切都在显示............除了实际的“ TO ”。
事实上,我知道代码有效的唯一原因是因为我通过“ CC ”获得了电子邮件的副本
这是我的代码:
if(isset($_POST['submit'])) {
$login = $_POST['login'];
$query = "select * from personal_data where login='$login'";
$result = mysqli_query($conn,$query);
$count=mysqli_num_rows($result);
$rows=mysqli_fetch_array($result);
if($count==0) {
echo "Sorry; that username does not exist in our database";
}
else {
function getRandomString($length)
{
$validCharacters = "ABCDEFGHIJKLMNPQRSTUXYVWZ123456789!#+=%&/?*$";
$validCharNumber = strlen($validCharacters);
$result = "";
for ($i = 0; $i < $length; $i++) {
$index = mt_rand(0, $validCharNumber - 1);
$result .= $validCharacters[$index];
}
return $result; }
$token=getRandomString(40);
$q="insert into token (token,login) values ('".$token."','".$login."')";
mysqli_query($conn,$q);
function mailresetlink($to,$token){
$to = $rows['email'];
$subject = "Password Reset";
$uri = 'http://'.$_SERVER['HTTP_HOST'] ;
$message = '
<html>
<head>
<title>Password Reset Link</title>
</head>
<body>
<p>We received a Password-Reset request from your account.</p>
<p>Click on the following link to reset your password : <a
href="'.$uri.'/PHP/password_reset?token='.$token.'">Reset Password</a></p>
</body>
</html>
';
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: Support<support@xxxxx.com>' . "\r\n";
$headers .= 'Bcc: Info<info@xxxxx.com>' . "\r\n";
if(mail($to, $subject, $message, $headers)) {
echo "A password reset link has been sent to your email address."
}
}
if(isset($_POST['login'])) {
mailresetlink($email,$token);
exit();
}
}
}