ThinkPHP uses PHPMailer to send emails

Source: Internet
Author: User
Tags php file

This article uses 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: Copy code

<? Php
/**********
* 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 ());
}

?>


Step 3: configure email information

Edit config. php in the Conf directory and add the following content in return array.

The code is as follows: Copy code

'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.

The code is as follows: Copy code

SendMail ("xxx@xxx.com", "Mail title", "Mail body ");

This tutorial is complete. Sahua ~~~

Welcome to the successful configuration of SendMail ("dreamrunner@foxmail.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 ("xxx@xxx.com", "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.

 

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.