Thinkphp using Phpmailer to implement _php instances of message delivery implementation code

Source: Internet
Author: User

This article uses the thinkphp version 2.1 and Phpmailer 5.1. (the latter suggests you download it directly from this blog because we cannot guarantee that the following code will work correctly in all versions of Phpmailer)

The following 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 in the thinkphp directory. (Please make sure the class.phpmailer.php file is in 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 Code code as follows:

<?php
/**********
 * Send message *
 **********/
Funct Ion SendMail ($address, $title, $message)
{
    vendor (' Phpmailer.class#phpmailer ');
    $mail =new phpmailer ();
   //Set Phpmailer use SMTP server to send email
    $mail->issmtp ();
   //Set the character encoding of the message, if not specified, then ' UTF-8 '
    $mail->charset= ' UTF-8 ';
   //Add a recipient address that can be used multiple times to add multiple recipients
    $mail->addaddress ($address);
   //Set message body
    $mail->body= $message;
   //Set the From field of the headers.
    $mail->from=c (' mail_address ');
   //Set sender name
    $mail->fromname= ' lilyrecruit ';
   //Set mail title
    $mail->subject= $title;
   //Set up the SMTP server.
&NBSP;&NBSP;&NBsp $mail->host=c (' mail_smtp ');
   //set to require validation
    $mail->smtpauth=true;
   //Set user name and password.
    $mail->username=c (' mail_loginname ');
    $mail->password=c (' Mail_password ');
   //Send mail.
    return ($mail->send ());
}
?>






Third Step, configure mailbox information

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

Copy Code code as follows:

' Mail_address ' => ' xxx@126.com ',//email address
' Mail_smtp ' => ' smtp.126.com ',//mailbox SMTP Server
' Mail_loginname ' => ' xxx ',//e-Mail login account
' Mail_password ' => ' xxx ',//e-mail password

The mailbox login account may need to include the contents of the @ behind, please try ^_^
Step fourth, send mail in action
Because thinkphp automatically loads the functions in common.php, you only need to use the following code when you need to send a message.
SendMail ("xxx@xxx.com", "Mail title", "Message body");

So far, this tutorial is over. Sprinkle flowers ~ ~ ~

Welcome to the successful configuration of the classmate SendMail ("dreamrunner@foxmail.com", "I will also send mail with thinkphp", "Blah blah ~ ~");

There are also some explanations

What if you need to change lines in the body of the message? In fact, the easiest way to do this is--

SendMail ("xxx@xxx.com", "Mail title", "Message body")
I'm changing, ~~!. ");

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

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.