忘记在php中发送邮件的密码
这是我忘记的密码。。当我测试它时,它工作得很好。它将新密码添加到数据库中。但它不会向用户发送新密码邮件忘记在php中发送邮件的密码,php,mailer,Php,Mailer,这是我忘记的密码。。当我测试它时,它工作得很好。它将新密码添加到数据库中。但它不会向用户发送新密码邮件 <?php function generateRandomString($length = 15) { $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $charactersLength = strlen($characters); $rand
有人能帮我解决这个问题吗?这将非常有帮助<?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>'; } ?>
调用内置函数。它只需要3个必要的参数:发送到的电子邮件,所述电子邮件的主题和正文。我真的很抱歉浪费你的时间,我使用了你的代码并修改了一些,但现在数据库没有得到新密码的更新,邮件也没有移动到用户。我可以给你发送重置php文件吗?你可以编辑吗。比如,在这里输入-,改变这个-。这将非常有帮助。我这样做了两天,找不到解决办法。是的,伙计,说吧。我不会真的为你们这么做,但我会写几点给你们,你们可以去读我所说的,然后自己改变它好的,你有很多问题。正如注释所示,您目前根本没有调用实际的mail()函数 mail()函数接受许多参数,如果构造不正确,实际传递的邮件可能会有很多问题 这一行:
应该是:$mnl = mail($_POST['email'],'Reset Password',$msg,$headers);
这意味着如果函数成功,您将得到一个布尔值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") ;
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; }
希望这有助于你在某些方面的形状或形式 我没有看到函数。我看到您试图将mail()
用作邮件功能,但我没有看到定义任何$mml
。如果您能帮助我进行更改,那将非常有用。。该模板附带了$mml
。@WEBFOX,您只需使用适当的参数调用$mml
。@AlexBarker我对编码是新手,我刚刚开始学习。你能确定并更改应该更改的代码吗?这将非常有帮助。我尝试了各种可能的论点,比如邮件和一些来自网络的东西。但它会更改数据库中的密码,但不会发送邮件。@WEBFOX,在新密码发送到您的邮件之前的某个地方mail()
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);