整理PHPMailer 发送邮件 邮件内容为html 可以添加多个附件等

composer require phpmailer/phpmailer

使用:

 $config = [
            'userName' => '***@qq.com',
            'password' => '***',
            'host' => 'smtp.qq.com',
            'port' => 465,
            'receiveEmail' => '***@qq.com,***@qq.com',
            'receiveNickname' => 'a,2',
            'subject' => 'wo是主题',
            'body' => $this->createHtml(), //html内容
            'attachment' => 'runtime/uploads/files/2020-04-07/20200316.pdf,runtime/uploads/files/2020-04-07/20200316.docx',
            'attachmentName' => 'pdf'
        ];
        $email = new PHPMailerLib($config);
        return json($email->sendEmail());

整理的类

<?php

/**
 *  +----------------------------------------------------------------------
 *  | ThinkPHP [ WE CAN DO IT JUST THINK ]
 *  +----------------------------------------------------------------------
 *  | Copyright (c) 2020 ahai574 All rights reserved.
 *  +----------------------------------------------------------------------
 *  | Licensed ( ++++ahai574++++ )
 *  +----------------------------------------------------------------------
 *  | Author: 阿海 <764882431@qq.com>
 *  +----------------------------------------------------------------------
 */

namespace app\common\library;

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

class PHPMailerLib
{
    /**
     * 开启调试 0 不开启 2开启
     */
    private $SMTPDebug = 0;

    /**
     * 发送服务端 smtp.qq.com  smtp.163.com
     */
    private $host = 'smtp.qq.com';

    /**
     * 是否需要开启认证授权
     */
    private $SMTPAuth = true;

    /**
     * 发送的邮箱地址
     */
    private $userName = '';

    /**
     * 邮箱授权密码
     */
    private $password = '';

    /**
     * 发送人的昵称
     */
    private $sendNickname = '';

    /**
     * 使用的协议
     */
    private $SMTPSecure = 'ssl';

    /**
     * 发送的端口如:25 阿里云服务器是禁用这个端口的,所以建议是使用465端口来发送
     */
    private $port = 465;

    /**
     * 接收者的邮箱,使用英文逗号“,”分割
     */
    private $receiveEmail = '';

    /**
     * 接收者的昵称,使用英文逗号“,”分割
     */
    private $receiveNickname = '';

    /**
     * 附件路径 ,使用英文逗号“,”分割
     */
    private $attachment = null;

    /**
     * 附件文件对应的名称 使用英文逗号“,”分割
     */
    private $attachmentName = '';

    /**
     * 回复人的邮箱,使用英文逗号“,”分割
     */
    private $replyEmail = null;

    /**
     * 回复人的名称,使用英文逗号“,”分割
     */
    private $replyNickname = '';

    /**
     * 邮件的主题 标题
     */
    private $subject = '';

    /**
     * 邮件的主体 html内容
     */
    private $body = '';

    public function __construct($config = null)
    {
        if (is_null($config) || !is_array($config)) {
            throw new Exception("配置必须是数组,参数不能为空,请查看setMailerConf的参数");
        }
        //批量设置参数
        $this->setMailerConf($config);
    }

    /**
     * 批量设置
     * 设置发送服务端的配置
     * @param $array 数组 ['userName'=>'','password'=>'','sendNickname'=>'','host'=>'smtp.163.com','port'=>465,...] 
     */
    public function setMailerConf($array)
    {
        if (is_array($array)) {
            $this->SMTPDebug = isset($array['SMTPDebug']) ? $array['SMTPDebug'] : $this->SMTPDebug;
            $this->userName = isset($array['userName']) ? $array['userName'] : $this->userName;
            $this->password = isset($array['password']) ? $array['password'] : $this->password;
            $this->sendNickname = isset($array['sendNickname']) ? $this->setMailerCharset($array['sendNickname']) : $this->sendNickname;
            $this->host = isset($array['host']) ? $array['host'] : $this->host;
            $this->port = isset($array['port']) ? $array['port'] : $this->port;
            $this->receiveEmail = isset($array['receiveEmail']) ? $array['receiveEmail'] : $this->receiveEmail;
            $this->receiveNickname = isset($array['receiveNickname']) ? $this->setMailerCharset($array['receiveNickname']) : $this->receiveNickname;
            $this->replyEmail = isset($array['replyEmail']) ? $array['replyEmail'] : $this->replyEmail;
            $this->replyNickname = isset($array['replyNickname']) ? $this->setMailerCharset($array['replyNickname']) : $this->replyNickname;
            $this->attachment = isset($array['attachment']) ? $array['attachment'] : $this->attachment;
            $this->attachmentName = isset($array['attachmentName']) ? $this->setMailerCharset($array['attachmentName']) : $this->attachmentName;
            $this->subject = isset($array['subject']) ?  $this->setMailerCharset($array['subject']) : $this->subject;
            $this->body = isset($array['body']) ? $array['body'] : $this->body;
        }
        return $this;
    }

