ThinkPHP USES PHPMailer to send mail implementation code

  • 2020-09-16 07:25:14
  • OfStack

ThinkPHP 2.1 and PHPMailer 5.1 were used for this article. (The latter is recommended to download directly from this blog, as we cannot guarantee that the following code will work in all versions of PHPMailer)

Here are the steps:

Step 1: Add the PHPMailer class library

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

Step 2: Add the send mail function

Add the following code to the ES26en. php file (if not created) in the Common folder in the project directory:


<?php
/**********
 *  Send E-mail  *
 **********/
function SendMail($address,$title,$message)
{
    vendor('PHPMailer.class#PHPMailer');
    $mail=new PHPMailer();
    //  Set up the PHPMailer use SMTP Server send Email
    $mail->IsSMTP();
    //  Sets the character encoding of the message, or if not specified 'UTF-8'
    $mail->CharSet='UTF-8';
    //  Add recipient addresses, which can be used multiple times to add multiple recipients 
    $mail->AddAddress($address);
    //  Set the body of the message 
    $mail->Body=$message;
    //  Set the header From Field. 
    $mail->From=C('MAIL_ADDRESS');
    //  Set sender name 
    $mail->FromName='LilyRecruit';
    //  Set the subject of the message 
    $mail->Subject=$title;
    //  Set up the SMTP The server. 
    $mail->Host=C('MAIL_SMTP');
    //  Set to "Need validation" 
    $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 mailbox information

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


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

The email login account may need to include the following content of @, please try ^_^ by yourself
Step 4: Send the email on Action
Since ThinkPHP automatically loads the functions in ES48en.php, you only need to use the following code when you need to send an email.
SendMail("xxx@xxx.com"," Subject "," Body ");

This completes the tutorial. Flower ~ ~ ~

Welcome SendMail("dreamrunner@foxmail.com"," I can also use ThinkPHP to send emails "," blah blah blah ~~");

There's one more explanation

What if you need a line break in the body of the message? Well, the easiest way to do it is to --

SendMail("xxx@xxx.com"," Subject "," Body of message"
I'm switching lines!" );

QQ mailbox (including foxmail) and netease 126, 163 were tested successfully. Because it is difficult to visit GMail recently, there is no test. I heard that GMail requires the use of SSL, and those students who need it can use the method of PHPMailer by themselves.


Related articles: