Thinkphp5+PHPMailer for sending mail

Keywords: PHP SSL github encoding

PHP needs to start the following services to send mail:

SMTP: Simple Mail Transfer Protocol for sending mail

POP3: Simple mail access protocol, one-way, local mail operations will not be synchronized to the mail server to receive mail

IMAP: is a more complex mail access protocol than POP3. It is bidirectional, and local operations on mailboxes are synchronized to the mailbox server to receive mail

Before sending, you need to have your own mail server. When testing, you can use the free mailbox you applied for. You do not need to set up your own server. You may need to configure the SMTP service of your mailbox. Most public mailboxes (163, qq, etc.) are closed by default and need to be opened manually in order to be safe.

NetEase Mailbox Configuration

 

QQ Mailbox Configuration

 

After the basic introduction, start to talk about how to use (take QQ mailbox for example).

1. Opening Services

Log in to the QQ mailbox, with a setup button on the top, click on the account, pull to the end, open the first line of POP3/SMTP service, after which there will be a series of passwords, which will be copied down for use.

 

 

 

2. Download and install PHPMailer

1. Download from github: https://github.com/PHPMailer/PHPMailer/

2. Install using composer:

composer require phpmailer/phpmailer

If you're just sending mail, leave only phpmailer.php and smtp.php.Create a new phpmailer folder under the vendor folder, place the downloaded files in this folder, open the composer.json file, add a line of code under autoload phpmailer is the namespace name, vendor/phpmailer is the corresponding file name

"autoload": {
        "psr-4": {
            "app\\": "application",
            "phpmailer\\": "vendor/phpmailer"
        }
    },

Open PHPMailer.php and SMTP.php, respectively, and modify their namespaces to "namespace phpmailer;". Both files need to be modified, otherwise the file will not be found

 

Open cmd, enter project root directory, load third-party class library using composer command, execute composer dump-autoload

 

3. Controller Code

<?php
namespace app\index\controller;
use think\Controller;
use phpmailer\PHPMailer;
use phpmailer\Exception;

class Sendmail extends Controller
{
    // 
    public function index()
    {    
        $toemail = '*****@126.com';    //This is the recipient's mailbox
        $mail=new Phpmailer();
        $mail->isSMTP();    // Use SMTP Service (mail-sending service)
        $mail->CharSet = "utf8";    // Encoding format is utf8,If no encoding is set, the Chinese language will be garbled
        $mail->Host = "smtp.qq.com";    // Sender's SMTP server address
        $mail->SMTPAuth = true;    // Whether to use authentication
        $mail->Username = "12*****186@qq.com";    // Applied smtp Mailbox name of service (own mailbox name)
        $mail->Password = "hcstaffeplbcjgii";    // Sender's mailbox password, not login password,yes qq Third party authorized login number,Open it yourself (the password you saved earlier)
        $mail->SMTPSecure = "ssl";    // Use ssl Agreement Mode,
        $mail->Port = 465;    // QQ Mailbox ssl Protocol mode port number is 465/587
        $mail->setFrom("12*****186@qq.com","Test Sender");    // Set sender information, such as the sender in the message format description,
        $mail->addAddress($toemail,'Test Recipient');    // Set recipient information, such as the recipient in the message format description
        $mail->addReplyTo("12*****186@qq.com","Reply");    // Setting the reply person information refers to the mailbox address to which the reply message will be sent if the recipient wants to reply after receiving the message
        //$mail->addCC ("xxx@163.com"); //Set up a mail copier to write only addresses or address only (this person can also receive mail)
        //$mail->addBCC ("xxx@163.com"); set up a secret Copyer (this person can also receive mail)
        //$mail->addAttachment ("bug0.jpg"); //Add Attachment
        $mail->Subject = "This is a test message";    // Mail Title
        $mail->Body = '<h1>Here is the message content</h1>' . date('Y-m-d H:i:s');// Body
        //$mail->AltBody = "This is the plain text"; //This is the body content that is displayed in plain text, which is useless if Html is not supported**
       if(!$mail->send()){    // Send Mail
           echo "Message could not be sent.";
           echo "Mailer Error: ".$mail->ErrorInfo;    // Output Error Information
        }else{
            echo '';
            return 'Send Successfully';
        }
    }

}

 

4. Operation

sendmail.bat

@echo off
start http://Web address/sendmail.php
exit

 

V. Operation results

Sender Mailbox

 

Recipient Mailbox

Posted by dustbuster on Fri, 03 Apr 2020 07:54:38 -0700