PHP 忘记密码脚本不发送电子邮件

标签 php mysql forgot-password

下面有一个 PHP 忘记密码脚本。该脚本的想法是提交电子邮件地址,然后脚本查看该电子邮件地址是否在数据库中,然后更改密码,然后将带有临时地址的电子邮件发送到表单中提交的电子邮件。

该脚本似乎会更改密码并执行所有操作,但它似乎不会实际发送带有临时密码的电子邮件。

自从我使用这个脚本(或 PHP)以来已经很长一段时间了,所以我们将不胜感激。

    <?php # forgot_password.php

    require_once ('./includes/config.inc.php');
    $page_title = 'Forgot Password';
    include ('./includes/header.html'); 

    if (isset($_POST['submitted'])) { // Handle the form.

       require_once ('database-connection.php');

       if (empty($_POST['user_email'])) { //Validate the email address.
          $uid = FALSE;
          echo 'You forgot to enter your email address!';
       } else {

            $query = "SELECT ID FROM wp_users WHERE user_email='". escape_data($_POST['user_email']) . "'";
            $result = mysql_query ($query)or trigger_error("Query:$query\n<br />MySQL Error: " .mysql_error());
            if (mysql_num_rows($result)== 1) {
            list($uid) = mysql_fetch_array ($result, MYSQL_NUM);

       } else {
            echo 'This email address is not registered';
            $uid = FALSE;
       }
    }

       if ($uid) { // If everything's OK.
          $p = substr ( md5(uniqid(rand(),1)), 3, 10);
          $query = "UPDATE wp_users SET user_pass=SHA('$p') WHERE ID=$uid";
          $result = mysql_query ($query) or trigger_error("Query: $query\n<br />MySQL Error: " .mysql_error());
          if (mysql_affected_rows() == 1) {

            $body = "Your password to log into the site has been temporarily changed to '$p'. 
            Please log in using this password and your username. At that time you may change your password to something more familiar.";
            mail($_POST['user_email'], 'Your temporary password.', $body,
            'From:info@website.com.au'); //From email address
            echo 'Your password has been temporarily changed.
            An email from info@website.com.au will be sent to your registered email address with a new, temporary password which you can log in with.
            Once you have logged in with this password, you may change it by clicking on the "Change Password" link at the bottom of your screen.';
            mysql_close(); // Close the database connection.
            include ('./includes/footer.html'); // Include the HTML footer.
            exit();

          } else { //If it did not run OK.
            echo 'Your password could not be changed due to a system error. We apologize for any inconvenience.';
          }
       } else { // Failed the validation test.
          echo '<p><font color="red"size="+1">Please try again.</font></p>';
       }
       mysql_close(); // Close the database connection.
    } // End of the main Submit conditional.

    ?>

    <p>Enter your email address below and your password will be reset.</p>
    <form action="forgot_password.php" method="post">
       <p><b>Email Address:</b> <input type="text"
       name="user_email" size="30" maxlength="40" value="<?php if (isset($_POST['user_email'])) echo $_POST['user_email']; ?>" /></p>
        <div align="center"><input type="submit" name="submit" value="Reset My Password" /></div>
       <input type="hidden" name="submitted" value="TRUE" />
    </form>

    <?php
    include ('./includes/footer.html');
    ?>

最佳答案

当您在数据库中输入密码时,请尽量避免使用哈希算法。 SHA 是一种散列算法,而不是加密算法。请确保数据库中的密码字段必须具有较大的长度。我认为你的 SHA() 生成一个长字符串。如果一切正常,请将您的邮件标题更改为以下内容。

$header = "From: noreply@example.com\r\n"; 
$header.= "MIME-Version: 1.0\r\n"; 
$header.= "Content-Type: text/plain; charset=utf-8\r\n"; 
$header.= "X-Priority: 1\r\n"; 

并尝试

mail($to,$from,$message,$header);

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

相关文章:

php - 高性能地将 CESU-8 转换为 UTF-8

javascript - 通过将 <select> 值与列值进行比较来过滤 HTML 表格(PHP、HTML、Javascript)

php - 使用 Update CONCAT 将数据添加到数据库中已有的数据

mysql - 如果关系存在或不存在,请选择 TRUE 或 FALSE

MySQL分组查询优化

mysql - 为什么 select ... for UPDATE 会锁定插入?

authentication - 忘记密码 : what is the best method of implementing a forgot password function?

javascript - 以 html post 形式存储重置密码 token 是否安全?

javascript - 通过网络摄像头检测图像中的文本

java - 如何处理忘记密码