一、邮件发送类实例修改
在Mail目录下修改邮件发送类实例,具体代码如下:
<?php
/*** 创建邮件发送类实例工厂* User: 龙哥·三年风水* Date: 2024/12/5* Time: 14:32*/
namespace Mail;
use app\model\param\Emailsms;
use Error\BaseError;
use Mail\channel\QqMailSender;
use Mail\channel\WangyiMailSender;class MailSenderFactory
{protected static $instance=null;//缓存实例protected static $channel = [];//通道参数/*** 调用邮件类实例* User: 龙哥·三年风水* Date: 2024/12/5* Time: 14:49* @ param $recipient 邮箱名称*/public static function create($recipient){$res = Emailsms::dataFind(['id' => 1],'email_id');if(empty($res) || empty($res['email_id']))throw new BaseError("未设置任何邮件发送通道",50000,200);$emailIds = explode(',',$res['email_id']);$emailType = explode('@',$recipient);self::$channel['recipient'] = $recipient;switch ($emailType[1]){case "qq.com":if(!in_array(1,$emailIds))throw new BaseError("未开启QQ邮件发送通道",50000,200);self::$channel['mail_id'] = 1;self::$instance = new QqMailSender(self::$channel);break;case "163.com":if(!in_array(2,$emailIds))throw new BaseError("未开启163邮件发送通道",50000,200);self::$channel['mail_id'] = 2;self::$instance = new WangyiMailSender(self::$channel);break;default:self::$instance = null;self::$channel = [];throw new BaseError("未设置任何短信发送通道",50000,200);break;}return self::$instance;}
}
二、QQ邮件工厂开发
1、添加框架对应的SDK
composer require phpmailer/phpmailer
2、添加QQ邮件工厂
在根目录下extend文件夹下Mail文件夹下channel文件夹下,创建QQ邮件发送工厂并命名为QqMailSender。记住,一定要在QQ邮件发送工厂类名后面去实现邮件发送工厂。
<?php
/*** 腾讯QQ邮件发送类* User: 龙哥·三年风水* Date: 2024/12/5* Time: 15:21*/
namespace Mail\channel;
use app\model\param\Mail;
use Mail\MailSenderInterface;
use Error\BaseError;
use PHPMailer\PHPMailer\PHPMailer;class QqMailSender implements MailSenderInterface
{protected static $host = '';// 发送人的SMTP服务器地址protected static $charSet = 'utf8';// 编码格式为utf8,不设置编码的话,中文会出现乱码protected static $recipient = '';// 收件人protected static $username = '';// 发件人protected static $password = '';// 发送方的邮箱密码,注意这里填写的是“客户端授权密码”而不是邮箱的登录密码!protected static $stmpSecure = '';// 使用ssl协议方式protected static $port = 0;// ssl协议方式端口号是465protected static $phpMailer = null;// 邮件发送客户端public function __construct($param){$res = Mail::dataFind(['id' => $param['mail_id']],'username,smtp_address,smtp_port,smtp_password,smtp_protocol,status',true);if(empty($res))throw new BaseError("未开启QQ邮件发送通道",50000,200);if($res['status'] == 0)throw new BaseError("QQ邮件发送通道已被禁用",50000,200);self::$host = $res['smtp_address'];self::$port = $res['smtp_port'];self::$password = $res['smtp_password'];self::$stmpSecure = $res['smtp_protocol'];self::$recipient = $param['recipient'];self::$username = $res['username'];self::$phpMailer = new PHPMailer(true);}/*** 单个邮件发送* User: 龙哥·三年风水* Date: 2024/12/5* Time: 14:29* @ param $emailSubject 邮件主题* @ param $emailContent 邮件内容* @ param string $emailAttachment 邮件附件* @ return mixed*/public static function send($emailSubject, $emailContent, $emailAttachment = ''){self::$phpMailer->isSMTP();// 使用SMTP服务self::$phpMailer->CharSet = self::$charSet;// 编码格式为utf8,不设置编码的话,中文会出现乱码self::$phpMailer->Host = self::$host;// 发送人的SMTP服务器地址self::$phpMailer->SMTPAuth = true;// 是否使用身份验证self::$phpMailer->Username = self::$username;// SMTP账号self::$phpMailer->Password = self::$password;// SMTP密码self::$phpMailer->SMTPSecure = self::$stmpSecure;// 使用ssl协议方式self::$phpMailer->Port = self::$port;// ssl协议方式端口号是465self::$phpMailer->setFrom(self::$username,$emailSubject);// 设置发件人信息,如邮件格式说明中的发件人,这里会显示为self::$phpMailer->addAddress(self::$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("QQ邮件发送失败:".self::$phpMailer->ErrorInfo,50000,200);return true;}
}
二、测试
<?php
namespace app\controller;
use Encipher\Encrypt;
use Mail\MailSenderFactory;
use Sms\SmsSenderFactory;class Index extends Emptys
{public function index(){$emailSender = MailSenderFactory::create('2420095288@qq.com');$emailSender::send('运维发送','IP更改');return succ('操作成功');}
}