使用nette/mail 封裝一個發送郵件類 (通用)

使用nette/mail 封裝一個發送郵件類 (通用)

使用到的包 

composer require nette/mail

封裝Mail

<?php
/**
* Created by PhpStorm.
* User: 鄧塵鋒
* Date: 19-7-5
* Time: 上午11:57
* surest.cn
*/

namespace app\common\server;


use Nette\InvalidArgumentException;
use Nette\Mail\Message;

class Mail extends Message

{

    public $config;

    // [String] e-mail

    protected $from;

    // [Array] e-mail list

    protected $to;

    protected $title;

    protected $body;

    public function __construct($to)
    {
        $host = config('email.username');
        $this->setFrom("{$host}", "D88科技")
            ->setHeader("name", $host);

        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;
    }

}

封裝Mailer發送類

    <?php
    /**
    * Created by PhpStorm.
    * User: chenf
    * Date: 19-7-16
    * Time: 下午3:35
    */

    namespace app\common\server;

    use Nette\Mail\Message;
    use Nette\Mail\SmtpMailer;

    /**
    *
    * 使用:
    * $mail = Mail::to($emails)->title("錯誤預警")->content($html);
    * Mailer::setMailer()->send($mail);
    *
    * Class Mailer
    * @package app\common\server
    */
    class Mailer
    {
        /**
        * 實例化一個Mailer類
        * @return SmtpMailer
        */
        public static function setMailer()
        {
            # 這裏的配置讀取的是config配置
            $mailer = new SmtpMailer([
                'host' => config('email.host'),
                'username' => config('email.username'),
                'password' => config('email.password'),
                'secure' => config('email.secure')
            ]);
            return $mailer;
        }

        /**
        * 發送
        * @param Message $mail
        */
        public function send(Message $mail)
        {
            $this->send($mail);
        }
    }

配置

'host' => 'smtp.exmail.qq.com', // 用的是qq的smtp服務器
'username' => 'username', 
'password' => 'password', 
'secure' => 'ssl' // ssl 是 445 端口, 如不設置, 默認端口是 22 , 可參見源碼

使用

// $emails 是一個數組
// Mail Message 體
$mail = Mail::to($emails)->title("錯誤預警")->content($html);

// 發送
Mailer::setMailer()->send($mail);

告知

如果直接使用如上方法, 採用的是同步發送的機制, 如果需要採用異步隊列進行發送郵件, 我提供如下解決思路

  • 使用redis
  • 數據+key 寫入 hash
  • key 寫入 list
  • 創建一個定時任務, 去除list key, 再去除hash數據, 進行發送
瞭解一下 , 生產者消費者模式
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章