interop.cdo 发送邮件乱码_以PHP门面模式实现简单的邮件发送

本文介绍门面模式并通过一个邮件发送系统的实例进行说明。详细讲述了如何利用门面模式简化客户端调用流程,并展示了从配置到异常处理的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言:

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

举例:

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

涉及:

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

编码:

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

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

0bb0690812336da420f2996ce09f186f.png

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 thinkContainer;use mailfacadeMailException;use mailfacadeMail;use mailfacadeValidate;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 mailfacade;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 mailfacade;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一-龥]+@[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 mailfacade;use PHPMailerPHPMailerPHPMailer;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 = mailMailFacade::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后查看邮件是否接收。

环境要求:

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

第一步:

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

9482eec38455e60fb0bb475d61caa181.png

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

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

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

0dde0d8aab87b56f97fa73e465530598.png

PHPMailer 核心文件

5b826a77a9dad17ed4b0a16a54d40eab.png

第三步:**QQ 邮箱设置

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

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

第四步:开启 SMTP 服务

a0cd38bfc4e0d32d65d79f12c080a9c9.png

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

第五步:验证密保

7762e2180e893dc5e6262e304e266959.png

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

第六步:获取授权码

dcad6fcaba54cc0d4502aaf0d5dbf1d9.png
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值