    /**
     * 中文--转码
     */
    private function setMailerCharset($value)
    {
        return "=?utf-8?B?" . base64_encode($value) . "?=";
    }

    /**
     * 开启调试 0 不开 2 开
     */
    public function setDebug($debug = 2)
    {
        $this->SMTPDebug = $debug;
        return $this;
    }

    /**
     * 设置附件
     */
    public function setAttachment($attachment = null, $attachmentName = null)
    {
        $this->attachment = $attachment;
        $this->attachmentName = $attachmentName;
        return $this;
    }

    /**
     * 设置内容主体
     */
    public function setBody($html)
    {
        $this->body = $html;
        return $this;
    }

    /**
     * 设置标题
     */
    public function setSubject($title)
    {
        $this->subject = $title;
        return $this;
    }

    /**
     * 设置收件人 及昵称
     */
    public function setReceiveEmail($email, $nickName = null)
    {
        $this->receiveEmail = $email;
        $this->receiveNickname = $nickName;
        return $this;
    }

    /**
     * 设置回复人邮箱 昵称
     */
    public function setReplyEmail($email, $nickName = null)
    {
        $this->replyEmail = $email;
        $this->replyNickname = $nickName;
        return $this;
    }

    /**
     * 发送邮件
     */
    public function sendEmail()
    {

        date_default_timezone_set("Asia/Shanghai");
        $mail = new PHPMailer(true);
        try {
            //服务端
            $mail->CharSet = 'utf-8';
            //$mail->Encoding  = "base64";
            $mail->setLanguage('zh_cn', env('root_path') . "\\vendor\\phpMailer\\phpmailer\\language\\");
            $mail->SMTPDebug = $this->SMTPDebug;
            $mail->isSMTP();
            $mail->Host = $this->host;
            $mail->SMTPAuth = $this->SMTPAuth;
            $mail->Username = $this->userName;
            $mail->Password = $this->password;
            $mail->SMTPSecure = $this->SMTPSecure;
            $mail->Port = $this->port;

            //发送端 只能一个
            $mail->setFrom($this->userName, $this->sendNickname);

            //接收端
            if (is_null($this->receiveEmail) || empty($this->receiveEmail)) {
                throw new Exception("必须设置接收者的邮箱");
            }

            $receiveEmails = explode(",", $this->receiveEmail);
            $receiveNicknames =  explode(",", $this->receiveNickname);
            foreach ($receiveEmails as $key => $val) {
                $mail->addAddress($val, isset($receiveNicknames[$key]) ? $receiveNicknames[$key]  : $val); //如果没有昵称则使用发送者的邮箱作为昵称
            }


            if (!is_null($this->replyEmail) || !empty($this->replyEmail)) {
                $replyEmails = explode(",", $this->replyEmail);
                $replyNicknames =  explode(",", $this->replyNickname);
                foreach ($replyEmails as $key => $val) {
                    $mail->addAddress($val, isset($replyNicknames[$key]) ? $replyNicknames[$key]  : $val); //如果没有昵称则使用回复者的邮箱作为昵称
                }
            }

            // 添加附件
            if (!is_null($this->attachment)) {
                $attachments = explode(",", $this->attachment);

                $attachmentsNames = explode(",", $this->attachmentName);
                foreach ($attachments as $key => $val) {
                    $ext = substr($val, strrpos($val, '.'));
                    $mail->addAttachment($val, isset($attachmentsNames[$key]) ? $attachmentsNames[$key] . $ext : "附件-" . ($key + 1) . $ext);
                }
            }

            $mail->isHTML(true);                                  // Set email format to HTML
            $mail->Subject = $this->subject;

            $mail->Body = $this->body;



            if (!$mail->send()) {
                return ['result' => false, 'errorMsg' => $mail->ErrorInfo];
            } else {
                return ['result' => true, 'msg' => '邮件发送成功'];
            }
        } catch (Exception $e) {
            return ['result' => false, 'errorMsg' => $mail->ErrorInfo];
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值