PHP发送忘记密码的电子邮件

Posted

技术标签:

【中文标题】PHP发送忘记密码的电子邮件【英文标题】:PHP send email for Forgot Password 【发布时间】:2013-06-13 14:27:41 【问题描述】:

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

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

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

            <tr><td align="right" >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);

           
         
                ?>

【问题讨论】:

你真的得到了 $result 吗? 有错误信息吗?你有什么迹象表明有问题?电子邮件是否发送但从未到达用户的收件箱? 您不应该发送密码。您应该向他们发送更改密码的链接。 是的。邮件已发送但未到达收件箱,我什至尝试使用其他邮件但无济于事。 不需要 $this-> (用于使用类结构(我经常使用类,所以会感到困惑))除了它应该可以工作 ^o)之外,您是否设置了$id 【参考方案1】:

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

这个函数允许 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="  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="  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发送邮件的过程。

再次感谢,

史蒂夫

【讨论】:

我是否将其放在我的 if($result) 中?或者另一个函数然后在里面回显它?抱歉,我对 php 很陌生,尤其是电子邮件 是的,只需将代码输入到 php 页面上的任意位置,然后在 if($result) $this->sendMail($parameterOne, $parameterTwo,etc) 应该放在 里面吧?但它在 public function sendMail($email, $company, $location, $startAt, $endAt, $date, $userId,$duration) 语法错误 能否在编辑中发布错误消息和新的代码行? 语法错误:内联 html 后的意外公共该行是公共函数 sendMail($email, $id)【参考方案2】:
<?php 
include "connection.php";
echo "<br>";
if(isset($_POST['submit']))

$email = $_POST['email'];
$check = mysql_query("select * from register where email = '$email' ");
$find = mysql_num_rows($check);
if($find)

function makepassword($length)

$validCharacters = "ABCDEFGHIJKLMNPQRSTUXYVWZ123456789";
$validCharNumber = strlen($validCharacters);
$result ="";
for ($i = 0; $i < $length; $i++) 

$index = mt_rand(0, $validCharNumber - 1);
$result .= $validCharacters[$index];

return $result;

$random_password = makepassword(10);
//echo $password_final = md5($random_password);
//echo "update set pass ='$random_password' where email = '$email'"; exit;
$final_result = mysql_query("update register set pass ='$random_password' where email = '$email' ");
if($final_result)

echo "Your Password is:".$random_password;


else

echo  "Email Not Found";


?>

【讨论】:

以上是关于PHP发送忘记密码的电子邮件的主要内容,如果未能解决你的问题,请参考以下文章

PHP忘记密码方法[重复]

忘记登录 - PHP 问题

提交时忘记密码导致空白页面

发送密码忘记邮件

Magento“忘记密码”电子邮件以错误的语言发送

在 Asp.Net 中使用电子邮件发送忘记密码