Integrate the PHP mail sending function into TP3.2.3

Source: Internet
Author: User
Provides various official and user-released code examples. For code reference, you are welcome to exchange and learn to write this article. I am hesitant to think about it, because several of my predecessors have previously written similar articles, it is also well written, but it is not clear enough for beginners to understand the text. I have dug this hole yesterday and I still have to fill it out. Next I will write my usage.

When I was working on a customer's website, a netizen submitted a consultation on the website and sent a reminder email to the customer's company mailbox. I studied it a little. My sharing is simple, but it is always usable.

1. A PHPMailer plug-in is down on the Internet, with the plug-in address >>. after downloading and unzipping, we only need to use two of the files, as shown in:

Place the class. phpmailer. php and class. smtp. php files
ThinkPHP/Library/Vendor/PHPMailer/class. phpmailer. php (case sensitive)
ThinkPHP/Library/Vendor/PHPMailer/class. smtp. php

Note: The default third-party class library directory of thinkPHP is placed. php defines define ('vendor _ path', APP_PATH. 'Common/Vendor/'); the path of the file must be the same, so that the class 'phpmailer' not found is avoided.

2. Create the User-Defined function file Application/Home/Common/function. php and place the following functions: /**
* Function: email sending Function
* @ Param string $ to target email address
* @ Param string $ subject (title)
* @ Param string $ to email content
* @ Return bool true
*/
Function sendMail ($ to, $ subject, $ content ){
Vendor ('phpmailer. class # smtp ');
Vendor ('phpmailer. class # phpmailer '); // note the case here. Otherwise, the class cannot be found. PHPMailer is the folder name, and class # phpmailer represents the class. phpmailer. php file name
$ Mail = new PHPMailer ();
// Assemble the email server
If (C ('mail _ SMTP ')){
$ Mail-> IsSMTP ();
}
$ Mail-> Host = C ('mail _ host'); // for parameter explanations, see the following configuration information annotations.
$ 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 ');
// Assemble the mail header information
$ Mail-> From = C ('mail _ username ');
$ Mail-> AddAddress ($ );
$ Mail-> FromName = C ('mail _ fromname ');
$ Mail-> IsHTML (C ('mail _ ISHTML '));
// Assemble the body of the email
$ Mail-> Subject = $ subject;
$ Mail-> Body = $ content;
// Send an email
If (! $ Mail-> Send ()){
Return FALSE;
} Else {
Return TRUE;
}
}
3. The C method is used in the above functions to load some configuration information, so we have to go to the configuration file (default/Application/Home/Conf/config. php) Add the following configuration information: Return array (
// Other configuration items are omitted ......

// Configure the email sending server
'Mail _ SMTP '=> TRUE,
'Mail _ host' => 'smtp .163.com ', // MAIL sending smtp Server
'Mail _ SMTPAUTH '=> TRUE,
'Mail _ username' => 'vsir ** @ 163.com ', // SMTP Server login USERNAME
'Mail _ password' => '123456abc', // SMTP server logon PASSWORD
'Mail _ SECURE '=> 'tls ',
'Mail _ charset' => 'utf-8 ',
'Mail _ ISHTML '=> TRUE,
'Mail _ fromname' => 'xx website customer ',
);
Appendix: Common Mail Server (receiving server and sending mail server) addresses. The information here may not be practical enough. You can find the configuration answer by Baidu, such as port 163 or server 163 settings.

4. Start calling. Assume that the URL /? M = home & c = index & a = send access, then we add the following method to the Application/Home/Controller/IndexController. class. php file: Namespace Home \ Controller;
Use Think \ Controller;
Class IndexController extends Controller {
Public function index (){

}
Public function send (){
If (sendMail ('vsiryxm @ qq.com ', 'Hello! Email Subject ',' This is the body of the test email! ')){
Echo 'sent successfully! ';
}
Else {
Echo 'failed to send! ';
}
}
}
5. I am at a limited level and may not have the expected functions (such as CC, BCC, and attachments), but the basic mail sending function has been implemented. You can use other functions of this plug-in on your own.

6. Other recommendations on this website:
Http://www.thinkphp1.cn/code/32.html
// The phpmail in the previous article cannot be opened. You can click here to download the plug-in address>
Http://www.thinkphp1.cn/code/989.html

7. Attachment package download:

Upload.zip (41.96 KB download: 75 times)

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

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.