This article mainly introduces the implementation of phpmailer mailbox binding, and analyzes in detail the configuration, function implementation, and related operation skills of phpmailer mailbox binding in combination with the instance form, for more information about how to bind a mailbox to phpmailer, see the example in this article. We will share this with you for your reference. The details are as follows:
The effect is as follows:
1. configuration
'Smtp .aliyun.com ', 'Email _ port' => '25', 'Email _ username' => 'diandodo @ aliyun.com', 'Email _ password' => 'xxxxxx ', '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 {$ code}. 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-> Port = 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{$ Username}Hello, your activation code is {$ code}. please 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.
I hope this article will help you with PHP programming.
For more information about how to bind phpmailer to an email address, see PHP!