The CI framework uses PHPmailer to send an email to retrieve the password, ciphpmailer

Source: Internet
Author: User
Tags preg email account

The CI framework uses PHPmailer to send an email to retrieve the password, ciphpmailer

Previously, PHP + Mysql + jQuery combined with ThinkPHP provided a user-verified mailbox password retrieval function. Now let's share the function of using the CI framework and PHPmailer to send a QQ mailbox to retrieve the password.

Start extension = php_openssl.dll & extension = php_sockets.dll of php. ini;

Then enable the smtp function for smtp Server Authentication mailbox. You can also use QQ mailbox for the 163 mailbox I use.

Enable the 163smtp function, log on to the 163 mailbox, and find "Settings ",

Enable the qq smtp function, log on to the QQ mailbox, and find "Settings ",

Download PHPmailer and decompress it to the/application/libreries/directory and rename it PHPmailer,

: Phpmailer download library for github.

These two files are mainly used:

Create the mailer. php file under libraries;

Defined ('basepath') OR exit ('no direct script access allowed'); class Mailer {function sendMail ($ emailsubject, $ emailbody, $ smtpemailto) {include_once ("PHPMailer/class. smtp. php "); // introduce the php mail class include_once (" PHPMailer/class. phpmailer. php "); // introduce the php mail class $ mail = new PHPMailer (); $ mail-> CharSet =" UTF-8 "; // encoding format $ mail-> IsSMTP (); $ mail-> SMTPAuth = true; // required. Check whether the SMTP server needs verification. If it is true, $ mail-> Host = "s is not required if it is false. Mtp.163.com "; // required. Set SMTP server $ mail-> Port = 465; // set Port $ mail-> Username =" email account for enabling smtp Service "; // required. The email address used to activate the SMTP service. $ mail-> Password = "Password used to enable the smtp service"; // required, the password for the above email address $ mail-> SMTPSecure = 'ssl '; // transmission protocol $ mail-> From = "sender's mailbox"; // required, sender Email $ mail-> FromName = "luokakale"; // required, sender nickname or name $ mail-> Subject = $ emailsubject; // required, mail title (Subject) $ mail-> MsgHTML ($ emailbody); // email content $ mail-> AddReplyTo ($ smtpemailto); // receive Recipient's email address $ mail-> AddAddress ($ smtpemailto); // recipient's email address $ mail-> IsHTML (true); // whether to send the email in HTML format. If not, please delete this line if (! $ Mail-> Send () {echo "failed to Send :". $ mail-> ErrorInfo;} The else {echo 'system has sent an email to your mailbox. <br/> Please log on to your mailbox and reset your password in time! ';}}}

Note: If the protocol is ssl, the port is 465; if the protocol is tsl, the port is 25.

The next step is to verify your mailbox. You don't need to worry about the parameters in the above code, so I will explain them later.

First, you must have an interface to retrieve the password and enter the email address ,:

The corresponding program is as follows:

1 <form id = "two"> 2 <ul> 3 <li> 4  5 <label> email </label> 6 <input type = "text" placeholder = "Enter the email address" id = "password"/> 7 </li> 8 </ul> 9 <input type = "button" value = "Send email" id = "sendEmail"/> 10 </form>

I will not write the style, just write it myself. This is not important:

Then we need to perform some mailbox verification. I use jQuery, Ajax, And the favorite layer plug-in for mailbox verification and data transmission.

1 $ ('# two input '). eq (0 ). blur (function () {2 var preg =/^ \ w + ([-+.] \ w +) * @ \ w + ([-.] \ w + )*\. \ w + ([-.] \ w +) */; 3 if ($ (this ). val (). length = 0) {// the email address 4 layer is not specified. msg ('mailbox cannot be blank ') 5} else if (! Preg. test ($ (this ). val () {// verify the mailbox format 6 layer. msg ('mailbox format incorrect ') 7} else {// obtain the user-filled email address through Ajax asynchronous request Server 8 var email = $ (' # two input '). eq (0 ). val (); 9 $ ("# sendEmail "). attr ("disabled", "disabled" 2.16.val('submit..'mirror.css ("cursor", "default"); 10 $. post ("<? Php echo base_url ('findpwdcontroller/EmailVerify ')?> ", {Mail: email}, function (msg) {11 if (msg =" noreg ") {// if 'noreg '12 layer is returned. msg ('this email has not been registered ') 13 $ ("# sendEmail "). removeAttr ("disabled "). val ('submit 'character .css ("cursor", "pointer "); 14} else {15 $ ("# demo" ).html ("

The above jQuery code is not very difficult. I believe that anyone who has learned jQuery can understand it. Next, the controller receives requests to process data, loads the mailer class, and calls the sendMail method, the Code is as follows:

1 // email verification 2 public function EmailVerify () {3 $ email = $ this-> input-> post ('mail '); // receives the parameter 4 $ this-> db-> select ('Member _ id, member_name, member_pwd ') passed by Ajax '); // perform email verification 5 $ SQL = $ this-> db-> get_where ('ecshop _ member ', "member_email =' $ email '")-> row_array (); 6 $ id = $ SQL ['Member _ id']; 7 if (! $ Id) {// This email address has not been registered! 8 echo 'noreg '; 9 exit; 10} else {11 $ getpasstime = time (); // obtain the current time 12 $ uid = $ SQL ['Member _ id']; // user id13 $ token = md5 ($ uid. $ SQL ['Member _ name']. $ SQL ['Member _ pwd']); // combined Verification Code 14 $ smtpemailto = $ email; // recipient mailbox 15 $ url = "http://www.msku.com/index.php/Home/Email/resetPwd? Email = ". $ email. "16 & token = ". $ token; // construct the URL17 $ time = date ('Y-m-d H: I ') for resetting the password address '); // construction time 18 $ emailsubject = "www.ci.com-retrieve password"; // email subject 19 $ emailbody = "dear ". $ email. ": <br/> you are in ". $ time. "A password retrieval request is submitted. Click the link below to reset the password 20 (the button is valid within 24 hours ). <Br/> <a href = '". $ url. "'target = '_ blank'> ". $ url. "</a>"; // mail content 21 // load the Mailer class and call the sendMail method to pass the parameter 22 $ this-> load-> library ('mailer '); 23 $ this-> mailer-> sendMail ($ emailsubject, $ emailbody, $ smtpemailto); 24 // data update time 25 $ addtime ['Member _ addtime'] = $ getpasstime; 26 $ this-> db-> update ('member', $ addtime, "member_id = $ uid"); 27} 28}

Based on the three parameters in the preceding mailer class, the preceding parameters are almost ready. $ Emailsubject refers to the subject of the sent email, and $ emailbody refers to the email content here. The content I write here is the link for the user to reset the password of the email, $ smtpmailto refers to the email address of the recipient, which is the email account entered during user registration. Try again. The email is successfully received:

The function of sending an email to retrieve the password using the CI framework has been implemented. As for resetting the password, we will not demonstrate the function. The above content will be helpful to colleagues who have problems in this regard,

This article is original content. For the purpose of respecting others' work, please enter the address of this Article for reprinting:

Http://www.cnblogs.com/luokakale/p/7249016.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.