Phpmailer mailbox binding implementation method, phpmailer mailbox binding
This example describes how to bind a mailbox to phpmailer. We will share this with you for your reference. The details are as follows:
The effect is as follows:
1. Configuration
<? Phpreturn array ('email _ host' => 'smtp .aliyun.com ', 'email _ port' => '25', 'email _ username' => 'diandodo @ aliyun.com ', 'email _ password' => 'xxxx', 'email _ from' => 'diandodo @ aliyun.com ', 'email _ fromname' => 'click duoduo ', 'email _ subobject' => 'alimail activation mailbox for store providers', 'email _ body' => "Dear user {$ username: your activation code is <font color = 'red' >{$ code} </font>. Enter the activation code for verification! The activation code is valid for 6 minutes ^_^ ",);
2. Sending Function
// Send the email private function _ sendEmail ($ email, $ code, $ username = '') {import ('@. ORG. phpmailer '); $ mail = new PHPMailer (); // create a mail sending class, the class name is not necessarily the same as the introduced file name $ mail-> CharSet = "UTF-8 "; $ mail-> IsSMTP (); // use SMTP to send $ mail-> Host = C ('email _ host '); // your enterprise Post Office domain name $ mail-> SMTPAuth = true; // enable the SMTP verification function $ mail-> Username = C ('email _ username '); // Post Office username (enter the complete email Address) $ mail-> Password = C ('email _ password'); // post office password $ mail-> Por T = C ('email _ port'); $ mail-> From = C ('email _ from '); // sender's email address $ mail-> FromName = C ('email _ fromname'); $ mail-> AddAddress ("$ email", "$ username "); $ mail-> IsHTML (true); // set email format to HTML // whether to use HTML format $ mail-> Subject = C ('email _ subobject '); // mail title $ email_body = "Dear user <strong >{$ username} </strong>: your activation code is <font color = 'red' >{$ code} </font>. Enter the activation code for verification! The activation code is valid for 6 minutes; $ mail-> Body = $ email_body; // The email content. if HTML is set above, it can be HTML if (! $ Mail-> Send () {return array ('status' => 2, 'info' => $ mail-> ErrorInfo );} else {return array ('status' => 1, 'info' => 'sent successfully ');;}}
3. Generate a verification code and save it to the session and send it
// Send the email activation code public function sendActivationcode () {session ($ this-> activationtime, null); $ activationtime = session ($ this-> activationtime ); $ email = $ this-> _ post ('email ', 'trim'); if (IS_AJAX &&(! $ Activationtime | time ()> $ activationtime) {$ activationcode = rand (1000,999 9); $ res = $ this-> _ sendEmail ($ email, $ activationcode, $ this-> user ['username']); if ($ res ['status'] = 1) {// sets the sending limit time session ($ this-> activationtime, time () + 50); session ($ this-> activationcode, array ('code' => $ activationcode, 'time' => time () + 600 )); $ this-> ajaxReturn (array ('result' => true);} else {// write log File $ log = date ('Y-m-d H: i: s '). "failed to send: {$ res ['info']}". PHP_EOL; file_put_contents (RUNTIME_PATH. 'Log/activationcode. log', $ log, FILE_APPEND); $ this-> ajaxReturn (array ('result' => false, 'error' => $ res ['info']); }} else {$ this-> ajaxReturn (array ('result' => false, 'error' => 'invalid request '));}}
4. Verify and bind
// Bind the public function bind_email () {if (IS_POST) {// obtain the verification code $ activationcode = $ this-> _ post ('activationcode', 'trim '); $ email = $ this-> _ post ('email ', 'trim'); $ session_activationcode = session ($ this-> activationcode); if (time ()> $ session_activationcode ['time'] | $ activationcode! = $ Session_activationcode ['code']) {$ this-> error ('verification code incorrect ');} else {M ('user ') -> where (array ('id' => $ this-> user ['id'])-> save (array ('email '=> $ email )); $ this-> success ('binding successfully', U ('account/my') ;}} else {$ this-> display ();}}
Summary:
1. This is similar to sending a mobile phone verification code.
2. The difference is that one is to send text messages and the other is to send emails.
3. the sender is Alibaba big fish, and the sender is the company's application email.
4. You are charged for sending text messages. emails are free of charge.