Today suddenly want to write a user to change the password function, and no SMS interface, can only choose to send verification code with the mailbox, poor Ah, no way, haha, the following is the text.
--------------------------------------------------------------------------------------------------------------- ---------------------------------------------
1. Preparatory work
(1) Download Phpmailer, plugin,: Link: http://pan.baidu.com/s/1hskrDqs Password: DCS9
(2) Compress the downloaded files and put them in the thinkphp extension library (Personal storage path: root directory \thinkphp\library\vendor\phpmailer)
2. Code spelling
(1) within the root directory \ Project directory \common\conf\config.php, add the following code;
//Configuring the Mail sending server' Mail_host ' = ' smtp.163.com ',//name of the SMTP server' Mail_smtpauth ' =TRUE,//Enable SMTP authentication' Mail_username ' = ' [email protected] ',//Sender's mailbox name' Mail_password ' = ' 9999999 ',//163 Mailbox Sender Authorization Password' Mail_from ' = ' [email protected] ',//Sender e-mail address' Mail_fromname ' and ' name ',//Sender's name' Mail_charset ' = ' utf-8 ',//set up message encoding' Mail_ishtml ' =TRUE,//whether HTML-formatted messages
Note: The following parameters
The sender's mailbox name,
163 Mailbox Sender Authorization Password,
Sender e-mail address,
Sender's name
Please make your own changes (with 163 mailbox Sender Authorization Password acquisition method).
(2) within the root directory \ Project directory \common\common, create a new function.php file and add the following code;
<?PHP/** Send mail * @param $to String * @param $title String * @param $content String * @return bool **/functionSendMail ($to,$title,$content) {Vendor (' Phpmailer.phpmailerautoload '); $mail=NewPhpmailer ();//instantiation of $mail->ISSMTP ();//Enable SMTP $mail->host=c (' Mail_host ');//name of the SMTP server (for example, QQ mailbox here) $mail->smtpauth = C (' Mail_smtpauth ');//Enable SMTP authentication $mail->username = C (' Mail_username ');//Sender Mailbox Name $mail->password = C (' Mail_password ');//163 Mailbox Sender Authorization Password $mail->from = C (' Mail_from ');//Sender address (i.e. your e-mail address) $mail->fromname = C (' Mail_fromname ');//Sender's name $mail->addaddress ($to, "Dear Customer"); $mail-WordWrap= 50;//set character length per line $mail->ishtml (C (' mail_ishtml '));//whether HTML-formatted messages $mail->charset=c (' Mail_charset ');//set up message encoding $mail->subject =$title;//Message Subject $mail->body =$content;//Message Content $mail->altbody = "This is a plain text body in a non-profit HTML email client";//the message body does not support alternate display of HTML return($mail-Send ());}
(3) I use Ajax to send the verification code, the following is the HTML code;
<form method= "POST" action= "" > <input name= "forget_name" id= "Forget_name" placeholder= "Account" required= "Typ e= "Text" > class= "Hr15" > <input name= "forget_email" placeholder= "Mailbox" id= "Forget_email" required= "" type= "text" style= "Width:7 0% "> <a href=" # "onclick=" Send (); " > Send Verification Code </a> <HRclass= "Hr15" > <input name= "code" placeholder= "Verification Code" required= "" type= "text" > class= "Hr15" > <input name= "New_pass" placeholder= "New password" required= "" type= "text" > class= "Hr15" > <input value= "Send verification Code" style= "width:100%;" type= "Submit" > class= "Hr20" > <a href= "" > Login </a> </form> <script>functionSend () {varName = $ (' #forget_name ').Val (); varemail = $ (' #forget_email ').Val (); $.Ajax ({type: "Get",URL: "Http://127.0.0.1/index.php/Edu/Login/send.html",Async:true,ContentType: "Application/json",DataType: "JSON",Data: {"Forget_name": Name, "Forget_email": email},Success:function(data) {alert (data); } }); } </script> <script src= "__public__/admin/style/jquery.min.js" ></script>
(4) The following is the PHP code;
//Send Message Verification code Public functionSend () {if(is_ajax) {//Receive account information $data[' name '] = I (' Get.forget_name ')); if(!$data[' Name '])$this->ajaxreturn ("Please fill in the login account", 0); //Receive Mailbox $data[' email '] = I (' Get.forget_email ')); if(!$data[' Email '])$this->ajaxreturn ("Please fill in your email account", 0); //Verify the mailbox format $pattern= "/^ ([0-9a-za-z\\-_\\.] +) @ ([0-9a-z]+\\. [A-z] {2,3} (\\. [A-z] {2})?) $/i "; if(Preg_match($pattern,$data[' Email '])){ //The mailbox is formatted correctly to determine if the user exists $Manager= D (' Manager '); $condition[' Display '] = 1; $condition[' name '] =$data[' Name ']; $condition[' status '] > 1; $ManagerInfo=$Manager->where ($condition),find (); if($ManagerInfo){ //Check whether the user account is consistent with the mailbox if($ManagerInfo[' email '] = =$data[' Email ']){ //Setting Parameters $title= "Email Verification";//message Header $font= "This message is to retrieve password verification email, do not reply, your verification code is";//Message Content $code=Rand(1000,9999);//Random Code $content=$font.$code;//Stitching if(SendMail ($data[' Email '],$title,$content)) { //Save Verification Code $Condition[' Code '] =$code; $Condition[' updatetime '] = Time(); $Cond[' name '] =$data[' Name ']; $ManagerSave=$Manager->where ($Cond)->save ($Condition); if($ManagerSave){ $this->ajaxreturn ("sent successfully", 0); }Else{ $this->ajaxreturn ("Verification Code save failed", 0); } }Else{ $this->ajaxreturn ("Send Failed", 0); } }Else{ $this->ajaxreturn ("Please check the correctness of user information", 0); } }Else{ $this->ajaxreturn ("Please check user information", 0); } }Else{ $this->ajaxreturn ("Please check mailbox information", 0); } }Else{ $this->ajaxreturn (0); } }
(5) QQ Email Verification code to receive the situation;
Thinkphp3.2 version uses 163 email (verification code) mail