thinkphp Implementation Code _php tutorial using phpmailer for mail delivery

Source: Internet
Author: User
This article uses thinkphp version 2.1 and Phpmailer 5.1. (The latter recommends that you download it directly from this blog, as we cannot guarantee that the following code will work in all versions of Phpmailer)

Here are the specific steps:

First step, add Phpmailer class Library

Click here to download
Unzip the downloaded file and move the Phpmail directory to the vendor within the thinkphp directory. (Make sure the class.phpmailer.php file is ThinkPHPVendorPHPMailerclass.phpmailer.php)

Step two, add the Send mail function

Add the following code to the common.php file (if not created) in the common folder in the project directory:
Copy CodeThe code is as follows:
/**********
* Send mail *
**********/
function SendMail ($address, $title, $message)
{
Vendor (' Phpmailer.class#phpmailer ');
$mail =new Phpmailer ();
Set Phpmailer to send email using an SMTP server
$mail->issmtp ();
Set the character encoding of the message, or ' UTF-8 ' if not specified
$mail->charset= ' UTF-8 ';
Add a recipient address that can be used multiple times to add multiple recipients
$mail->addaddress ($address);
Set the message body
$mail->body= $message;
Sets the From field of the message header.
$mail->from=c (' mail_address ');
Set Sender Name
$mail->fromname= ' lilyrecruit ';
Set the message header
$mail->subject= $title;
Set up the SMTP server.
$mail->host=c (' mail_smtp ');
Set to "Require verification"
$mail->smtpauth=true;
Set the user name and password.
$mail->username=c (' mail_loginname ');
$mail->password=c (' Mail_password ');
Send the message.
Return ($mail->send ());
}
?>


Step Three, configure mailbox information

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

Copy the Code code as follows:
' mail_address ' = ' xxx@126.com ',//email address
' Mail_smtp ' = ' smtp.126.com ',//mailbox SMTP Server
' Mail_loginname ' = ' xxx ',//Email login account
' Mail_password ' = ' xxx ',//email password

The mailbox login account may need to include the content after the @, please try it yourself ^_^
Fourth step, send mail in action
Since thinkphp will automatically load functions in common.php, it is only necessary to use the following code when sending a message.
SendMail ("xxx@xxx.com", "Mail Header", "message body");

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

Welcome to successfully configure the classmate SendMail ("dreamrunner@foxmail.com", "I will also use thinkphp send mail", "Blah blah ~ ~");

There are some notes

What if I need to wrap the message body? In fact, the simplest way is--

SendMail ("xxx@xxx.com", "Mail Header", "message body
I'll change the line, ~~!. ");

QQ mailbox (including Foxmail) and NetEase's 126, 163 are tested successfully. Because the recent visit Gmail is difficult, so there is no test. I heard that Gmail requirements must use SSL, there is the need for students to Google Baidu Phpmailer method.

http://www.bkjia.com/PHPjc/313571.html www.bkjia.com true http://www.bkjia.com/PHPjc/313571.html techarticle This article uses thinkphp version 2.1 and Phpmailer 5.1. (The latter recommends that you download it directly from this blog, as we cannot guarantee that the following code will work in all versions of Phpmailer .)

  • Related Article

    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.