最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

PHP門面模式實(shí)現(xiàn)簡單的郵件發(fā)送示例

 更新時(shí)間:2023年05月25日 11:33:40   作者:北橋蘇  
這篇文章主要為大家介紹了PHP門面模式實(shí)現(xiàn)簡單的郵件發(fā)送示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

前言:

門面模式屬于設(shè)計(jì)模式中三大分類之一的結(jié)構(gòu)類型,也叫外觀模式。其作用對(duì)客戶端低耦合底層功能的封裝,客戶端不用知道子系統(tǒng)間的調(diào)用。

舉例:

門面模式就相當(dāng)于電腦主機(jī),用戶要打開某個(gè)應(yīng)用程序,只需要知道兩步。打開開機(jī)按鈕,電腦開機(jī)后再打開應(yīng)用。開機(jī)按鈕就相當(dāng)于一個(gè)門面,里面的開機(jī)需要調(diào)用不同的模塊,比如硬件自檢,選擇啟動(dòng)盤,加載引導(dǎo),加載內(nèi)核,OS初始化,啟動(dòng)指定級(jí)任務(wù)等,以下也通過發(fā)郵件的例子描述門面一模式。

涉及:

  • call_user_func函數(shù)的使用
  • 異常類的自定義處理
  • 類的分層封裝
  • 發(fā)郵件功能的實(shí)現(xiàn)與配置 

編碼:

  • 必須先composer require phpmailer/phpmailer安裝依賴庫。
  • 創(chuàng)建擴(kuò)展類目錄,里面包括獨(dú)立的配置文件,門面角色類,郵件功能類,校驗(yàn)類,異常類。

3. 獨(dú)立的配置類,包括smtp服務(wù)地址,端口,中轉(zhuǎn)郵箱賬號(hào),授權(quán)碼,郵件發(fā)送者昵稱(唯一標(biāo)識(shí))。

<?php
/**
 * @Notes: 郵箱SMTP服務(wù)配置
 * @Interface getCondition
 * @Return mixed
 * @Author: bqs
 * @Time: 2020/8/31 10:15
 */
return [
    'smtp_server' => 'smtp.qq.com',                     // QQ郵箱開啟的smtp
    'smtp_port' => 465,                                 // QQsmtp服務(wù)端口
    'smtp_user' => '2652364582@qq.com',                 // 北橋蘇郵箱
    'smtp_pwd' => 'ynxdedefduuhecbj',                   // SMTP服務(wù)開啟后授權(quán)碼
    'email_id' => '酷D'                                 // 郵件發(fā)送者的唯一標(biāo)識(shí)(自定義的昵稱)
];
  • 門面角色類,也就是客戶直接調(diào)用的,只有一個(gè)發(fā)送方法,但是該方法需要調(diào)用校驗(yàn)和實(shí)際發(fā)送的方法實(shí)現(xiàn)。
<?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: 面向客戶的郵件發(fā)送調(diào)用
     * @Author: bqs
     * @Time: 2020/8/31 13:33
     * @Interface send
     * @param $params
     * @Return boolean 成功|失敗
     */
    private function send($params)
    {
        // 校驗(yàn)參數(shù)
        $validate = Validate::make(__FUNCTION__);
        $res = $validate->check($params);
        if (!$res) {
            // 拋出自定義異常
            throw new MailException($validate->getError(),422);
            return false;
        }
        // 發(fā)送郵件
        $mail = new Mail();
        $res = $mail->send($params);
        return $res;
    }
}
  • 自定義異常類,可以在門面角色中以該類拋出,然后在客戶調(diào)用中以該類捕捉,以下自定義了錯(cuò)誤消息的輸出。
