gpt4 book ai didi

PHP 发送忘记密码的邮件

转载 作者:行者123 更新时间:2023-12-04 18:09:49 25 4
gpt4 key购买 nike

所以我正在构建一个本地主机页面,它包含用户,所以我创建了一个忘记密码页面,以便在他们忘记密码时重置密码,并将随 secret 码更新到数据库中。

这是 ForgotPassword 的代码,我通过验证电子邮件而不是 ID 使用它。

<h2 style="text-align:center;"> Forgot Password </h2>
<table align="center" bgcolor="#FFFFFF" width="500" height ="180">
<form action="doForgotPassword.php" method="post">
<tr><td align="right" height="50" width="200">First Name</td>
<td><input type="text" name="first_name" /></td></tr>

<tr><td align="right" height="50">Email</td>
<td><input type="text" name="email" /></td></tr>

<tr><td colspan="2" align="right"><input type="submit" value="Submit"/></td></tr>

</table> </form> </td></table>

这是 DoForgotPassword 的代码,我设法在数据库中更改了它,但无法通过电子邮件发送给用户。我在这条线上遇到了麻烦

    <?php
public function sendMail($email =$_POST['email'] , $id)

这是它之后的其余代码。

    {
$to = $email /* separated by comma for another email (useful if you want to keep records(sending to yourself))*/;
$subject = 'INSERT_SUBJECT_HERE';

$bound_text = "----*%$!$%*";
$bound = "--".$bound_text."\r\n";
$bound_last = "--".$bound_text."--\r\n";

$headers = "From: noreply@somewhere.com\r\n";
$headers .= "MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed; boundary=\"$bound_text\""."\r\n" ;

$message = " you may wish to enable your email program to accept HTML \r\n".
$bound;

$message .=
'Content-Type: text/html; charset=UTF-8'."\r\n".
'Content-Transfer-Encoding: 7bit'."\r\n\r\n".
'

        <font size="3" color="#000000" style="text-decoration:none;font-family:Lato light">
<div class="info" Style="align:left;">

<p>information here<!--(im sure you know how to write html ;))--></p>

<br>

'. /* <p>Charge: '.$charge.' </p> */'
<br>

<p>Reference Number: '.$id.'</p>

</div>

</br>
<p>-----------------------------------------------------------------------------------------------------------------</p>
</br>
<p>( This is an automated message, please do not reply to this message, if you have any queries please contact someone@someemail.com )</p>
</font>
</div>
</body>
'."\n\n".
$bound_last;

$sent = mail($to, $subject, $message, $headers); // finally sending the email


}
$email =$_POST['email'];


function createRandomPassword() {
$chars = "ABCDEFGHJKLMNOPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz0123456789";
$i = 0;
$pass = '' ;

while ($i <= 8) {
$num = mt_rand(0,61);
$tmp = substr($chars, $num, 1);
$pass = $pass . $tmp;
$i++;
}
return $pass;
}

$pw = createRandomPassword();
$query = "UPDATE users SET password= SHA1('$pw') WHERE email = '$email' ";
$result = mysqli_query($link, $query);
if ($result){
$query3 = "SELECT * FROM users where email = '$email'";
$sql = mysqli_query($link, $query3) or die(mysqli_error());
$rownum = mysqli_num_rows($sql);

if(!$rownum ) {
echo "We can not find your email in our records";
}

}

这是结果代码

     if($result){

if(isset($_POST['id'])){
$id = $_POST['id'];

sendMail($email, $id);

}
}
?>

最佳答案

这是我在当前应用程序上发送的任何邮件所使用的功能(已删除敏感信息),很容易弄清楚您需要做什么,但所提供的评论应该可以回答一些问题,

此函数允许进行 html 格式化,这应该能让您让它变得漂亮 :D

public function sendMail($email, $company, $location, $startAt, $endAt, $date, $userId,$duration)
{
$to = $email; /* '. ','; separated by comma for another email (useful if you want to keep records(sending to yourself))*/;
$subject = 'INSERT_SUBJECT_HERE';

$bound_text = "----*%$!$%*";
$bound = "--".$bound_text."\r\n";
$bound_last = "--".$bound_text."--\r\n";

$headers = "From: noreply@somewhere.com\r\n";
$headers .= "MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed; boundary=\"$bound_text\""."\r\n" ;

$message = " you may wish to enable your email program to accept HTML \r\n".
$bound;

$message .=
'Content-Type: text/html; charset=UTF-8'."\r\n".
'Content-Transfer-Encoding: 7bit'."\r\n\r\n".
'
<!-- here is where you format the email to what you need, using html you can use whatever style you want (including the use of images)-->
<BODY BGCOLOR="White">
<body>
<div Style="align:center;">
<p>
<img src="IMAGE_URL" alt= "IMAGE_NAME">
</p>
</div>
</br>
<div style=" height="40" align="left">

<font size="3" color="#000000" style="text-decoration:none;font-family:Lato light">
<div class="info" Style="align:left;">

<p>information here<!--(im sure you know how to write html ;))--></p>

