[英]PHP send email for Forgot Password
因此,我正在構建一個本地主機頁面,其中包含用戶,因此我創建了一個“忘記密碼”頁面,以在他們忘記密碼時重置密碼,並將隨機密碼更新到數據庫中。
這是ForgotPassword的代碼,我通過驗證電子郵件而不是ID來使用它。
<h2 style="text-align:center;"> Forgot Password </h2>
<table align="center" bgcolor="#FFFFFF" width="500" height ="180">
<form action="doForgotPassword.php" method="post">
<tr><td align="right" height="50" width="200">First Name</td>
<td><input type="text" name="first_name" /></td></tr>
<tr><td align="right" height="50">Email</td>
<td><input type="text" name="email" /></td></tr>
<tr><td colspan="2" align="right"><input type="submit" value="Submit"/></td></tr>
</table> </form> </td></table>
這是DoForgotPassword的代碼,我設法在數據庫中進行了更改,但無法通過電子郵件發送給用戶。 我在這條線有麻煩
<?php
public function sendMail($email =$_POST['email'] , $id)
這是后面的其余代碼。
{
$to = $email /* separated by comma for another email (useful if you want to keep records(sending to yourself))*/;
$subject = 'INSERT_SUBJECT_HERE';
$bound_text = "----*%$!$%*";
$bound = "--".$bound_text."\r\n";
$bound_last = "--".$bound_text."--\r\n";
$headers = "From: noreply@somewhere.com\r\n";
$headers .= "MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed; boundary=\"$bound_text\""."\r\n" ;
$message = " you may wish to enable your email program to accept HTML \r\n".
$bound;
$message .=
'Content-Type: text/html; charset=UTF-8'."\r\n".
'Content-Transfer-Encoding: 7bit'."\r\n\r\n".
'
<font size="3" color="#000000" style="text-decoration:none;font-family:Lato light">
<div class="info" Style="align:left;">
<p>information here<!--(im sure you know how to write html ;))--></p>
<br>
'. /* <p>Charge: '.$charge.' </p> */'
<br>
<p>Reference Number: '.$id.'</p>
</div>
</br>
<p>-----------------------------------------------------------------------------------------------------------------</p>
</br>
<p>( This is an automated message, please do not reply to this message, if you have any queries please contact someone@someemail.com )</p>
</font>
</div>
</body>
'."\n\n".
$bound_last;
$sent = mail($to, $subject, $message, $headers); // finally sending the email
}
$email =$_POST['email'];
function createRandomPassword() {
$chars = "ABCDEFGHJKLMNOPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz0123456789";
$i = 0;
$pass = '' ;
while ($i <= 8) {
$num = mt_rand(0,61);
$tmp = substr($chars, $num, 1);
$pass = $pass . $tmp;
$i++;
}
return $pass;
}
$pw = createRandomPassword();
$query = "UPDATE users SET password= SHA1('$pw') WHERE email = '$email' ";
$result = mysqli_query($link, $query);
if ($result){
$query3 = "SELECT * FROM users where email = '$email'";
$sql = mysqli_query($link, $query3) or die(mysqli_error());
$rownum = mysqli_num_rows($sql);
if(!$rownum ) {
echo "We can not find your email in our records";
}
}
這是結果代碼
if($result){
if(isset($_POST['id'])){
$id = $_POST['id'];
sendMail($email, $id);
}
}
?>
這是我在當前應用程序上處理的任何郵件所使用的功能(已刪除敏感信息),很容易弄清楚您需要做什么,但提供的注釋應該回答一些問題,
此功能允許html格式化,這應該使您變得漂亮:D
public function sendMail($email, $company, $location, $startAt, $endAt, $date, $userId,$duration)
{
$to = $email; /* '. ','; separated by comma for another email (useful if you want to keep records(sending to yourself))*/;
$subject = 'INSERT_SUBJECT_HERE';
$bound_text = "----*%$!$%*";
$bound = "--".$bound_text."\r\n";
$bound_last = "--".$bound_text."--\r\n";
$headers = "From: noreply@somewhere.com\r\n";
$headers .= "MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed; boundary=\"$bound_text\""."\r\n" ;
$message = " you may wish to enable your email program to accept HTML \r\n".
$bound;
$message .=
'Content-Type: text/html; charset=UTF-8'."\r\n".
'Content-Transfer-Encoding: 7bit'."\r\n\r\n".
'
<!-- here is where you format the email to what you need, using html you can use whatever style you want (including the use of images)-->
<BODY BGCOLOR="White">
<body>
<div Style="align:center;">
<p>
<img src="IMAGE_URL" alt= "IMAGE_NAME">
</p>
</div>
</br>
<div style=" height="40" align="left">
<font size="3" color="#000000" style="text-decoration:none;font-family:Lato light">
<div class="info" Style="align:left;">
<p>information here<!--(im sure you know how to write html ;))--></p>
<br>
<p>Location: '.$location.' <!-- $location is the variable you wish to insert as is $date etc --> </p>
<p>Date: '.$date.' </p>
<p>Time: '.$startAt.' </p>
<p>Duration: '.$duration.' </p>
<p>Company: '.$company.' </p>
'. /* <p>Charge: '.$charge.' </p> */'
<br>
<p>Reference Number: '.$userId.'</p>
</div>
</br>
<p>-----------------------------------------------------------------------------------------------------------------</p>
</br>
<p>( This is an automated message, please do not reply to this message, if you have any queries please contact someone@someemail.com )</p>
</font>
</div>
</body>
'."\n\n".
$bound_last;
$sent = mail($to, $subject, $message, $headers); // finally sending the email
}
這絕對可以工作(只要您替換INSERT _...等),除了我不能立即看到您的代碼有任何問題,我唯一能真正建議的就是檢查您的應用程序錯誤日志,它應該給您一個指示是否有任何錯誤。
否則檢查您的php ini文件,看看是否已激活sendmail :)
問候史蒂夫。
---------編輯---------
因此您的新代碼應遵循以下原則:
<?php
$email =$_POST['email'];
public function sendMail($email, $userId)
{
$to = $email;
$subject = 'Password Reset';
$bound_text = "----*%$!$%*";
$bound = "--".$bound_text."\r\n";
$bound_last = "--".$bound_text."--\r\n";
$headers = "From: noreply@somewhere.com\r\n";
$headers .= "MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed; boundary=\"$bound_text\""."\r\n" ;
$message = " you may wish to enable your email program to accept HTML \r\n".
$bound;
$message .=
'Content-Type: text/html; charset=UTF-8'."\r\n".
'Content-Transfer-Encoding: 7bit'."\r\n\r\n".
'
<BODY BGCOLOR="White">
<body>
</br>
<div style=" height="40" align="left">
<font size="3" color="#000000" style="text-decoration:none;font-family:Lato light">
<div class="info" Style="align:left;">
<p>place link here for password reset</p>
<p>Reference Number: '.$userId.'</p>
</div>
</br>
<p>-----------------------------------------------------------------------------------------------------------------</p>
</br>
<p>( This is an automated message, please do not reply to this message, if you have any queries please contact someone@someemail.com )</p>
</font>
</div>
</body>
'."\n\n".
$bound_last;
$sent = mail($to, $subject, $message, $headers); // finally sending the email
}
function createRandomPassword() {
$chars = "ABCDEFGHJKLMNOPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz0123456789";
$i = 0;
$pass = '' ;
while ($i <= 8) {
$num = mt_rand(0,61);
$tmp = substr($chars, $num, 1);
$pass = $pass . $tmp;
$i++;
}
return $pass;
}
$pw = createRandomPassword();
$query = "UPDATE users SET password= SHA1('$pw') WHERE email = '$email' ";
$result = mysqli_query($link, $query);
if ($result){
$query3 = "SELECT * FROM users where email = '$email'";
$sql = mysqli_query($link, $query3) or die(mysqli_error());
$rownum = mysqli_num_rows($sql);
if(!$rownum ) {
echo "We can not find your email in our records";
}
}
if($result){
$this->sendMail($email, $userId); /*does it need to be in a class for $this->? or can you call functions within the php page without?*/
} ?>
------編輯------
好的函數調用應該是sendMail($variableOne, $variableTwo)
如果您在sendMail函數的開始處刪除了變量,則應將其設置為function sendMail($email){/*code*/}
然后嘗試執行該操作(發送電子郵件需要使用電子郵件)
------編輯------
我最近對該功能進行了重新設計,以允許使用Gmail SMTP。如果您從RamDev簽出此頁面(請查看Stephen Penn的帖子) ,我將解釋步驟以及我構建的功能,這可能有助於您了解流程通過SMTP的郵件。
再次感謝,
史蒂夫
<?php
include "connection.php";
echo "<br>";
if(isset($_POST['submit']))
{
$email = $_POST['email'];
$check = mysql_query("select * from register where email = '$email' ");
$find = mysql_num_rows($check);
if($find)
{
function makepassword($length)
{
$validCharacters = "ABCDEFGHIJKLMNPQRSTUXYVWZ123456789";
$validCharNumber = strlen($validCharacters);
$result ="";
for ($i = 0; $i < $length; $i++)
{
$index = mt_rand(0, $validCharNumber - 1);
$result .= $validCharacters[$index];
}
return $result;
}
$random_password = makepassword(10);
//echo $password_final = md5($random_password);
//echo "update set pass ='$random_password' where email = '$email'"; exit;
$final_result = mysql_query("update register set pass ='$random_password' where email = '$email' ");
if($final_result)
{
echo "Your Password is:".$random_password;
}
}
else
{
echo "Email Not Found";
}
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.