ThinkPHP uses PHPMailer to implement email sending implementation code_PHP tutorial

WBOY
Release: 2016-07-21 16:12:53
Original
932 people have browsed it

This article uses ThinkPHP version 2.1 and PHPMailer version 5.1. (The latter is recommended that you download it directly from this blog, because we cannot guarantee that the following code will run normally in all versions of PHPMailer)

Here are the specific steps:

The first step is to add the PHPMailer class library

Click here to download
Unzip the downloaded file and move the PHPMail directory to the Vendor in the ThinkPHP directory. (Please make sure the class.phpmailer.php file is in ThinkPHPVendorPHPMailerclass.phpmailer.php)

The second step is to add the send email function

Add the following code to the common.php file in the Common folder in the project directory (please create it if it does not exist):

Copy the code The code is as follows:

/**********
* Send email *
**********/
function SendMail($address,$title,$message)
{
vendor('PHPMailer .class#PHPMailer');
$mail=new PHPMailer();
// Set up PHPMailer to use the SMTP server to send emails
$mail->IsSMTP();
// Set up the email Character encoding, if not specified, it will be 'UTF-8'
$mail->CharSet='UTF-8';
// Add recipient address, can be used multiple times to add multiple recipients To person
$mail->AddAddress($address);
// Set the email body
$mail->Body=$message;
// Set the From field of the email header.
$mail->From=C('MAIL_ADDRESS');
// Set the sender name
$mail->FromName='LilyRecruit';
// Set the email title
$mail->Subject=$title;
// Set up SMTP server.
$mail->Host=C('MAIL_SMTP');
// Set to "Require verification"
$mail->SMTPAuth=true;
// Set username and password.
$mail->Username=C('MAIL_LOGINNAME');
$mail->Password=C('MAIL_PASSWORD');
// Send email.
return($mail->Send());
}
?>


The third step, configure email information

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

Copy code The code is as follows:

'MAIL_ADDRESS'=>'xxx@126.com', // Email address
'MAIL_SMTP'=>'smtp.126.com', // Email SMTP server
'MAIL_LOGINNAME'=>'xxx', // Email login account
'MAIL_PASSWORD'=>' xxx', // Email password

The email login account may need to include the content after @, please try it yourself^_^
The fourth step is to send the email in Action
Because ThinkPHP will automatically load the function in common.php, so When you need to send an email, just use the following code.
SendMail("xxx@xxx.com","Email title","Email text");

At this point, this tutorial has ended. Sprinkle flowers~~~

Welcome students who have successfully configured SendMail("dreamrunner@foxmail.com","I can also use ThinkPHP to send emails","Wow wow wow~~");

A few more instructions

What should I do if I need to break the line in the email body? In fact, the simplest way is——

SendMail("xxx@xxx.com","Email title","Email text
I changed the line~~!");

QQ mailboxes (including foxmail) and NetEase’s 126 and 163 were tested successfully. Because it has been difficult to access GMail recently, I did not test it. I heard that GMail requires the use of SSL. Students who need it can Google Baidu PHPMailer on their own.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/313571.htmlTechArticleThis article uses ThinkPHP version 2.1 and PHPMailer version 5.1. (The latter is recommended that you download it directly from this blog, because we cannot guarantee that the following code will work properly in all versions of PHPMailer...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!