2016-07-01 56 views
-1

我已經做了「忘記密碼」頁面,以便當用戶提交他們的emailid,然後從電子郵件數據庫相匹配,並且如果然後電子郵件存在的密碼發送到該郵箱無法接收電郵。 mail()函數用於發送電子郵件。如果我提交則消息顯示使用mail()函數

發送到您的郵箱帳號密碼。請立即檢查您的電子郵件!

但消息沒有收件箱或垃圾郵件會。我的託管是LINUX託管。

<form method="post" id="loginForm" name="loginForm" action="" onsubmit="return executeOnSubmit();"> 
    <div class="col-lg-4" style="text-align:right;"> 
     Email ID&nbsp;<span style="color:red">*</span> 
    </div> 
    <div class="col-lg-8"> 
     <input type="email" class="form-control" value="<?php echo $email; ?>" name="email" placeholder="Enter email address" required /> 
    </div> 
    <div style="clear:both;"><br /></div> 
    <div class="col-lg-4"></div> 
    <div class="col-lg-8 pull-right"> 
     <input type="submit" class="btn btn-success" value="Submit" name="submit" /> 
    </div> 
</form> 

<?php 
$email = ""; 
if(isset($_POST["submit"])) 
{ 
    $email = $_POST["email"]; 
    $res=mysql_query("select * from mainaccount where email='$email'") or die(mysql_error()); 
    if($row = mysql_fetch_array($res)) 
    { 
     extract($row); 
     $msg = "Hi User,\n\nEmail: ".$email."\n\nPassword: ".$password; 
     $smail=mail($email,"Scholarship Forgot Password!","Forgot Password Details: ",$password); 
     if(!$smail) 
     { 
      echo "<span style='color:red;'>&nbsp;&mdash;&nbsp;Mail Not Send!</span>"; 
     } 
     else 
     { 
      echo "&nbsp;&mdash;&nbsp;Password sent to your email id. Please check your email now!"; 
     } 
    } 
    else 
    { 
     echo "<span style='color:red;'>Email id does not match!</span>"; 
    } 
} 
?> 
+3

必讀:爲什麼不呢?我應該使用mysql_ * PHP函數(https://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in- PHP),[我怎樣才能防止SQL注入的PHP?](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php),https://開頭XKCD .COM/327 /,http://plaintextoffenders.com/about/(另外,不要訪問任何網站,你再次得到這個90級的代碼)。 –

+2

注意,不要將密碼存儲爲明文。您需要允許用戶重置密碼,而不是取回密碼。看看'password_hash()'。 – Rasclatt

+0

檢查ARGS:'郵件(字符串$到,串$主題,串$消息[,字符串$ additional_headers [,字符串$ additional_parameters])' – Progrock

回答

1

您可以使用PHPMailer。從https://github.com/PHPMailer/PHPMailer下載庫。

<?php 

function send_mail($email, $recipient_name, $subject, $message='') 
{ 
    require("phpmailer/class.phpmailer.php"); 

    $mail = new PHPMailer(); 

    $mail->CharSet="utf-8"; 
    $mail->IsSMTP();          // set mailer to use SMTP 
    $mail->Host = "mail.example.com"; // specify main and backup server 
    $mail->SMTPAuth = true;  // turn on SMTP authentication 
    $mail->Username = "myusername"; // SMTP username 
    $mail->Password = "[email protected]"; // SMTP password 

    $mail->From = "[email protected]"; 
    $mail->FromName = "System-Ad"; 
    $mail->AddAddress($email, $recipient_name); 

    $mail->WordWrap = 50;         // set word wrap to 50 characters 
    $mail->IsHTML(true);         // set email format to HTML (true) or plain text (false) 

    $mail->Subject = $subject; 
    $mail->Body = $message; 
    $mail->AltBody = "This is the body in plain text for non-HTML mail clients";  
    // $mail->AddEmbeddedImage('images/logo.png', 'logo', 'logo.png'); 
    //$mail->addAttachment('files/file.xlsx'); 

    if(!$mail->Send()) 
    { 
     return false; 
    } 

    return true; 

} 


$email = ""; 
if(isset($_POST["submit"])) 
{ 
    $email = $_POST["email"]; 
    $res=mysql_query("select * from mainaccount where email='$email'") or die(mysql_error()); 
    if($row = mysql_fetch_array($res)) 
    { 
     extract($row); 
     $msg = "Hi User,\n\nEmail: ".$email."\n\nPassword: ".$password; 
     $smail= send_mail($email,"User name","Scholarship Forgot Password!",msg); 
     if(!$smail) 
     { 
      echo "<span style='color:red;'>&nbsp;&mdash;&nbsp;Mail Not Send!</span>"; 
     } 
     else 
     { 
      echo "&nbsp;&mdash;&nbsp;Password sent to your email id. Please check your email now!"; 
     } 
    } 
    else 
    { 
     echo "<span style='color:red;'>Email id does not match!</span>"; 
    } 
} 
?> 
+0

他們可以使用'富'郵件。列出替代軟件和庫可能會令人筋疲力盡。爲什麼在這裏使用PhpMailer會有幫助? – Progrock