Home  >  Article  >  Backend Development  >  Send email in ThinkPHP5 using PHPMailer

Send email in ThinkPHP5 using PHPMailer

coldplay.xixi
coldplay.xixiforward
2020-06-08 11:19:573755browse

Send email in ThinkPHP5 using PHPMailer

ThinkPHP5 uses PHPMailer to send emails

phpMailer is a very powerful php email class that can set the sending email address and reply Address, email subject, html web page, upload attachments, and it is very convenient to use.

Features of phpMailer:

1. Contain multiple TO, CC, BCC and REPLY-TO in the email.

2. The platform is widely used and supported SMTP servers include Sendmail, qmail, Postfix, Gmail, Imail, Exchange, etc.

3. Support embedded images, attachments, and HTML emails.

4. Reliable and powerful debugging function.

5. Support SMTP authentication.

6. Customize the email header.

7. Supports 8bit, base64, binary and quoted-printable encoding.

phpMailer installation or download method:

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

2. Use composer to install:

composer require phpmailer/phpmailer

or

Add

"phpmailer/phpmailer": "~6.0"

in your composer.json file. You need to have your own mail server before sending. , when testing, it is actually most convenient to use the free mailbox you applied for. You do not need to build a server yourself. You may need to configure the SMTP service of the mailbox. Most public mailboxes (163, qq, etc.) are closed by default for security reasons.

NetEase mailbox configuration is as shown below:

Send email in ThinkPHP5 using PHPMailer

## QQ mailbox related configuration is as shown below:

qq.compop.qq.comsmtp.qq .com
Email POP3 Server (Port 995) smtp server (port 465 or 587)

Of course, in addition to NetEase and QQ mailboxes, other mailboxes are also available.

After executing the above composer command, the /vendor directory under the root directory will There is one more phpmailer folder

php code example:

<?php
namespace app\api\controller;
use think\Controller;
 
use think\Cache;
use think\Db;
 
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
 
class Test extends Controller
{
 
    public function email(){
        $mail = new PHPMailer(true);                              // Passing `true` enables exceptions 
        try { 
            //服务器配置 
            $mail->CharSet ="UTF-8";                     //设定邮件编码 
            $mail->SMTPDebug = 0;                        // 调试模式输出 
            $mail->isSMTP();                             // 使用SMTP 
            $mail->Host = &#39;smtp.163.com&#39;;                // SMTP服务器 
            $mail->SMTPAuth = true;                      // 允许 SMTP 认证 
            $mail->Username = &#39;liqingbo27@163.com&#39;;      // SMTP 用户名  即邮箱的用户名 
            $mail->Password = &#39;&#39;;                        // SMTP 密码  部分邮箱是授权码(例如163邮箱,不明白看下面有说明) 
            $mail->SMTPSecure = &#39;ssl&#39;;                   // 允许 TLS 或者ssl协议 
            $mail->Port = 465;                           // 服务器端口 25 或者465 具体要看邮箱服务器支持 
 
            $mail->setFrom(&#39;liqingbo27@163.com&#39;, &#39;Mailer&#39;);  //发件人 
            $mail->addAddress(&#39;252588119@qq.com&#39;, &#39;Joe&#39;);  // 收件人 
            //$mail->addAddress(&#39;ellen@example.com&#39;);  // 可添加多个收件人 
            $mail->addReplyTo(&#39;liqingbo27@163.com&#39;, &#39;info&#39;); //回复的时候回复给哪个邮箱 建议和发件人一致 
            //$mail->addCC(&#39;cc@example.com&#39;);                    //抄送 
            //$mail->addBCC(&#39;bcc@example.com&#39;);                    //密送 
 
            //发送附件 
            // $mail->addAttachment(&#39;../xy.zip&#39;);         // 添加附件 
            // $mail->addAttachment(&#39;../thumb-1.jpg&#39;, &#39;new.jpg&#39;);    // 发送附件并且重命名 
 
            //Content 
            $mail->isHTML(true);                                  // 是否以HTML文档格式发送  发送后客户端可直接显示对应HTML内容 
            $mail->Subject = &#39;这里是邮件标题&#39; . time(); 
            $mail->Body    = &#39;<h1>这里是邮件内容</h1>&#39; . date(&#39;Y-m-d H:i:s&#39;); 
            $mail->AltBody = &#39;如果邮件客户端不支持HTML则显示此内容&#39;; 
 
            $mail->send(); 
            echo &#39;邮件发送成功&#39;; 
        } catch (Exception $e) { 
            echo &#39;邮件发送失败: &#39;, $mail->ErrorInfo; 
        }
    }
}

Direct access link: https://www.liqingbo.cn/api/test/email

Normal In this case, it will output: Email sent successfully

The recipient effect is as shown in the figure

Send email in ThinkPHP5 using PHPMailer

Send email in ThinkPHP5 using PHPMailer

163 Set authorization code


Settings->Client authorization password->Authorization code

Send email in ThinkPHP5 using PHPMailer##Recommended tutorial: "

PHP Video Tutorial

"

The above is the detailed content of Send email in ThinkPHP5 using PHPMailer. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:liqingbo.cn. If there is any infringement, please contact admin@php.cn delete