首先在系统根目录下extend文件夹下创建邮件工厂文件夹并更名叫Mail。
一、邮件发送父类
在Mail目录下创建邮件发送父类并更名为MailSenderInterface.php,代码如下
<?php
/**
* 邮件发送父类
* User: 龙哥·三年风水
* Date: 2024/12/5
* Time: 14:22
*/
namespace Mail;
interface MailSenderInterface
{
/**
* 单个邮件发送
* User: 龙哥·三年风水
* Date: 2024/12/5
* Time: 14:29
* @ param $emailSubject 邮件主题
* @ param $emailContent 邮件内容
* @ param string $emailAttachment 邮件附件
* @ return mixed
*/
public static function send($emailSubject, $emailContent, $emailAttachment = '');
}
二、具体类实现
在Mail目录下创建文件夹并命名为channel,然后在channel文件夹下,创建QQ邮件发送类、网易邮件发送类等,代码如下
<?php
/**
* 腾讯QQ邮件发送类
* User: 龙哥·三年风水
* Date: 2024/12/5
* Time: 15:21
*/
namespace Mail\channel;
use Mail\MailSenderInterface;
class QqMailSender implements MailSenderInterface
{
public function __construct($param){
}
/**
* 单个邮件发送
* User: 龙哥·三年风水
* Date: 2024/12/5
* Time: 14:29
* @ param $emailSubject 邮件主题
* @ param $emailContent 邮件内容
* @ param string $emailAttachment 邮件附件
* @ return mixed
*/
public static function send($emailSubject, $emailContent, $emailAttachment = '')
{
// TODO: Implement send() method.
}
}
三、邮件发送类实例
在Mail目录下创建邮件发送类实例并更名为MailSenderFactory.php,具体代码如下:
<?php
/**
* 创建邮件发送类实例工厂
* User: 龙哥·三年风水
* Date: 2024/12/5
* Time: 14:32
*/
namespace Mail;
use app\model\param\Emailsms;
use Error\BaseError;
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;
break;
case "163.com":
if(!in_array(2,$emailIds))throw new BaseError("未开启163邮件发送通道",50000,200);
self::$channel['mail_id'] = 2;
break;
default:
self::$instance = null;
self::$channel = [];
throw new BaseError("未设置任何短信发送通道",50000,200);
break;
}
return self::$instance;
}
}
四、提前说明
明天开始将完成邮件工厂类封装及测试。