<?php
/**
 * @Notes: 郵件發(fā)送校驗(yàn)器
 * @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();
    }
}

校驗(yàn)器,主要判斷客戶調(diào)用傳入的參數(shù)。

<?php
/**
 * @Notes: 郵件發(fā)送校驗(yàn)器
 * @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;
 }
 // 創(chuàng)建驗(yàn)證器對(duì)象
 public static function make($type)
 {
     return new self($type);
 }
 // 與實(shí)際傳入的參數(shù)做校驗(yàn)
 public function check($params = [])
 {
     if (empty($params)) {
         $this->error = "參數(shù)不足,非法請求";
     }
     $this->error = call_user_func([new self($this->type),$this->type],$params);
     return $this->error ? false : true;
 }
 // 發(fā)送參數(shù)校驗(yàn)
 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 "郵箱格式不正確";
         }
     }
     // 郵件標(biāo)題
     if (!isset($params[1]) || !$params[1]) {
         return "郵件標(biāo)題不能為空";
     }
     if (!isset($params[2]) || !$params[2]) {
         return "郵件內(nèi)容不能為空";
     }
     return $res;
 }
 // 獲取錯(cuò)誤信息
 public function getError()
 {
     return $this->error;
 }
}
  • 實(shí)際的郵件發(fā)送,需要使用phpmail庫。
<?php
/**
 * @Notes: 郵件實(shí)際發(fā)送
 * @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: 發(fā)郵件
     * @Author: bqs
     * @Time: 2020/8/31 13:07
     * @Interface send
     * @Return mixed
     */
    public function send($params)
    {
        $to = $params[0];                       // 接收者
        $subject = $params[1];                  // 郵件標(biāo)題
        $content = $params[2];                  // 郵件內(nèi)容
        $emails = new PHPMailer();
        $emails->CharSet = 'UTF-8'; //設(shè)定郵件編碼,默認(rèn)ISO-8859-1,如果發(fā)中文此項(xiàng)必須設(shè)置,否則亂碼
        $emails->isSMTP();
        //Enable SMTP debugging
        // 0 = off (for production use)
        // 1 = client messages
        // 2 = client and server messages
        $emails->SMTPDebug = 0;
        //調(diào)試輸出格式
        //$emails->Debugoutput = 'html';
        //smtp服務(wù)器
        $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';// 使用安全協(xié)議
        //Whether to use SMTP authentication
        $emails->SMTPAuth = true;
        //發(fā)送郵箱
        $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']);
        //回復(fù)地址
        //$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
        //標(biāo)題
        $emails->Subject = $subject;
        //HTML內(nèi)容轉(zhuǎn)換
        $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();
    }
}
  • 客戶調(diào)用部分。
public function sendMail()
    {
        try {
            $res = \mail\MailFacade::send(["1641181271@qq.com"], "測試標(biāo)題", "測試內(nèi)容");
            var_dump($res);
            die;
        } catch (MailException $e) {                // 捕捉自定義異常類拋出
            var_dump($e->errorMessage());
            die;
        } catch (\Exception $e) {
            var_dump($e->getMessage());
            die;
        }
    }
  • 返回true后查看郵件是否接收。 

環(huán)境要求:

實(shí)現(xiàn)郵件發(fā)送是需要特定的環(huán)境和相關(guān)的配置才能實(shí)現(xiàn),以下就以實(shí)現(xiàn)成功發(fā)送補(bǔ)充的操作。 

第一步:打開網(wǎng)址下載PHPMailer,PHPMailer 需要 PHP 的 sockets 擴(kuò)展支持,而登錄 QQ 郵箱 SMTP 服務(wù)器則必須通過 SSL 加密的, PHP 還得包含 openssl 的支持。

 第二步:使用 phpinfo() 函數(shù)查看 socket 和 openssl 擴(kuò)展信息(wamp server 默認(rèn)啟用了該擴(kuò)展)。openssl 如果沒有開啟請打開php.ini文件進(jìn)行開啟首先檢查php.ini中;extension=php_openssl.dll是否存在, 如果存在的話去掉前面的注釋符‘;’, 如果不存在這行,那么添加extension=php_openssl.dll。

PHPMailer 核心文件

 第三步:**QQ 郵箱設(shè)置所有的主流郵箱都支持 SMTP 協(xié)議,但并非所有郵箱都默認(rèn)開啟,您可以在郵箱的設(shè)置里面手動(dòng)開啟。第三方服務(wù)在提供了賬號(hào)和密碼之后就可以登錄 SMTP 服務(wù)器,通過它來控制郵件的中轉(zhuǎn)方式。 

第四步:開啟 SMTP 服務(wù)

選擇 IMAP/SMTP 服務(wù),點(diǎn)擊開啟服務(wù) 第五步:驗(yàn)證密保

發(fā)送短信“配置郵件客戶端”至1069-0700-69 第六步:獲取授權(quán)碼

SMTP 服務(wù)器認(rèn)證密碼,也就是授權(quán)碼,使用的時(shí)候沒有空格,需要妥善保管。

