Integrate Phpmailer into thinkphp 3.2 for SMTP send mail

Source: Internet
Author: User

This content reproduced source:, and made a change.

thinkphp no mail send function, so, I think about, will phpmailer integration into thinkphp it.

Phpmailer is not in line with the thinkphp specification plug-in program, so we need to first put Phpmailer program into thinkphp Library/vendor directory, I here is the latest version of Thinkphp 3.2, if it is thinkphp Before the 3.2 version, the Lib directory may be. The vendor directory is designed to store non-standard thinkphp plugins in the following directories:

Phpmailer integrated into the thinkphp storage directory

Next in order to facilitate the sending of mail, we can define a special send mail function SendMail (), this function can only accept the most basic parameters sent to the mail, to which mailbox $id, the subject of the message $subject, the contents of the message $content. However, in order to facilitate us to modify the SMTP mail sending server, we first define the mail sending server in the config.php file, the content is as follows:


1<?PHP2 return Array(   3     //Configuring the Mail sending server4' Mail_smtp ' =TRUE,5' Mail_host ' + ' Mail send SMTP server ',6' Mail_smtpauth ' =TRUE,7' Mail_username ' = ' SMTP server login username ',8' Mail_password ' = ' SMTP server login password ',9' Mail_secure ' = ' TLS ',//Note This feature requires SSL feature support in the php.ini file (new modifications)Ten' Mail_charset ' = ' utf-8 ', One' Mail_ishtml ' =TRUE, A ); -?>

The specific configuration can be added according to your needs. It is possible to define it by the rules of Phpmailer, and then use the large C (' Conf_name ') method in the definition function to invoke it.

Next, you need to define the mail Send function SendMail () in the function.php file under the Common folder, and if the function.php file does not exist, create a new one. And the methods in this function.php file can be used globally without first introducing the function.php file.

When Phpmailer is needed in a method, it is not implemented by the Import keyword, but by the public method vendor () method. The specific contents are as follows:

View Source

1<?PHP2 /**3 * Mail Send function4  */5 functionSendMail ($to,$subject,$content) {6Vendor (' Phpmailer.class#phpmailer ');7     $mail=NewPhpmailer ();8     //assemble the mail server9     if(C (' Mail_smtp ')) {Ten         $mail-issmtp (); One     } A     $mail->host = C (' Mail_host '); -     $mail->smtpauth = C (' Mail_smtpauth '); -     $mail->username = C (' Mail_username '); the     $mail->password = C (' Mail_password '); -     $mail->smtpsecure = C (' mail_secure '); -     $mail->charset = C (' Mail_charset '); -     //Assemble message header information +     $mail->from = C (' Mail_username '); -     $mail->addaddress ($to); +     $mail->fromname = ' Silly bean laugh garden '; A     $mail->ishtml (C (' mail_ishtml ')); at     //Assemble message body information -     $mail->subject =$subject; -     $mail->body =$content; -     //Send mail -     if(!$mail-Send ()) { -         return FALSE; in}Else { -         return TRUE; to     } + } -?>

At this point, we have integrated the Phpmailer into the thinkphp, and now we can use the SendMail () method anywhere in the program, no need to introduce function.php file, because thinkphp will automatically find it in this file.

In-depth study of the vendor () method in thinkphp 3.2 and the correct extension of Phpmailer

When we want to introduce third-party extensions in thinkphp, and third-party extensions are not written in accordance with thinkphp specifications, we need to place the third-party extensions in the Library/vendor directory, of course, for thinkphp 3.2, The lower version is based on the situation.

Then, when you need to use a third-party extension in a controller or function, you can use the vendor () method to reference it directly.

But when I put Phpmailer in the vendor directory (specifically, you can refer to a previous blog:, in this machine run well, recently uploaded the program to the server, Direct prompt

Class ' Phpmailer ' not found

Then run again on this machine, or correct! Through the front of this blog can know that I was through


This line of code will be introduced Phpmailer. Now that you are prompted not to find the Phpmailer class, the description is not introduced correctly. What is this for? Just a cursory look at the source of the vendor () method, it is found that the vendor () method is the import () method is a parameter assembly, and then handed over to the import () method processing.

The source of the import () method is also found, in the import () method, for the above parameter parsing is actually will '. ' Replace with '/', replace ' # ' with '. ', BaseURL is automatically replenished by the vendor () method, pointing to the vendor directory. So the parameters in the vendor () method above are eventually parsed into the following directory:

Library/vendor/phpmailer/class. phpmailer.php

The actual directory address for the Phpmailer portal file is:


The content is the same! However, I use a Linux server, so the case is strictly differentiated, so of course, can not successfully import this class. The solution is to change the introduction of vendor () to:

Vendor (' Phpmailer.class#phpmailer ')

In addition, for the use of Phpmailer also pay attention to, Phpmailer if the SMTP way to send mail, need PHP to fsockopen support, so we need to modify php.ini Disable_ Functions The Fscokopen is removed or a run error occurs:

Fsockopen () has been disabled

The ErrorInfo property can be obtained by Phpmailer!

Consolidate Phpmailer into thinkphp 3.2 for SMTP send mail

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.