利用 Composer 完善本身的 PHP 框架(二)——發送郵件

說明

原文發表在個人我的網站:利用 Composer 完善本身的 PHP 框架(二)——發送郵件php

本教程示例代碼見 https://github.com/johnlui/My-First-Framework-based-on-Composerhtml

回顧

上一篇文章中,咱們手工建造了一個簡易的視圖加載器,順便引入了錯誤處理包,讓咱們的 MFFC 框架在 M、V、C 三個方面都達到了「好用」的水平。View 是一個可插拔組件,在本篇文章中咱們要建立另外一個可插拔組件——郵件發送模塊。git

正文

咱們採用 nette/mail 包做爲咱們的郵件發送基礎模塊,在它的基礎上封裝一個 Mail 類,暴露出簡潔的 API 給控制器使用,下面咱們正式開始。github

引入 nette/mail 包,修改 composer.jsonjson

"require": {
  "codingbean/macaw": "dev-master",
  "illuminate/database": "*",
  "filp/whoops": "*",
  "nette/mail": "*"
},

運行 composer update,等待安裝完成。nette/mail 的文檔位於:http://doc.nette.org/en/2.2/mailing 讓咱們閱讀它,而後設計 Mail 類:數組

新建 services/Mail.php 文件,內容以下:緩存

<?php

use Nette\Mail\Message;

/**
* \Mail
*/
class Mail extends Message
{
  public $config;

  // [String] e-mail
  protected $from;
  // [Array] e-mail list
  protected $to;

  protected $title;
  protected $body;

  function __construct($to)
  {
    $this->config = require BASE_PATH.'/config/mail.php';

    $this->setFrom($this->config['username']);

    if ( is_array($to) ) {
      foreach ($to as $email) {
        $this->addTo($email);
      }
    } else {
      $this->addTo($to);
    }
  }

  public function from($from=null)
  {
    if ( !$from ) {
      throw new InvalidArgumentException("郵件發送地址不能爲空!");
    }
    $this->setFrom($from);
    return $this;
  }

  public static function to($to=null)
  {
    if ( !$to ) {
      throw new InvalidArgumentException("郵件接收地址不能爲空!");
    }
    return new Mail($to);
  }

  public function title($title=null)
  {
    if ( !$title ) {
      throw new InvalidArgumentException("郵件標題不能爲空!");
    }
    $this->setSubject($title);
    return $this;
  }

  public function content($content=null)
  {
    if ( !$content ) {
      throw new InvalidArgumentException("郵件內容不能爲空!");
    }
    $this->setHTMLBody($content);
    return $this;
  }
}

Mail 類和 View 類工做的方式基本一致:composer

$this->mail = Mail::to(['ooxx@gmail.com', 'ooxx@qq.com'])
                    ->from('MotherFucker <ooxx@163.com>')
                    ->title('Fuck Me!')
                    ->content('<h1>Hello~~</h1>');

上面這段代碼位於 HomeController 中, View::make() 那行代碼的下面。框架

新建 MFFC/config/mail.php,請自行替換郵件地址和密碼:異步

<?php

return [
  'host' => 'smtp.163.com',
  'username' => 'ooxx@163.com',
  'password' => 'password',
  'secure' => ''
];

Mail 和 View 同樣也在 BaseController 的析構函數 __destruct() 函數中處理,如今這個 function 長這樣:

public function __destruct()
{
  $view = $this->view;
  if ( $view instanceof View ) {
    extract($view->data);
    require $view->view;
  }

  $mail = $this->mail;
  if ( $mail instanceof Mail ) {
    $mailer = new Nette\Mail\SmtpMailer($mail->config);
    $mailer->send($mail);
  }
}

OK,準備的差很少了,運行 composer dump-autoload 把 Mail 類加入自動加載,刷新頁面!

pic

若是你看到以上頁面,恭喜你!郵件發送成功了!

趕快去檢查一下收件箱有木有郵件!:-D 此次頁面加載可能會稍慢,由於郵件是同步發送的。異步的隊列系統咱們會在之後講到。

分析

郵件發送的總體流程想必你們已經輕車熟路了,如今主要敘述一下 Mail 類的設計過程:

  1. 郵件發送的核心參數是 目標地址,即郵件要發送到的 E-mail 地址,因此咱們設計 Mail::to('oo@xx.me') 做爲發送的 觸發 API
  2. 目前咱們採用最簡單的 SMTP 方式發送郵件,文檔在 這裏。配置文件放置在 MFFC/config/mail.php 中,依舊返回一個數組。
  3. Mail 類繼承了 Nette\Mail\Message 類。Mail::to() 的時候建立一個 Mail 類的實例(對象)並返回,這時候其實 BaseController 中的析構函數中的代碼已經會被觸發並處理這個對象了。默認的發送人是從配置文件中讀取的 username
  4. Mail::to() 支持 字符串 或者數組做爲參數,能夠一次發送一封或多封郵件。
  5. from()title()content() 方法用於豐富郵件內容。content() 方法能夠直接傳遞 HTML 代碼。
  6. from() 配置不必定都可以成功,部分郵件服務商不支持修改發送人地址。
  7. 這個變量所有組裝完成後,被賦值給控制器的 $mail 成員變量,而後被析構函數處理,郵件被髮送,成功後頁面代碼被髮送回客戶端,流程結束。

預告

下一步:利用 Composer 完善本身的 PHP 框架(三)——Redis 緩存(待更新)

相關文章
相關標籤/搜索