繁体   English   中英

在 php 中忘记密码发送邮件

[英]Forgot password sending mail in php

这是我忘记的密码。当我测试它时,它的工作很迷人。 它将新密码添加到数据库。 但它没有向用户发送新密码邮件

谁能帮我解决这个问题? 这会很有帮助。

<?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>';
        }
?>

好的,所以你有很多问题。 正如评论所暗示的,您目前根本没有调用实际的 mail() function。

mail() function 需要许多 arguments 如果构造不正确,可能会出现许多邮件实际传递的问题。

这一行:

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

应该:

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

这意味着如果 FUNCTION 成功,您将获得 boolean 真/假。 (这并不一定意味着您的邮件将到达 go 所需的位置......)为了确保您的邮件实际上会到达预期的位置,您需要确保您的标题全部正确构建并且在此之上您的 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") ;

因此,在邮件 function 中包含 -f 开关很重要(强制它发送),还需要尽可能详细地构建标题以避免被垃圾邮件过滤器拾取。 您将不得不在自己的时间阅读有关正确的 header 构造它超出了我愿意在这里输入的 scope 的内容。

PHP 的邮件 function 通常很挑剔,但是,您可以将 PHPMailer 用作 class ,这使得很多这些事情变得容易得多。

Or you could look at using sockets or something and write your own function to send it using SMTP which is what I do sometimes, the following function is what I usually use if the chips are down and will give you at least a starting point to work从....

    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);

希望这能以某种方式帮助你塑造或形成!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM