群控系统服务端开发模式-应用开发-邮件发送工具类

发布于:2024-12-18 ⋅ 阅读:(60) ⋅ 点赞:(0)

一、邮件发送工具类开发

        1、添加框架对应的SDK

composer require phpmailer/phpmailer

        2、添加工具集

                在根目录下extend文件夹下创建Email文件夹,在Email文件夹下添加工具集控制并命名为EmailSender.php

<?php
/**
 * 邮件发送工具
 * User: 龙哥·三年风水
 * Date: 2024/12/9
 * Time: 10:20
 */
namespace Email;
use Error\BaseError;
use PHPMailer\PHPMailer\PHPMailer;
use app\model\param\Mail;
class EmailSender
{
    protected static $host = '';// 发送人的SMTP服务器地址
    protected static $charSet = 'utf8';// 编码格式为utf8,不设置编码的话,中文会出现乱码
    protected static $account = '';// 发件人账号
    protected static $username = '杭州安巽';// 发件人名称
    protected static $password = '';// 发送方的邮箱密码,注意这里填写的是“客户端授权密码”而不是邮箱的登录密码!
    protected static $stmpSecure = '';// 使用ssl协议方式
    protected static $port = 0;// ssl协议方式端口号是465
    protected static $phpMailer = null;// 邮件发送客户端
    // 初始化数据
    public function __construct(){
        $res = Mail::dataFind(['id' => 2],'username,smtp_address,smtp_port,smtp_password,smtp_protocol,status',true);
        if(empty($res))throw new BaseError("未开启微软邮件发送通道",50000,200);
        if($res['status'] == 0)throw new BaseError("微软邮件发送通道已被禁用",50000,200);
        self::$host = $res['smtp_address'];
        self::$port = $res['smtp_port'];
        self::$password = $res['smtp_password'];
        self::$stmpSecure = $res['smtp_protocol'];
        self::$account = $res['username'];
        self::$phpMailer = new PHPMailer(true);
    }

    /**
     * 单条发送邮件
     * User: 龙哥·三年风水
     * Date: 2024/12/9
     * Time: 10:25
     * @ param $recipient
     * @ param $emailSubject
     * @ param $emailContent
     * @ param string $emailAttachment
     * @ return bool
     * @ throws \PHPMailer\PHPMailer\Exception
     */

    public static function send($recipient, $emailSubject, $emailContent, $emailAttachment = ''){
        if(empty($recipient) || empty($emailSubject) || empty($emailContent))throw new BaseError('邮件参数必须传',50000,200);
        if(is_array($recipient) && count($recipient) >= 0)throw new BaseError('接收人只能是字符串方式',50000,200);
        self::$phpMailer->isSMTP();// 使用SMTP服务
        self::$phpMailer->CharSet = self::$charSet;// 编码格式为utf8,不设置编码的话,中文会出现乱码
        self::$phpMailer->Host = self::$host;// 发送人的SMTP服务器地址
        self::$phpMailer->SMTPAuth = true;// 是否使用身份验证
        self::$phpMailer->Username = self::$account;// SMTP账号
        self::$phpMailer->Password = self::$password;// SMTP密码
        self::$phpMailer->SMTPSecure = self::$stmpSecure;// 使用ssl协议方式
        self::$phpMailer->Port = self::$port;// ssl协议方式端口号是465
        self::$phpMailer->setFrom(self::$account,self::$username);// 设置发件人信息,如邮件格式说明中的发件人,这里会显示为
        self::$phpMailer->addAddress($recipient,$emailSubject);// 设置收件人信息,如邮件格式说明中的收件人
        if(!empty($emailAttachment))self::$phpMailer->addAttachment($emailAttachment);// 添加附件
        self::$phpMailer->isHTML(true);
        self::$phpMailer->Subject = $emailSubject;
        self::$phpMailer->Body = $emailContent;
        if(self::$phpMailer->send() == false)throw new BaseError("邮件发送失败:".self::$phpMailer->ErrorInfo,50000,200);
        return true;
    }

    /**
     * 批量发送邮件
     * User: 龙哥·三年风水
     * Date: 2024/12/9
     * Time: 10:29
     * @ param $recipient
     * @ param $emailSubject
     * @ param $emailContent
     * @ param string $emailAttachment
     */

    public static function batchSend($recipients, $emailSubject, $emailContent, $emailAttachment = ''){
        if(empty($recipients) || empty($emailSubject) || empty($emailContent))throw new BaseError('邮件参数必须传',50000,200);
        if(!is_array($recipients) && count($recipients) == 0)throw new BaseError('邮件必须是数组格式',50000,200);
        self::$phpMailer->isSMTP();// 使用SMTP服务
        self::$phpMailer->CharSet = self::$charSet;// 编码格式为utf8,不设置编码的话,中文会出现乱码
        self::$phpMailer->Host = self::$host;// 发送人的SMTP服务器地址
        self::$phpMailer->SMTPAuth = true;// 是否使用身份验证
        self::$phpMailer->Username = self::$account;// SMTP账号
        self::$phpMailer->Password = self::$password;// SMTP密码
        self::$phpMailer->SMTPSecure = self::$stmpSecure;// 使用ssl协议方式
        self::$phpMailer->Port = self::$port;// ssl协议方式端口号是465
        self::$phpMailer->setFrom(self::$account,self::$username);// 设置发件人信息,如邮件格式说明中的发件人,这里会显示为
        if(!empty($emailAttachment))self::$phpMailer->addAttachment($emailAttachment);// 添加附件
        self::$phpMailer->isHTML(true);
        self::$phpMailer->Subject = $emailSubject;
        self::$phpMailer->Body = $emailContent;
        foreach ($recipients as $recipient){
            self::$phpMailer->addAddress($recipient,$emailSubject);// 设置收件人信息,如邮件格式说明中的收件人
            if(self::$phpMailer->send() == false)throw new BaseError("邮件发送失败:".self::$phpMailer->ErrorInfo,50000,200);
        }
        return true;
    }
}

二、测试

        1、单条发送

<?php
namespace app\controller;
use Email\EmailSender;
use Encipher\Encrypt;
use Mail\MailSenderFactory;
use Sms\SmsSenderFactory;

class Index extends Emptys
{
    public function index()
    {
        $emailSender = new EmailSender();
        $emailSender::send('1509454760@qq.com','运维发送','IP更改');
        return succ('操作成功');
    }
}

        2、批量发送

<?php
namespace app\controller;
use Email\EmailSender;
use Encipher\Encrypt;
use Mail\MailSenderFactory;
use Sms\SmsSenderFactory;

class Index extends Emptys
{
    public function index()
    {
        $emailSender = new EmailSender();
        $emailSender::batchSend(['1509454760@qq.com','2420095288@qq.com'],'运维发送','数据模式切换');
        return succ('操作成功');
    }
}

三、总结

        通过这5天的开发做出对比后,才发现,其实邮件发送功能只需要采用工具类形式开发就行。无需建立工厂开发模式。


网站公告

今日签到

点亮在社区的每一天
去签到