以上就是PHP門面模式實(shí)現(xiàn)簡單的郵件發(fā)送示例的詳細(xì)內(nèi)容,更多關(guān)于PHP門面模式發(fā)送郵件的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • PHP實(shí)現(xiàn)將HTML5中Canvas圖像保存到服務(wù)器的方法

    PHP實(shí)現(xiàn)將HTML5中Canvas圖像保存到服務(wù)器的方法

    這篇文章主要介紹了PHP實(shí)現(xiàn)將HTML5中Canvas圖像保存到服務(wù)器的方法,可實(shí)現(xiàn)將Canvas圖像保存到服務(wù)器的功能,是非常實(shí)用的技巧,需要的朋友可以參考下
    2014-11-11
  • Search File Contents PHP 搜索目錄文本內(nèi)容的代碼

    Search File Contents PHP 搜索目錄文本內(nèi)容的代碼

    這個(gè)類可以用來搜索在給定的文本目錄中的文件。它可以給定目錄遍歷遞歸查找某些文件擴(kuò)展名的文件。
    2010-02-02
  • 對(duì)于PHP 5.4 你必須要知道的

    對(duì)于PHP 5.4 你必須要知道的

    PHP 5.4.0 性能大幅提升, 修復(fù)超過100個(gè)bug. 廢除了register_globals, magic_quotes以及安全模式
    2013-08-08
  • 基于php使用memcache存儲(chǔ)session的詳解

    基于php使用memcache存儲(chǔ)session的詳解

    本篇文章是對(duì)php使用memcache存儲(chǔ)session進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • php校驗(yàn)公鑰是否可用的實(shí)例方法

    php校驗(yàn)公鑰是否可用的實(shí)例方法

    在本文里小編給大家整理的是一篇關(guān)于php校驗(yàn)公鑰是否可用的知識(shí)點(diǎn)內(nèi)容,需要的朋友們參考下。
    2019-09-09
  • PHP array操作10個(gè)小技巧分享

    PHP array操作10個(gè)小技巧分享

    其實(shí)任何一門計(jì)算機(jī)語言中對(duì)array(數(shù)組)的操作都是一門學(xué)問,PHP也不例外。下面筆者想向各位介紹關(guān)于PHP中array操作的10個(gè)小技巧及相關(guān)的函數(shù)。
    2011-06-06
  • PHP使用finfo_file()函數(shù)檢測上傳圖片類型的實(shí)現(xiàn)方法

    PHP使用finfo_file()函數(shù)檢測上傳圖片類型的實(shí)現(xiàn)方法

    這篇文章主要介紹了PHP使用finfo_file()函數(shù)檢測上傳圖片類型的實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了finfo_file()函數(shù)的功能、使用方法及相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2017-04-04
  • php實(shí)現(xiàn)的SESSION類

    php實(shí)現(xiàn)的SESSION類

    這篇文章主要介紹了php實(shí)現(xiàn)的SESSION類,包括session的創(chuàng)建、初始化、讀取、寫入與銷毀等常用的操作,是非常實(shí)用的技巧,需要的朋友可以參考下
    2014-12-12
  • php接口報(bào)錯(cuò)解決分析記錄

    php接口報(bào)錯(cuò)解決分析記錄

    記一次解決php接口報(bào)錯(cuò) The GET method is not supported for this route. Supported methods: POST.的bug,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-10-10
  • PHP中遇到的時(shí)區(qū)問題解決方法

    PHP中遇到的時(shí)區(qū)問題解決方法

    最近,在用PHP+MySQL編寫一個(gè)小程序的時(shí)候,發(fā)現(xiàn)一個(gè)問題:錄入數(shù)據(jù)庫的時(shí)間與實(shí)際時(shí)間差8小時(shí),這和中國位于的東8區(qū)是符合的。由于數(shù)據(jù)庫時(shí)間是由PHP寫入的,那問題就出現(xiàn)在PHP中了。
    2015-07-07

最新評(píng)論

舟曲县| 林口县| 晴隆县| 岳西县| 平南县| 巴中市| 桓仁| 新巴尔虎右旗| 漯河市| 电白县| 安丘市| 神农架林区| 晋州市| 青河县| 隆德县| 南充市| 禹州市| 大石桥市| 金坛市| 正蓝旗| 英吉沙县| 台中市| 兴宁市| 海林市| 贞丰县| 瓦房店市| 玉树县| 滁州市| 定兴县| 灵台县| 蕲春县| 丰台区| 毕节市| 大同市| 邵阳县| 昌吉市| 长兴县| 柯坪县| 行唐县| 抚松县| 吉木萨尔县|