This article describes how to use phpmailer to send emails in thinkphp. if you need PHPMailer, refer to ThinkPHP 2.1 and phpmailer 5.1. (The latter is recommended to be downloaded directly from this blog, because we cannot ensure that the following code runs properly in all versions of PHPMailer)
The procedure is as follows:
Step 1: Add the PHPMailer class library
Click here to download
Decompress the downloaded file and move the PHPMail directory to the Vendor directory in ThinkPHP. (Make sure that the class. phpmailer. php file is in ThinkPHPVendorPHPMailerclass. phpmailer. php)
Step 2: add the email sending function
Add the following code to the Common. php file in the common folder in the project directory (if not, create one:
The code is as follows:
Step 3: configure email information
* Send an email *
Function SendMail ($ address, $ title, $ message)
Vendor ('phpmailer. class # PHPMailer ');
$ Mail = new PHPMailer ();
// Set PHPMailer to use the SMTP server to send an Email
$ Mail-> IsSMTP ();
// Set the character encoding of the email. If this parameter is not specified, it is 'utf-8'
$ Mail-> CharSet = 'utf-8 ';
// Add recipient addresses, which can be used multiple times to add multiple recipients
$ Mail-> AddAddress ($ address );
// Set the email body
$ Mail-> Body = $ message;
// Set the From Field of the mail header.
$ Mail-> From = C ('mail _ address ');
// Set the sender name
$ Mail-> FromName = 'lilyrecruit ';
// Set the Mail title
$ Mail-> Subject = $ title;
// Set the SMTP server.
$ Mail-> Host = C ('mail _ SMTP ');
// Set it to "verification required"
$ Mail-> SMTPAuth = true;
// Set the user name and password.
$ Mail-> Username = C ('mail _ loginname ');
$ Mail-> Password = C ('mail _ password ');
// Send an email.
Return ($ mail-> Send ());
Edit config. php in the Conf directory and add the following content in return array.
The code is as follows:
'Mail _ address' => 'XXX @ 126.com ', // email ADDRESS
'Mail _ SMTP '=> 'smtp .126.com', // MAIL smtp server
'Mail _ loginname' => 'XXX', // email logon account
'Mail _ password' => 'XXX', // email PASSWORD
The email login account may need to include the content following @. please try it yourself ^_^
Step 4: send an email in Action
Because ThinkPHP automatically loads functions in common. php, you only need to use the following code to send emails.
SendMail ("firstname.lastname@example.org", "Mail title", "Mail body ");
This tutorial is complete. Sahua ~~~
Welcome to the successful configuration of SendMail ("email@example.com", "I will also use ThinkPHP to send mail", "Wow ~~ ");
Some other notes
What if a line break is required in the mail body? In fact, the simplest way is --
SendMail ("firstname.lastname@example.org", "Mail title", "Mail body
I have a new line ~~! ");
Both QQ mail (including foxmail) and NetEase's 126 and 163 tests were successful. It is difficult to access GMail recently, so it is not tested. I heard that GMail requires SSL. if you need it, Google Baidu PHPMailer.