ThinkPHP3.2 sending mail using Phpmailer

Source: Internet
Author: User

First download Phpmailer, directly in Baidu Search, and then download just fine.
Then register a Sina mailbox as the sender's mailbox. QQ and 163 mailbox are not stable, and sometimes mail can not be sent out.

Unzip the downloaded file (if it is a compressed package) and get the Phpmailer folder with the following files in the folder:



The file folder is then placed in the thinkphp Library in the Vendor, the specific path is: Project Folders \thinkphp\library\vendor\,



Here, Phpmailer even joined the Thinkphp3.2.

How to use:

Open application\common\common\function.php (if you do not have this file, create a new one):

Add the following code:

/* Send a verification mailbox
<span style= "White-space:pre" ></span> $to to the recipient's email address
The <span style= "White-space:pre" ></span>c () function means that config.php */function ($to) {sendMail (') is obtained from the configuration file//vendor      Phpmailer.phpmailerautoload ');    Vendor (' Phpmailer.class#phpmailer ');    Vendor (' phpmailer.class#smtp '); $mail = new Phpmailer (); Instantiate $mail->issmtp (); Enable SMTP $mail->host=c (' mail_host '); The name of the SMTP server (in this case, Sina mailbox) $mail->smtpauth = C (' Mail_smtpauth '); Enable SMTP authentication $mail->username = C (' Mail_username '); Sender mailbox name, obtained from config.php $mail->password = C (' Mail_password '); Sender Mailbox Password $mail->from = C (' Mail_from '); The sender's address (that is, your email address) $mail->fromname = C (' Mail_fromname ');    Sender's name $mail->addaddress ($to, "Dear Customer"); $mail->wordwrap = 50;    Sets the length of characters per line $mail->port = 25; $mail->ishtml (C (' mail_ishtml ')); Whether HTML-formatted mail $mail->charset=c (' Mail_charset '); Set the message encoding $mail->subject = ' HelloWorld '; Message subject $mail->body = ' This email is sent by phpmailer! '; Message content $mail->altbody = "This is a plain text body in a non-profit HTML email client"; The message body does notSupport for alternate display of HTML $result = $mail->send (); return $result;}

Save and exit.

Please register

Then open application\common\conf\config.php and add the following code:

<?phpreturn Array (    ' mail_host ' = ' smtp.sina.com ',//SMTP server name, here is Sina Mail, qq:smtp.qq.com, 163: smtp.163.com    ' Mail_smtpauth ' =>true,//Enable SMTP authentication    ' mail_username ' = ' [email protected]om ',//sender's mailbox name, Pay attention to your registered Sina email address    ' mail_from ' + ' [email protected] ',//Sender email address, pay attention to your registered Sina email address    ' mail_fromname ' and ' Liujan ',//Sender's name    ' Mail_password ' * * * * * * * * * * *, please fill in the sender's email password    ' mail_charset ' + ' utf-8 ',//Set message encoding    ' Mail_ishtml ' =>true,//whether HTML format mail);? >

Save and exit.

Then open the Indexcontroller under the home,

Add function SendMail:

Public Function SendMail () {$email = ' [email protected] ';  Recipient's email address sendmail ($email);}
Save and exit.

Enter the address in the browser:

Http://localhost/thinkphp/Home/Index/sendMail

If your recipient's email address is valid and the sender's email address and password are correct, you should be able to send the message normally. Take a look at your email inbox.



If you want to use Phpmailer to send the activation mailbox when registering an account, you can add three fields to the user table in the database, Reg_time (registration time), status (0 means inactive, 1 is active), Verify_code (indicates the activation code)

Then the user registers, with its input user mailbox, password plus the current time using MD5 to generate an activation code Verify_code:

$data [' verify_code '] = MD5 ($data [' User_email ']. $data [' user_pwd ']. $data [' reg_time ']);

The Send activation link is then entered to the user.

Then the user input data, registration time, generated activation code, write to the database, note that the status of 0

The content of the activation link is mainly the activation code generated just now, and the handler function when the user clicks on the activation link.

For example, my activation link sample is:

Http://localhost/mr.pan/?m=home&c=user&a=active& verify_code=687ae065b161d6968086ef08af3a2c0e

Then put

I wrote a function active in the Usercontroller under the home module, mainly to determine whether the activation Link Activation code is the same as in the database, if the same, activate the account, otherwise activation fails, and the link is only valid within 24 hours, the function is as follows:

Public Function Active () {if (is_get) {$verify _code = I ("Verify_code"), $user = D ("user"), $data = $user->where ("Verify_ Code= ' $verify _code ' ")->find ();  Find the same record as Verify_code in the Verify_code and activation links in the database if (!empty ($data)) {$user _id = $data [' user_id '];if ($data [' status '] = = 0) {$current _time = date ("y-m-d h:i:s", Strtotime ("1 day");  Get 24 hours before the time if ($data [' Reg_time '] < $current _time) {  //determine if the echo  "Your activation has expired in 24 hours, please re-register to send activation email"; $user- >where ("user_id= ' $user _id '")->delete (); return false;} else{$data [' status '] = 1;  Activation succeeds if ($user->where ("user_id= ' $user _id '")->save ($data)) {return true;} Else{echo "Activation failed, please re-register"; $user->where ("user_id= ' $user _id '")->delete (); return false;}} else{return true;}} Else{echo "Activation failed, please re-register"; return false;}}




Common errors:

1. CLASS Phpmailer not found:

Workaround:

Enter vendor, open the class.phpmailer.php file, copy the class name, and then change the Phpmailer in the class.phpmailer.php file name to the one you just copied. The class# class name Phpmailer in the vendor in the function.php file is also
Changed to just copied.


2. Phpmailer Error SMTP Error:could not connect to SMTP host

Workaround:

Open vendor under class.smtp.php, find $this->smtp_conn = @fsockopen ($host,//The host of the server
Then change to $this->smtp_conn = Stream_socket_client ("tcp://". $host. ":". $port, $errno, $errstr, $tval);
Open class.phpmailer.php, find function issmtp (), Change $this->mailer = ' smtp ' to $this->mailer = ' smtp ',
Change case ' SMTP ' to case ' SMTP ' in 572-579 rows at the same time


3. A case where the $mail->send () return value has been 1 (true)

Workaround:

Please put the class.phpmailer.php in the 第572-579 line
Case ' SMTP ' changed to Case ' SMTP '
When it fails to pass the verification, please set the sender's username and from to your mailbox (preferably with Sina email, QQ and 163 Sometimes this error occurs)


ThinkPHP3.2 sending mail using Phpmailer

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.