忘记了用php发送邮件的密码
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了忘记了用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()函数。
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,'Password Reset Subject',$subject,$headers,$message) ; //Use SMTP var_dump($result);
希望这会以某种方式帮助您塑造形状!
以上是关于忘记了用php发送邮件的密码的主要内容,如果未能解决你的问题,请参考以下文章