TP5.1 发送邮件

安装

composer require phpmailer/phpmailer

直接上代码

<?php
/**
 * Created by PhpStorm.
 * User: Zhangyongfeng
 * Date: 2020/12/2
 * Time: 13:15
 *
 * ━━━━━━━━━神兽出没━━━━━━━━━
 *
 *        ┏┓   ┏┓+ +
 *       ┏┛┻━━━┛┻┓ + +
 *       ┃       ┃  
 *       ┃   ━   ┃ ++ + + +
 *       ████━████ ┃+
 *       ┃       ┃ +
 *       ┃   ┻   ┃
 *       ┃       ┃ + +
 *       ┗━┓   ┏━┛
 *         ┃   ┃           
 *         ┃   ┃ + + + +
 *         ┃   ┃    Code is far away from bug with the animal protecting       
 *         ┃   ┃ +     神兽保佑,代码无bug  
 *         ┃   ┃
 *         ┃   ┃  +         
 *         ┃    ┗━━━┓ + +
 *         ┃        ┣┓
 *         ┃        ┏┛
 *         ┗┓┓┏━┳┓┏┛ + + + +
 *          ┃┫┫ ┃┫┫
 *          ┗┻┛ ┗┻┛+ + + +
 *
 * ━━━━━━━━━感觉萌萌哒━━━━━━━━━
 */

namespace appasecontroller;

use PHPMailerPHPMailerPHPMailer;

class Mailer extends Base
{

    /*
     * 发送邮件
     * @param $toemail          //收件人地址
     * @param $toname           //收件人名称
     * @param string $subject   //主题
     * @param string $body      //内容
     * @param null $attachment  //附件
     * @return bool
     **/
    function send_mail($toemail, $toname, $subject = '', $body = '', $attachment = null)
    {
        $username = $this->config['cfg_mailUserName'];
        $password = $this->config['cfg_mailPassword'];
        $mail = new PHPMailer();            //实例化PHPMailer对象
        $mail->CharSet = 'UTF-8';           //设定邮件编码,默认ISO-8859-1,如果发中文此项必须设置,否则乱码
        $mail->IsSMTP();                    // 设定使用SMTP服务
        $mail->SMTPDebug = 0;               // SMTP调试功能 0=关闭 1 = 错误和消息 2 = 消息
        $mail->SMTPAuth = true;             // 启用 SMTP 验证功能
        $mail->SMTPSecure = 'ssl';          // 使用安全协议
        $mail->Host = "smtp.qq.com";        // 企业邮局域名
        $mail->Port = 465;                  //设置ssl连接smtp服务器的远程服务器端口号 可选465或587

        $mail->Username = $username;    //邮件发送人的用户名(请填写完整的email地址)
        $mail->Password = $password;    // 邮件发送人的 密码 (授权码)

        $mail->SetFrom($username, $username);
        $replyEmail = '';                   //留空则为发件人EMAIL
        $replyName = '';                    //回复名称(留空则为发件人名称)
        $mail->AddReplyTo($replyEmail, $replyName);  //回复的地址

        $mail->Subject = $subject;   //邮件标题
        $mail->MsgHTML($body);       //邮件内容

        $mail->AddAddress($toemail, $toname);  //收件人地址,("收件人email","收件人姓名")

        if (is_array($attachment)) { // 添加附件
            foreach ($attachment as $file) {
                is_file($file) && $mail->AddAttachment($file);
            }
        }
        return $mail->Send() ? true : $mail->ErrorInfo;
    }


}

调用

  // 邮箱发送
    public function mailer()
    {
        $toemail = '1322816443@qq.com';
        $toname = '1322816443';
        $subject = 'QQ邮件发送测试';
        $content = '<h1>恭喜你,邮件测试成功。</h1>';
        dump($this->mailer->send_mail($toemail, $toname, $subject, $content));
    }
原文地址:https://www.cnblogs.com/zyfeng/p/14074109.html