您的位置 首页 php

以PHP门面模式实现简单的邮件发送

前言:

门面模式属于设计模式中三大分类之一的结构类型,也叫外观模式。其作用对客户端低耦合底层功能的封装,客户端不用知道子系统间的调用。

举例:

门面模式就相当于电脑主机,用户要打开某个应用程序,只需要知道两步。打开开机按钮,电脑开机后再打开应用。开机按钮就相当于一个门面,里面的开机需要调用不同的模块,比如硬件自检,选择启动盘,加载引导,加载内核,OS初始化,启动指定级任务等,以下也通过发邮件的例子描述门面一模式。

涉及:

1. call_user_func函数的使用
2. 异常类的自定义处理
3. 类的分层封装
4. 发邮件功能的实现与配置

编码:

1. 必须先composer require phpmailer/phpmailer安装依赖库。

2. 创建扩展类目录,里面包括独立的配置文件,门面角色类,邮件功能类,校验类,异常类。

3. 独立的配置类,包括smtp服务地址,端口,中转邮箱账号,授权码,邮件发送者昵称(唯一标识)。

 <?php
/**
 * @Notes: 邮箱SMTP服务配置
 * @Interface getCondition
 * @Return mixed
 * @Author: bqs
 * @Time: 2020/8/31 10:15
 */

return [
    'smtp_server' => 'smtp.qq.com',                     // QQ邮箱开启的smtp
    'smtp_port' => 465,                                 // QQsmtp服务端口
    'smtp_user' => '2652364582@qq.com',                 // 北桥苏邮箱
    'smtp_pwd' => 'ynxdedefduuhecbj',                   // SMTP服务开启后授权码
    'email_id' => '酷D'                                 // 邮件发送者的唯一标识(自定义的昵称)
];




  

4. 门面角色类,也就是客户直接调用的,只有一个发送方法,但是该方法需要调用校验和实际发送的方法实现。

 <?php
/**
 * @Notes: 邮件门面
 * @Interface getCondition
 * @Return mixed
 * @Author: bqs
 * @Time: 2020/8/31 13:10
 */

namespace mail;

use think\Container;
use mail\facade\MailException;
use mail\facade\Mail;
use mail\facade\Validate;

class MailFacade
{

    protected $error;

    public static function __callStatic($method, $params)
    {
        //return (new static)->{$method}(...$params);
        return call_user_func([new MailFacade(),$method],$params);
    }

    /**
     * @Notes: 面向客户的邮件发送调用
     * @Author: bqs
     * @Time: 2020/8/31 13:33
     * @Interface send
     * @param $params
     * @Return boolean 成功|失败
     */
    private function send($params)
    {
        // 校验参数
        $validate = Validate::make(__FUNCTION__);

        $res = $validate->check($params);

        if (!$res) {
            // 抛出自定义异常
            throw new MailException($validate->getError(),422);
            return false;
        }

        // 发送邮件
        $mail = new Mail();
        $res = $mail->send($params);

        return $res;
    }

}


  

5. 自定义异常类,可以在门面角色中以该类抛出,然后在客户调用中以该类捕捉,以下自定义了错误消息的输出。

 <?php
/**
 * @Notes: 邮件发送校验器
 * @Interface getCondition
 * @Return mixed
 * @Author: bqs
 * @Time: 2020/8/31 13:03
 */

namespace mail\facade;


class MailException extends \Exception
{

    public function errorMessage()
    {
        return "mail error: ".$this->getMessage();
    }

}


  

6. 校验器,主要判断客户调用传入的参数。

 <?php
/**
 * @Notes: 邮件发送校验器
 * @Interface getCondition
 * @Return mixed
 * @Author: bqs
 * @Time: 2020/8/31 13:03
 */

namespace mail\facade;


class Validate
{
    protected $error;

    protected $type;        // 方法名

    public function __construct($type)
    {
        $this->type = $type;
    }

    // 创建验证器对象
    public static function make($type)
    {
        return new self($type);
    }

    // 与实际传入的参数做校验
    public function check($params = [])
    {
        if (empty($params)) {
            $this->error = "参数不足,非法请求";
        }

        $this->error = call_user_func([new self($this->type),$this->type],$params);

        return $this->error ? false : true;
    }

    // 发送参数校验
    public function send($params)
    {
        $res = "";

        // 邮件
        if (!isset($params[0]) || empty($params[0])) {
            return "邮箱不能为空";
        }

        $email = [];
        if (is_array($params[0])) {
            $email = $params[0];
        }else {
            $email[0] = $params[0];
        }

        foreach ($email as $key => $val) {
            if (!preg_match("/^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/",$val)) {
                return "邮箱格式不正确";
            }
        }

        // 邮件标题
        if (!isset($params[1]) || !$params[1]) {
            return "邮件标题不能为空";
        }

        if (!isset($params[2]) || !$params[2]) {
            return "邮件内容不能为空";
        }

        return $res;
    }

