ThinkPHP5 encapsulates the mail sending service (attachments can be sent), and thinkphp5 mails can be sent

Source: Internet
Author: User
Tags mail code

ThinkPHP5 encapsulates the mail sending service (attachments can be sent), and thinkphp5 mails can be sent
1. Install phpmailer in Composer

composer require phpmailer/phpmailer

 

2. encapsulate the mail service class in ThinkPHP

I encapsulated it in the extended directory extend/Mail. php file with the following content:

<? Php/*** Mail service class */class mail extends \ PHPMailer {function _ construct () {date_default_timezone_set ('prc'); $ this-> CharSet = config ('mail. charset '); $ this-> isSMTP (); $ this-> SMTPDebug = config ('mail. smtp_debug '); $ this-> Debugoutput = config ('mail. debug_output '); $ this-> Host = config ('mail. host '); $ this-> Port = config ('mail. port '); $ this-> SMTPAuth = config ('mail. smtp_auth '); $ this-> SMTPSecure = config (' Mail. smtp_secure '); $ this-> Username = config ('mail. username'); $ this-> Password = config ('mail. password '); $ this-> setFrom (config ('mail. from '), config ('mail. from_name '); $ this-> addReplyTo (config ('mail. reply_to '), config ('mail. reply_to_name '));} /*** send email ** @ param [type] $ toMail recipient address * @ param [type] $ toName recipient name * @ param [type] $ subject * @ param [type] $ content, html * @ param [type] $ Attachment List. Array of file paths or paths * @ return [type] returns true if the file is successful. If the file fails, an error message */function sendMail ($ toMail, $ toName, $ subject, $ content, $ attachment = null) {$ this-> addAddress ($ toMail, $ toName); $ this-> Subject = $ subject; $ this-> msgHTML ($ content ); if ($ attachment) {// Add an attachment if (! Is_array ($ attachment) {is_file ($ attachment) & $ this-> AddAttachment ($ attachment);} else {foreach ($ attachment as $ file) {is_file ($ file) & $ this-> AddAttachment ($ file) ;}} if (! $ This-> send () {// send return $ this-> ErrorInfo;} else {return true ;}}}

  

NOTE: If an attachment is sent, we recommend that you use an English path. The Chinese path may cause the attachment to fail to be sent, and the received email has no attachment.

The preceding configuration parameters are stored in the application/extra/mail. php Extension Configuration directory. The content is as follows:

<? Php/*** mail service configuration */return ['charset' => 'utf-8', // Mail Code 'smtp _ debug' => 0, // debug mode. 0: Close, 1: client message, 2: client and server message, 3: 2 and connection status, 4: More detailed 'debug _ output' => 'html ', // debug output type. 'Host' => 'smtp .126.com ', // SMPT server address 'Port' => 465, // port number 'smtp _ auth' => true, // enable SMTP authentication 'smtp _ secure '=> 'ssl', // enable the security protocol 'username' => 'yourname @ example.com ', // SMTP username 'Password' => 'yourpassword', // SMTP password. 126 the client Authorization code is used in the mailbox, and the independent password 'from' => 'from @ example.com 'is used in the QQ mailbox. // the sender's mailbox 'from _ name' => 'name ', // sender name 'reply _ to' => '', // the address of the reply email address. Leave blank the sender's mailbox 'reply _ to_name '=> '', // name. Leave blank the sender name];

Note: The default port is 25. If the ssl protocol is used, the port number is generally 465 or 587. For example, 126 mailbox.

More configuration parameters, you can look at the source code: https://github.com/PHPMailer/PHPMailer/blob/master/class.phpmailer.php

 

3. Test

In the Controller method, add the test code:

Public function mail () {$ mail = new \ Mail; $ OK = $ mail-> sendMail ('xxxxxxxxx @ qq.com ', 'mingc', 'mail ', '<p style = "color: # f60; font-weight: 700;"> congratulations, the email is successful! </P> ', 'c:/Users/Administrator/Desktop/body.bmp'); var_dump ($ OK );}

  

Here I use the 126 mailbox, the security protocol ssl, the port number 465, to send html content, the test is successful:

 

Reference link:

Phpmail STMP mail instance

 

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.