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:
config.php
‘MAIL_HOST‘ => ‘邮件发送SMTP服务器‘ , |
‘MAIL_USERNAME‘ => ‘SMTP服务器登陆用户名‘ , |
‘MAIL_PASSWORD‘ => ‘SMTP服务器登陆密码‘ , |
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
function sendMail( $to , $subject , $content ) { |
vendor( ‘PHPMailer.class#PHPMailer‘ ); |
$mail ->Host = C( ‘MAIL_HOST‘ ); |
$mail ->SMTPAuth = C( ‘MAIL_SMTPAUTH‘ ); |
$mail ->Username = C( ‘MAIL_USERNAME‘ ); |
$mail ->Password = C( ‘MAIL_PASSWORD‘ ); |
$mail ->SMTPSecure = C( ‘MAIL_SECURE‘ ); |
$mail ->CharSet = C( ‘MAIL_CHARSET‘ ); |
$mail ->From = C( ‘MAIL_USERNAME‘ ); |
$mail ->FromName = ‘憨豆儿笑园‘ ; |
$mail ->IsHTML(C( ‘MAIL_ISHTML‘ )); |
$mail ->Subject = $subject ; |
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: http://www.xcoder.cn/index.php/archives/889), 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
vendor(
‘PHPMailer.class#PHPMailer‘
);
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:
library/vendor/phpmailer/class.phpmailer.php
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!