<br>

<p>Location: '.$location.' <!-- $location is the variable you wish to insert as is $date etc --> </p>

<p>Date: '.$date.' </p>

<p>Time: '.$startAt.' </p>

<p>Duration: '.$duration.' </p>

<p>Company: '.$company.' </p>

'. /* <p>Charge: '.$charge.' </p> */'
<br>

<p>Reference Number: '.$userId.'</p>

</div>

</br>
<p>-----------------------------------------------------------------------------------------------------------------</p>
</br>
<p>( This is an automated message, please do not reply to this message, if you have any queries please contact someone@someemail.com )</p>
</font>
</div>
</body>
'."\n\n".
$bound_last;

$sent = mail($to, $subject, $message, $headers); // finally sending the email


}

这应该绝对有效(只要你替换 INSERT_... 等),除此之外我不能立即发现你的代码有任何问题,我唯一真正推荐的是检查你的应用程序错误日志,它应该如果有任何问题,给您一个指示。

否则请检查您的 php ini 文件以查看 sendmail 是否已激活 :)

问候史蒂夫。

--------编辑--------

所以你的新代码应该遵循;

<?php
$email =$_POST['email'];

public function sendMail($email, $userId)
{
$to = $email;
$subject = 'Password Reset';

$bound_text = "----*%$!$%*";
$bound = "--".$bound_text."\r\n";
$bound_last = "--".$bound_text."--\r\n";

$headers = "From: noreply@somewhere.com\r\n";
$headers .= "MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed; boundary=\"$bound_text\""."\r\n" ;

$message = " you may wish to enable your email program to accept HTML \r\n".
$bound;

$message .=
'Content-Type: text/html; charset=UTF-8'."\r\n".
'Content-Transfer-Encoding: 7bit'."\r\n\r\n".
'

<BODY BGCOLOR="White">
<body>

</br>
<div style=" height="40" align="left">

<font size="3" color="#000000" style="text-decoration:none;font-family:Lato light">
<div class="info" Style="align:left;">

<p>place link here for password reset</p>

<p>Reference Number: '.$userId.'</p>

</div>

</br>
<p>-----------------------------------------------------------------------------------------------------------------</p>
</br>
<p>( This is an automated message, please do not reply to this message, if you have any queries please contact someone@someemail.com )</p>
</font>
</div>
</body>
'."\n\n".
$bound_last;

$sent = mail($to, $subject, $message, $headers); // finally sending the email


}


function createRandomPassword() {
$chars = "ABCDEFGHJKLMNOPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz0123456789";
$i = 0;
$pass = '' ;

while ($i <= 8) {
$num = mt_rand(0,61);
$tmp = substr($chars, $num, 1);
$pass = $pass . $tmp;
$i++;
}
return $pass;
}

$pw = createRandomPassword();
$query = "UPDATE users SET password= SHA1('$pw') WHERE email = '$email' ";
$result = mysqli_query($link, $query);
if ($result){
$query3 = "SELECT * FROM users where email = '$email'";
$sql = mysqli_query($link, $query3) or die(mysqli_error());
$rownum = mysqli_num_rows($sql);

if(!$rownum ) {
echo "We can not find your email in our records";
}

}
if($result){
$this->sendMail($email, $userId); /*does it need to be in a class for $this->? or can you call functions within the php page without?*/
} ?>

------ 编辑 ------

好的函数调用应该是 sendMail($variableOne, $variableTwo)

如果您删除了 sendMail 函数开头的变量,那么它应该是 function sendMail($email){/*code*/> 并尝试这样做(需要电子邮件才能发送邮件)

------ 编辑 ------

我最近改造了这个函数以允许使用 Gmail SMTP,如果你看看这个 Page from RamDev (Check the post by Stephen Penn) ,我会解释步骤以及我构建的函数,它可以帮助你理解通过 SMTP 发送邮件的过程。

再次感谢,

史蒂夫

关于PHP 发送忘记密码的邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17150834/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com