    // 获取错误信息
    public function getError()
    {
        return $this->error;
    }

}


  

7. 实际的邮件发送,需要使用phpmail库。

 <?php
/**
 * @Notes: 邮件实际发送
 * @Interface getCondition
 * @Return mixed
 * @Author: bqs
 * @Time: 2020/8/31 13:03
 */

namespace mail\facade;

use PHPMailer\PHPMailer\PHPMailer;

class Mail
{
    protected $config = [];

    public function __construct()
    {
        $this->config = include(dirname(__DIR__) . "../config/mail_config.php");
    }

    /**
     * @Notes: 发邮件
     * @Author: bqs
     * @Time: 2020/8/31 13:07
     * @Interface send
     * @Return mixed
     */
    public function send($params)
    {
        $to = $params[0];                       // 接收者
        $subject = $params[1];                  // 邮件标题
        $content = $params[2];                  // 邮件内容

        $emails = new PHPMailer();

        $emails->CharSet = 'UTF-8'; //设定邮件编码,默认ISO-8859-1,如果发中文此项必须设置,否则乱码
        $emails->isSMTP();

        //Enable SMTP debugging
        // 0 = off (for production use)
        // 1 = client messages
        // 2 = client and server messages
        $emails->SMTPDebug = 0;

        //调试输出格式
        //$emails->Debugoutput = 'html';
        //smtp服务器
        $emails->Host = $this->config['smtp_server'];

        //端口 - likely to be 25, 465 or 587
        $emails->Port = $this->config['smtp_port'];

        if ($emails->Port === 465) $emails->SMTPSecure = 'ssl';// 使用安全协议
        //Whether to use SMTP authentication
        $emails->SMTPAuth = true;
        //发送邮箱
        $emails->Username = $this->config['smtp_user'];
        //密码
        $emails->Password = $this->config['smtp_pwd'];
        //Set who the message is to be sent from
        $emails->setFrom($this->config['smtp_user'], $this->config['email_id']);

        //回复地址
        //$emails->addReplyTo('replyto@example.com', 'First Last');

        // 接收邮件方
        if (is_array($to)) {

            foreach ($to as $v) {
                $emails->addAddress($v);
            }

        } else {

            $emails->addAddress($to);
        }

        $emails->isHTML(true);// send as HTML

        //标题
        $emails->Subject = $subject;

        //HTML内容转换
        $emails->msgHTML($content);

        //Replace the plain text body with one created manually
        //$emails->AltBody = 'This is a plain-text message body';

        //添加附件
        //$emails->addAttachment('images/phpmailer_mini.png');
        //send the message, check for errors

        return $emails->send();
    }

}


  

8. 客户调用部分。

 // 测试发邮件的门面
    public function sendMail()
    {

        try {

            $res = \mail\MailFacade::send(["1641181271@qq.com"], "测试标题", "测试内容");

            var_dump($res);
            die;

        } catch (MailException $e) {                // 捕捉自定义异常类抛出

            var_dump($e->errorMessage());
            die;

        } catch (\Exception $e) {

            var_dump($e->getMessage());
            die;
        }

    }


  

9. 返回true后查看邮件是否接收。

环境要求:

实现邮件发送是需要特定的环境和相关的配置才能实现,以下就以实现成功发送补充的操作。

第一步:

打开网址 下载PHPMailer,PHPMailer 需要 PHP 的 sockets 扩展支持,而登录 QQ 邮箱 SMTP 服务器则必须通过 SSL 加密的, PHP 还得包含 openssl 的支持。

第二步:使用 phpinfo() 函数查看 socket 和 openssl 扩展信息(wamp server 默认启用了该扩展)。

openssl 如果没有开启请打开php.ini文件进行开启

首先检查php.ini中;extension=php_openssl.dll是否存在, 如果存在的话去掉前面的注释符‘;’, 如果不存在这行,那么添加extension=php_openssl.dll。

PHPMailer 核心文件

第三步:**QQ 邮箱设置

所有的主流邮箱都支持 SMTP 协议,但并非所有邮箱都默认开启,您可以在邮箱的设置里面手动开启。

第三方服务在提供了账号和密码之后就可以登录 SMTP 服务器,通过它来控制邮件的中转方式。

第四步:开启 SMTP 服务

选择 IMAP/SMTP 服务,点击开启服务

第五步:验证密保

发送短信“配置邮件客户端”至1069-0700-69

第六步:获取授权码

文章来源:智云一二三科技

文章标题:以PHP门面模式实现简单的邮件发送

文章地址:https://www.zhihuclub.com/77678.shtml

关于作者: 智云科技

热门文章

网站地图