忘记密码在php中发送邮件

时间:2019-09-30 21:09:04

标签: php mailer

这是我忘记的密码。它将新密码添加到数据库。但它没有向用户发送新的密码邮件

有人可以帮助我解决这个问题吗?这会很有帮助。

<?php
   function generateRandomString($length = 15) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    return $randomString;
}

include 'config.php';

$check=mysqli_num_rows(mysqli_query($conn,"SELECT * FROM users WHERE email='".$_POST['email']."'"));

        if($check==1)
        {
            $users=mysqli_fetch_assoc(mysqli_query($conn,"SELECT * FROM users WHERE email='".$_POST['email']."'"));
            $code=generateRandomString();
            $msg='Hy, '.$users['name'].' Your New Generated Password is: '.$code.'';
            $headers = "[Reset Password]";
            $mml($_POST['email'],'Reset Password',$msg,$headers);

            if($mml){
mysqli_query($conn,"UPDATE users SET password='".md5($code)."' where email='".$_POST['email']."'");

            echo '<div class="alert alert-success">
                      <strong>New Password Has Been Sent to Your Mail!</strong> Check Your Inbox.
                    </div>';} else {
echo '<div class="alert alert-warning">
                      <strong>Timeout, Try again later!</strong>.
                    </div>';
}

        } else {
                echo '<div class="alert alert-danger">
                              <strong>User not found!</strong>.
                            </div>';
        }
?>

1 个答案:

答案 0 :(得分:0)

好的,所以您有很多问题。正如评论所建议的那样,您目前根本没有调用实际的mail()函数。

mail()函数带有许多参数,如果构造不正确,则实际上会传递一些邮件问题。

此行:

 $mml($_POST['email'],'Reset Password',$msg,$headers);

应该是:

 $mnl = mail($_POST['email'],'Reset Password',$msg,$headers);

这意味着如果FUNCTION成功,您将得到一个布尔值true / false。 (这并不一定意味着您的邮件会到达需要去的地方...)为了确保您的邮件实际上将到达预期的去处,您需要确保所有标头都正确构造,并且在此之上您的DNS记录配置正确(DKIM,SPF等)

请考虑以下代码段:

    $hostname       = "yourhostname.org";
    $date           = date('r', time());
    $nameto         = "Mr. Target";
    $recipient      = "target@targetdomain.com";
    $from           = "origin_address@yourdomain.net";
    $namefrom       = "Mr. Sender";
    $subject        = "Password Reset Thingy";

    $headers = [];
    $headers[] = "MIME-Version: 1.0";
    //  $headers[] = "Content-type: text/plain; charset=iso-8859-1";
    $headers[] = "Content-type: text/html; charset=utf-8";
    $headers[] = "From: $namefrom <$from>";
    $headers[] = "Sender: $namefrom <$from>";
    //  $headers[] = "Bcc: Some Other Dude <me@mydomain.net>";
    $headers[] = "Reply-To: $namefrom <$from>";
    $headers[] = "To: $nameto <$recipient>";
    $headers[] = "Subject: $subject";
    $headers[] = "Message-ID: <". sha1(microtime(true)) ."@$hostname>"; //This is essential for getting to Gmail addresses.... 
    $headers[] = "X-Mailer: PHP/".phpversion();
    $headers[] = "Date: $date";
    $headers = implode("\r\n", $headers);

    $mnl = mail($recipient,$subject,$body,$headers,"-f $from") ;

因此,在邮件功能中包括-f开关很重要(强制其发送),还需要尽可能详细地构造标题,以免被垃圾邮件过滤器接收。您将不得不花费自己的时间阅读有关正确的标头构造的信息,这超出了我在这里要键入的内容。

PHP的邮件功能通常是挑剔的,但是,您可以将PHPMailer用作一个类,这会使很多这类事情变得容易得多。

或者您可以考虑使用套接字或其他东西,然后编写自己的功能以使用SMTP发送它,这是我有时要做的事情,以下功能是我通常在芯片故障时使用的功能,至少可以让您起步从...开始工作。

    function authSendEmail($from, $namefrom, $to, $nameto, $subject, $headers, $message) {


$smtpServer = "mail.yourhostname.com";
$port = "25"; //Or whatever the default port on your server is
$timeout = "30";
$username = "origin@yourdomain.com";
$password = "myemailpassword";
$localhost = "yourhostname.com";
$newLine = "\r\n";
$date = date('r', time()); 


    //Connect to the host on the specified port
$smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout);
$smtpResponse = fgets($smtpConnect, 515);
if(empty($smtpConnect)) {
    $output = "Failed to connect: $smtpResponse";
    return $output;
} else {
    $logArray['connection'] = "Connected: $smtpResponse";
}

   //Request Auth Login
fputs($smtpConnect,"AUTH LOGIN" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['authrequest'] = "$smtpResponse";

    //Send username
fputs($smtpConnect, base64_encode($username) . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['authusername'] = "$smtpResponse";

    //Send password
fputs($smtpConnect, base64_encode($password) . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['authpassword'] = "$smtpResponse";

    //Say Hello to SMTP
fputs($smtpConnect, "HELO $localhost" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['heloresponse'] = "$smtpResponse";

    //Email From
fputs($smtpConnect, "MAIL FROM: $from" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['mailfromresponse'] = "$smtpResponse";

    //Email To
fputs($smtpConnect, "RCPT TO: $to" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['mailtoresponse'] = "$smtpResponse";

    //The Email
fputs($smtpConnect, "DATA" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['data1response'] = "$smtpResponse";

fputs($smtpConnect, "To: $to\nFrom: $from\nSubject: $subject\n$headers\n\n$message\n.\n");
$smtpResponse = fgets($smtpConnect, 515);
$logArray['data2response'] = "$smtpResponse";

     // Say Bye to SMTP
fputs($smtpConnect,"QUIT" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['quitresponse'] = "$smtpResponse";

//Show log
//  echo "<pre>$to<br/>";
//  print_r ($logArray);
//  echo "</pre><hr />";
    $output = $logArray;
    return $output;
}   

然后您可以这样称呼它:

$result=authSendEmail($from,$fname,$to,$nameto,$subject,$headers,$message) ; //Use SMTP
var_dump($result);

希望这会以某种方式帮助您!