Integrate Phpmailer into thinkphp to implement SMTP send mail

Source: Internet
Author: User


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

<?php
returnarray(   
    // 配置邮件发送服务器
    ‘MAIL_SMTP‘=>TRUE,
    ‘MAIL_HOST‘=>‘邮件发送SMTP服务器‘,
    ‘MAIL_SMTPAUTH‘=>TRUE,
    ‘MAIL_USERNAME‘=>‘SMTP服务器登陆用户名‘,
    ‘MAIL_PASSWORD‘=>‘SMTP服务器登陆密码‘,
    ‘MAIL_SECURE‘=>‘tls‘,
    ‘MAIL_CHARSET‘=>‘utf-8‘,
    ‘MAIL_ISHTML‘=>TRUE,
);
?>

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

<?php
/**
 * 邮件发送函数
 */
functionsendMail($to, $subject, $content) {
    vendor(‘PHPMailer.class#PHPMailer‘);
    $mail= newPHPMailer();
    // 装配邮件服务器
    if(C(‘MAIL_SMTP‘)) {
        $mail->IsSMTP();
    }
    $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->AddAddress($to);
    $mail->FromName = ‘憨豆儿笑园‘;
    $mail->IsHTML(C(‘MAIL_ISHTML‘));
    // 装配邮件正文信息
    $mail->Subject = $subject;
    $mail->Body = $content;
    // 发送邮件
    if(!$mail->Send()) {
        returnFALSE;
    } else{
        returnTRUE;
    }
}
?>

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!


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.