說一說 Laravel 郵件傳送流程

coding01發表於2018-08-26

當我使用 Laravel 的郵件傳送功能時,腦子裡浮現出這麼幾個問題:

  1. Laravel 整合了 SMTP 、Mailgun 、SparkPost 、 Amazon SES 等驅動,是怎麼做到的?

  2. Laravel 提供全文字格式、網頁格式和 Markdown 格式,是怎麼實現的?

  3. 整個郵件傳送流程是什麼樣的?

下面就讓我們開始徒手扒一扒「郵件傳送功能」的實現原理。

寫個 demo

我們使用阿里雲提供的免費郵,和採用「smtp」驅動,作為測試,參考 .env 配置:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mxhichina.com
MAIL_PORT=25
MAIL_USERNAME=***@coding01.cn
MAIL_PASSWORD=****
MAIL_ENCRYPTION=tls
MAIL_FROM=***@coding01.cn
MAIL_NAME=coding01
複製程式碼

寫個測試流程,還是挺簡單的,具體如下:

// 1. 建立測試類
php artisan make:mail TestEmail

// 2. 在 TestEmail 類,載入檢視

public function build()
{
    return $this->view('mail.test');
}

// 3. 輸出 hello coding01

<p>hello coding01</p>
複製程式碼

最後寫個命令函式:

Artisan::command('test', function () {
    Mail::to('yemeishu@126.com')->send(new \App\Mail\TestEmail());
});
複製程式碼

執行 php artisan test 看測試是否傳送成功:

說一說 Laravel 郵件傳送流程

解析 MailServiceProvider

寫了不少 Laravel 程式碼,看

 Mail::to('yemeishu@126.com')->send(new \App\Mail\TestEmail());
複製程式碼

自然而然的想到是不是有一個 MailServiceProvider,果不其然,在 config/app.php 的陣列 providers 就包含了該 ServiceProvider

說一說 Laravel 郵件傳送流程

所以我們就開始圍繞這個 MailServiceProvider 來解析了

/**
 * Register the service provider.
 *
 * @return void
 */
public function register()
{
    $this->registerSwiftMailer();

    $this->registerIlluminateMailer();

    $this->registerMarkdownRenderer();
}
複製程式碼

register 函式,一目瞭然,我們將重點看看這三個方法都是幹嘛用的。

registerSwiftMailer

看程式碼:

/**
 * Register the Swift Mailer instance.
 *
 * @return void
 */
public function registerSwiftMailer()
{
    $this->registerSwiftTransport();

    // Once we have the transporter registered, we will register the actual Swift
    // mailer instance, passing in the transport instances, which allows us to
    // override this transporter instances during app start-up if necessary.
    $this->app->singleton('swift.mailer', function ($app) {
        if ($domain = $app->make('config')->get('mail.domain')) {
            Swift_DependencyContainer::getInstance()
                            ->register('mime.idgenerator.idright')
                            ->asValue($domain);
        }

        return new Swift_Mailer($app['swift.transport']->driver());
    });
}
複製程式碼

很好理解,就是註冊 Swift Mailer 例項。在建立例項之前,執行 $this->registerSwiftTransport();方法:

/**
 * Register the Swift Transport instance.
 *
 * @return void
 */
protected function registerSwiftTransport()
{
    $this->app->singleton('swift.transport', function ($app) {
        return new TransportManager($app);
    });
}
複製程式碼

看看這個 TransportManager 類是幹嘛用的:

<?php

namespace Illuminate\Mail;

use Aws\Ses\SesClient;
use Illuminate\Support\Arr;
use Psr\Log\LoggerInterface;
use Illuminate\Support\Manager;
use GuzzleHttp\Client as HttpClient;
use Swift_SmtpTransport as SmtpTransport;
use Illuminate\Mail\Transport\LogTransport;
use Illuminate\Mail\Transport\SesTransport;
use Illuminate\Mail\Transport\ArrayTransport;
use Swift_SendmailTransport as MailTransport;
use Illuminate\Mail\Transport\MailgunTransport;
use Illuminate\Mail\Transport\MandrillTransport;
use Illuminate\Mail\Transport\SparkPostTransport;
use Swift_SendmailTransport as SendmailTransport;

class TransportManager extends Manager
{
    /**
     * Create an instance of the SMTP Swift Transport driver.
     *
     * @return \Swift_SmtpTransport
     */
    protected function createSmtpDriver()
    {
        $config = $this->app->make('config')->get('mail');

        // The Swift SMTP transport instance will allow us to use any SMTP backend
        // for delivering mail such as Sendgrid, Amazon SES, or a custom server
        // a developer has available. We will just pass this configured host.
        $transport = new SmtpTransport($config['host'], $config['port']);

        if (isset($config['encryption'])) {
            $transport->setEncryption($config['encryption']);
        }

        // Once we have the transport we will check for the presence of a username
        // and password. If we have it we will set the credentials on the Swift
        // transporter instance so that we'll properly authenticate delivery.
        if (isset($config['username'])) {
            $transport->setUsername($config['username']);

            $transport->setPassword($config['password']);
        }

        // Next we will set any stream context options specified for the transport
        // and then return it. The option is not required any may not be inside
        // the configuration array at all so we'll verify that before adding.
        if (isset($config['stream'])) {
            $transport->setStreamOptions($config['stream']);
        }

        return $transport;
    }

    /**
     * Create an instance of the Sendmail Swift Transport driver.
     *
     * @return \Swift_SendmailTransport
     */
    protected function createSendmailDriver()
    {
        return new SendmailTransport($this->app['config']['mail']['sendmail']);
    }

    /**
     * Create an instance of the Amazon SES Swift Transport driver.
     *
     * @return \Illuminate\Mail\Transport\SesTransport
     */
    protected function createSesDriver()
    {
        $config = array_merge($this->app['config']->get('services.ses', []), [
            'version' => 'latest', 'service' => 'email',
        ]);

        return new SesTransport(new SesClient(
            $this->addSesCredentials($config)
        ));
    }

    /**
     * Add the SES credentials to the configuration array.
     *
     * @param  array  $config
     * @return array
     */
    protected function addSesCredentials(array $config)
    {
        if ($config['key'] && $config['secret']) {
            $config['credentials'] = Arr::only($config, ['key', 'secret']);
        }

        return $config;
    }

    /**
     * Create an instance of the Mail Swift Transport driver.
     *
     * @return \Swift_SendmailTransport
     */
    protected function createMailDriver()
    {
        return new MailTransport;
    }

    /**
     * Create an instance of the Mailgun Swift Transport driver.
     *
     * @return \Illuminate\Mail\Transport\MailgunTransport
     */
    protected function createMailgunDriver()
    {
        $config = $this->app['config']->get('services.mailgun', []);

        return new MailgunTransport(
            $this->guzzle($config),
            $config['secret'], $config['domain']
        );
    }

    /**
     * Create an instance of the Mandrill Swift Transport driver.
     *
     * @return \Illuminate\Mail\Transport\MandrillTransport
     */
    protected function createMandrillDriver()
    {
        $config = $this->app['config']->get('services.mandrill', []);

        return new MandrillTransport(
            $this->guzzle($config), $config['secret']
        );
    }

    /**
     * Create an instance of the SparkPost Swift Transport driver.
     *
     * @return \Illuminate\Mail\Transport\SparkPostTransport
     */
    protected function createSparkPostDriver()
    {
        $config = $this->app['config']->get('services.sparkpost', []);

        return new SparkPostTransport(
            $this->guzzle($config), $config['secret'], $config['options'] ?? []
        );
    }

    /**
     * Create an instance of the Log Swift Transport driver.
     *
     * @return \Illuminate\Mail\Transport\LogTransport
     */
    protected function createLogDriver()
    {
        return new LogTransport($this->app->make(LoggerInterface::class));
    }

    /**
     * Create an instance of the Array Swift Transport Driver.
     *
     * @return \Illuminate\Mail\Transport\ArrayTransport
     */
    protected function createArrayDriver()
    {
        return new ArrayTransport;
    }

    /**
     * Get a fresh Guzzle HTTP client instance.
     *
     * @param  array  $config
     * @return \GuzzleHttp\Client
     */
    protected function guzzle($config)
    {
        return new HttpClient(Arr::add(
            $config['guzzle'] ?? [], 'connect_timeout', 60
        ));
    }

    /**
     * Get the default mail driver name.
     *
     * @return string
     */
    public function getDefaultDriver()
    {
        return $this->app['config']['mail.driver'];
    }

    /**
     * Set the default mail driver name.
     *
     * @param  string  $name
     * @return void
     */
    public function setDefaultDriver($name)
    {
        $this->app['config']['mail.driver'] = $name;
    }
}
複製程式碼

通過觀察,可以看出,TransportManager 主要是為了建立各種驅動:

  • Smtp —— 建立 Swift_SmtpTransport 例項物件,主要使用的引數為:hostportencryptionusernamepasswordstream

  • SendmailMail —— 建立 Swift_SendmailTransport 例項物件,使用的引數為:sendmail

  • Ses —— 建立 SesTransport 例項物件,使用的引數為 config/services 下對應的值:

'ses' => [
複製程式碼
'key' => env('SES_KEY'),
'secret' => env('SES_SECRET'),
'region' => 'us-east-1',
複製程式碼

],


- `Mailgun` —— 建立 `MailgunTransport` 例項物件,使用的引數為 `config/services` 下對應的值:

複製程式碼

'mailgun' => [ 'domain' => env('MAILGUN_DOMAIN'), 'secret' => env('MAILGUN_SECRET'), ],


- `Mandrill` —— 建立 `MandrillTransport` 例項物件,使用的引數為 `config/services` 下對應的值:「暫無」,可以自行新增

- `SparkPost` —— 建立 `SparkPostTransport` 例項物件,使用的引數為 `config/services` 下對應的值:

複製程式碼

'sparkpost' => [ 'secret' => env('SPARKPOST_SECRET'), ],


此外,就是建立 `Log` 驅動,和設定預設的驅動,由 `app['config']['mail.driver']` 決定的。

複製程式碼

通過上文,我們還可以看出在使用 MailgunMandrill 或者 SparkPost 都需要使用外掛 guzzle,這也是為什麼官網提示要安裝 guzzle 外掛的原因了:

說一說 Laravel 郵件傳送流程

同時,這些驅動類都是 extends Illuminate\Mail\Transport,而且抽象類 Transport 是實現 Swift_Transport 介面:

<?php

/*
 * This file is part of SwiftMailer.
 * (c) 2004-2009 Chris Corbyn
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 * Sends Messages via an abstract Transport subsystem.
 *
 * @author Chris Corbyn
 */
interface Swift_Transport
{
    /**
     * Test if this Transport mechanism has started.
     *
     * @return bool
     */
    public function isStarted();

    /**
     * Start this Transport mechanism.
     */
    public function start();

    /**
     * Stop this Transport mechanism.
     */
    public function stop();

    /**
     * Check if this Transport mechanism is alive.
     *
     * If a Transport mechanism session is no longer functional, the method
     * returns FALSE. It is the responsibility of the developer to handle this
     * case and restart the Transport mechanism manually.
     *
     * @example
     *
     *   if (!$transport->ping()) {
     *      $transport->stop();
     *      $transport->start();
     *   }
     *
     * The Transport mechanism will be started, if it is not already.
     *
     * It is undefined if the Transport mechanism attempts to restart as long as
     * the return value reflects whether the mechanism is now functional.
     *
     * @return bool TRUE if the transport is alive
     */
    public function ping();

    /**
     * Send the given Message.
     *
     * Recipient/sender data will be retrieved from the Message API.
     * The return value is the number of recipients who were accepted for delivery.
     *
     * @param Swift_Mime_SimpleMessage $message
     * @param string[]           $failedRecipients An array of failures by-reference
     *
     * @return int
     */
    public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null);

    /**
     * Register a plugin in the Transport.
     *
     * @param Swift_Events_EventListener $plugin
     */
    public function registerPlugin(Swift_Events_EventListener $plugin);
}
複製程式碼

我們利用 PhpStorm 檢視有多少類實現該介面:

說一說 Laravel 郵件傳送流程

好了,有了建立驅動的例項,接下來就是建立 Swift_Mailer 物件例項了:

$this->app->singleton('swift.mailer', function ($app) {
    
    ...
     
    return new Swift_Mailer($app['swift.transport']->driver());
});
複製程式碼

下面藉助 $app['swift.transport']->driver() 函式來說一說怎麼拿到我們指定的驅動。

TransportManager 的父類 Manager 抽象類找到driver() 函式:

/**
 * Get the default driver name.
 *
 * @return string
 */
abstract public function getDefaultDriver();

/**
 * Get a driver instance.
 *
 * @param  string  $driver
 * @return mixed
 */
public function driver($driver = null)
{
    $driver = $driver ?: $this->getDefaultDriver();

    if (is_null($driver)) {
        throw new InvalidArgumentException(sprintf(
            'Unable to resolve NULL driver for [%s].', static::class
        ));
    }

    // If the given driver has not been created before, we will create the instances
    // here and cache it so we can return it next time very quickly. If there is
    // already a driver created by this name, we'll just return that instance.
    if (! isset($this->drivers[$driver])) {
        $this->drivers[$driver] = $this->createDriver($driver);
    }

    return $this->drivers[$driver];
}
複製程式碼

主要的使用各個繼承類 (TransportManager) 實現的 $this->getDefaultDriver()

/**
 * Get the default mail driver name.
 *
 * @return string
 */
public function getDefaultDriver()
{
    return $this->app['config']['mail.driver'];
}
複製程式碼

這就好理解了,指定的驅動是由 config 自主指定的;當拿到驅動名稱後,我們回到 driver() 函式,繼續往下看到程式碼:

if (! isset($this->drivers[$driver])) {
    $this->drivers[$driver] = $this->createDriver($driver);
}

// 注:$this->createDriver($driver) 這才是真正建立指定驅動的方法

/**
 * Create a new driver instance.
 *
 * @param  string  $driver
 * @return mixed
 *
 * @throws \InvalidArgumentException
 */
protected function createDriver($driver)
{
    // We'll check to see if a creator method exists for the given driver. If not we
    // will check for a custom driver creator, which allows developers to create
    // drivers using their own customized driver creator Closure to create it.
    if (isset($this->customCreators[$driver])) {
        return $this->callCustomCreator($driver);
    } else {
        $method = 'create'.Str::studly($driver).'Driver';

        if (method_exists($this, $method)) {
            return $this->$method();
        }
    }
    throw new InvalidArgumentException("Driver [$driver] not supported.");
}
複製程式碼

當然我們的目標就定在這裡:

$method = 'create'.Str::studly($driver).'Driver';

if (method_exists($this, $method)) {
    return $this->$method();
}
複製程式碼

通過拿到的「驅動名稱」,拼接成函式名,假如我們的驅動名稱為:mailgun,則函式名:createMailgunDriver,然後就可以直接執行該方法,拿到對應的驅動物件例項了。

注:推薦看看這個 Str::studly($driver) 函式原始碼

到此,我們知道了如何利用 config 配置檔案,來建立指定的驅動器,最後建立 Swift_Mailer 物件,以供之後執行使用。

registerIlluminateMailer

看程式碼:

/**
 * Register the Illuminate mailer instance.
 *
 * @return void
 */
protected function registerIlluminateMailer()
{
    $this->app->singleton('mailer', function ($app) {
        $config = $app->make('config')->get('mail');

        // Once we have create the mailer instance, we will set a container instance
        // on the mailer. This allows us to resolve mailer classes via containers
        // for maximum testability on said classes instead of passing Closures.
        $mailer = new Mailer(
            $app['view'], $app['swift.mailer'], $app['events']
        );

        if ($app->bound('queue')) {
            $mailer->setQueue($app['queue']);
        }

        // Next we will set all of the global addresses on this mailer, which allows
        // for easy unification of all "from" addresses as well as easy debugging
        // of sent messages since they get be sent into a single email address.
        foreach (['from', 'reply_to', 'to'] as $type) {
            $this->setGlobalAddress($mailer, $config, $type);
        }

        return $mailer;
    });
}
複製程式碼

光看這個,比較簡單,就是傳入 view、第一步建立好的郵件傳送器Swift_Mailer 物件,和 events 事件分發器,如果有佇列,傳入佇列,建立 Illuminate mailer 物件,供我們真正場景使用;最後就是配置全域性引數了。

registerMarkdownRenderer

Laravel 能夠捕獲很多開發者的?,還有一個核心的地方在於:知道開發者想要什麼。其中 Markdown 基本就是開發者的必備。用 Markdown 寫郵件,是一個不錯的方案,下面看看怎麼做到的?

為了扒 Markdown 程式碼,先寫個 demo 看怎麼使用。

使用命令,帶上 --markdown 選項:

php artisan make:mail TestMdEmail --markdown=mail.testmd
複製程式碼

這樣就可以為我們建立了 TestMdEmail

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class TestMdEmail extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->markdown('mail.testmd');
    }
}
複製程式碼

和檢視 testmd.blade.php,預設檢視內容:

@component('mail::message')
# Introduction

The body of your message.

@component('mail::button', ['url' => ''])
Button Text
@endcomponent

Thanks,<br>
{{ config('app.name') }}
@endcomponent
複製程式碼

寫個測試,傳送看看執行效果:

Artisan::command('testmd', function () {
    Mail::to('yemeishu@126.com')->send(new \App\Mail\TestMdEmail());
});
複製程式碼

說一說 Laravel 郵件傳送流程

一切使用預設的,就可以很輕易的建立 markdown 格式的郵件內容,併傳送。

我們可以看看原始碼了:

/**
 * Register the Markdown renderer instance.
 *
 * @return void
 */
protected function registerMarkdownRenderer()
{
    if ($this->app->runningInConsole()) {
        $this->publishes([
            __DIR__.'/resources/views' => $this->app->resourcePath('views/vendor/mail'),
        ], 'laravel-mail');
    }

    $this->app->singleton(Markdown::class, function ($app) {
        $config = $app->make('config');

        return new Markdown($app->make('view'), [
            'theme' => $config->get('mail.markdown.theme', 'default'),
            'paths' => $config->get('mail.markdown.paths', []),
        ]);
    });
}
複製程式碼

目標很簡單,就是利用配置資訊,建立 Markdown 物件,為後續服務。

我們先看預設的 mail config

/*
   |--------------------------------------------------------------------------
| Markdown Mail Settings
|--------------------------------------------------------------------------
|
| If you are using Markdown based email rendering, you may configure your
| theme and component paths here, allowing you to customize the design
| of the emails. Or, you may simply stick with the Laravel defaults!
|
*/

'markdown' => [
    'theme' => 'default',
    'paths' => [
        resource_path('views/vendor/mail'),
    ],
],
複製程式碼

預設的 markdown 配置資訊都存在 views/vendor/mail 資料夾下,我們可以通過命令:

$ php artisan vendor:publish --tag=laravel-mail

Copied Directory [/vendor/laravel/framework/src/Illuminate/Mail/resources/views] To [/resources/views/vendor/mail]
Publishing complete.
複製程式碼

所有的預設元件都存在這個資料夾下,還有頁面的檢視樣式主題等:

說一說 Laravel 郵件傳送流程

*注:*我們可以自定元件和增加發布郵箱的 css 樣式

Maikdown 建構函式:

/**
 * Create a new Markdown renderer instance.
 *
 * @param  \Illuminate\Contracts\View\Factory  $view
 * @param  array  $options
 * @return void
 */
public function __construct(ViewFactory $view, array $options = [])
{
    $this->view = $view;
    $this->theme = $options['theme'] ?? 'default';
    $this->loadComponentsFrom($options['paths'] ?? []);
}
複製程式碼

主要是傳入 View 檢視構造器和主題樣式,以及各個 markdown 元件。

郵件傳送流程

下面我們結合上面的 demo 看看如何構造郵件內容,和傳送郵件的,我們看程式碼:

Mail::to('yemeishu@126.com')->send(new \App\Mail\TestMdEmail());
複製程式碼

這裡的 Mail 就是上面 registerIlluminateMailer 註冊的 Illuminate\Mail\Mailer 物件。

我們且看它的 send() 方法:

/**
 * Send a new message using a view.
 *
 * @param  string|array|MailableContract  $view
 * @param  array  $data
 * @param  \Closure|string  $callback
 * @return void
 */
public function send($view, array $data = [], $callback = null)
{
    if ($view instanceof MailableContract) {
        return $this->sendMailable($view);
    }

    // First we need to parse the view, which could either be a string or an array
    // containing both an HTML and plain text versions of the view which should
    // be used when sending an e-mail. We will extract both of them out here.
    list($view, $plain, $raw) = $this->parseView($view);

    $data['message'] = $message = $this->createMessage();

    // Once we have retrieved the view content for the e-mail we will set the body
    // of this message using the HTML type, which will provide a simple wrapper
    // to creating view based emails that are able to receive arrays of data.
    call_user_func($callback, $message);

    $this->addContent($message, $view, $plain, $raw, $data);

    // If a global "to" address has been set, we will set that address on the mail
    // message. This is primarily useful during local development in which each
    // message should be delivered into a single mail address for inspection.
    if (isset($this->to['address'])) {
        $this->setGlobalTo($message);
    }

    // Next we will determine if the message should be sent. We give the developer
    // one final chance to stop this message and then we will send it to all of
    // its recipients. We will then fire the sent event for the sent message.
    $swiftMessage = $message->getSwiftMessage();

    if ($this->shouldSendMessage($swiftMessage, $data)) {
        $this->sendSwiftMessage($swiftMessage);

        $this->dispatchSentEvent($message, $data);
    }
}
複製程式碼

我們看第一步:

if ($view instanceof MailableContract) {
    return $this->sendMailable($view);
}
複製程式碼

執行的 $this->sendMailable($view)

/**
 * Send the message using the given mailer.
 *
 * @param  \Illuminate\Contracts\Mail\Mailer  $mailer
 * @return void
 */
public function send(MailerContract $mailer)
{
    $translator = Container::getInstance()->make(Translator::class);

    $this->withLocale($this->locale, $translator, function () use ($mailer) {
        Container::getInstance()->call([$this, 'build']);

        $mailer->send($this->buildView(), $this->buildViewData(), function ($message) {
            $this->buildFrom($message)
                 ->buildRecipients($message)
                 ->buildSubject($message)
                 ->runCallbacks($message)
                 ->buildAttachments($message);
        });
    });
}
複製程式碼

核心的在於先執行我們預設 build 方法:

/**
 * Build the message.
 *
 * @return $this
 */
public function build()
{
    return $this->markdown('mail.testmd');
}
複製程式碼

這就是為什麼在命令建立傳送郵件模板類時,都會預設建立該 build 方法了,然後在該方法裡,載入我們的構建內容和邏輯;在 markdown 檢視中,預設的是執行 $this->markdown('mail.testmd'):

/**
 * Set the Markdown template for the message.
 *
 * @param  string  $view
 * @param  array  $data
 * @return $this
 */
public function markdown($view, array $data = [])
{
    $this->markdown = $view;
    $this->viewData = array_merge($this->viewData, $data);

    return $this;
}
複製程式碼

將檢視和檢視內容載入物件中。

然後我們繼續回到上個 send 方法中:

$mailer->send($this->buildView(), $this->buildViewData(), function ($message) {
    $this->buildFrom($message)
         ->buildRecipients($message)
         ->buildSubject($message)
         ->runCallbacks($message)
         ->buildAttachments($message);
});
複製程式碼

我們一個個方法來解析:

$this->buildView()

/**
 * Build the view for the message.
 *
 * @return array|string
 */
protected function buildView()
{
    if (isset($this->html)) {
        return array_filter([
            'html' => new HtmlString($this->html),
            'text' => isset($this->textView) ? $this->textView : null,
        ]);
    }

    if (isset($this->markdown)) {
        return $this->buildMarkdownView();
    }

    if (isset($this->view, $this->textView)) {
        return [$this->view, $this->textView];
    } elseif (isset($this->textView)) {
        return ['text' => $this->textView];
    }

    return $this->view;
}
複製程式碼

很顯然,執行 $this->buildMarkdownView()

/**
 * Build the Markdown view for the message.
 *
 * @return array
 */
protected function buildMarkdownView()
{
    $markdown = Container::getInstance()->make(Markdown::class);

    if (isset($this->theme)) {
        $markdown->theme($this->theme);
    }

    $data = $this->buildViewData();

    return [
        'html' => $markdown->render($this->markdown, $data),
        'text' => $this->buildMarkdownText($markdown, $data),
    ];
}
複製程式碼

這時候,Markdown 物件就派上用場了,目標該放在這兩個方法上了:

return [
    'html' => $markdown->render($this->markdown, $data),
    'text' => $this->buildMarkdownText($markdown, $data),
];
複製程式碼

$markdown->render() 方法:

/**
 * Render the Markdown template into HTML.
 *
 * @param  string  $view
 * @param  array  $data
 * @param  \TijsVerkoyen\CssToInlineStyles\CssToInlineStyles|null  $inliner
 * @return \Illuminate\Support\HtmlString
 */
public function render($view, array $data = [], $inliner = null)
{
    $this->view->flushFinderCache();

    $contents = $this->view->replaceNamespace(
        'mail', $this->htmlComponentPaths()
    )->make($view, $data)->render();

    return new HtmlString(($inliner ?: new CssToInlineStyles)->convert(
        $contents, $this->view->make('mail::themes.'.$this->theme)->render()
    ));
}
複製程式碼

$markdown->renderText() 方法:


/**
 * Render the Markdown template into HTML.
 *
 * @param  string  $view
 * @param  array  $data
 * @return \Illuminate\Support\HtmlString
 */
public function renderText($view, array $data = [])
{
    $this->view->flushFinderCache();

    $contents = $this->view->replaceNamespace(
        'mail', $this->markdownComponentPaths()
    )->make($view, $data)->render();

    return new HtmlString(
        html_entity_decode(preg_replace("/[\r\n]{2,}/", "\n\n", $contents), ENT_QUOTES, 'UTF-8')
    );
}
複製程式碼

主要的邏輯,就是將 markdown 格式轉變成 html 格式,以及構成陣列 ['html', 'data'] 輸出,最後再次執行 send 方法,並傳入閉包函式,供構建 message 服務:

$mailer->send($this->buildView(), $this->buildViewData(), function ($message) {
    $this->buildFrom($message)
         ->buildRecipients($message)
         ->buildSubject($message)
         ->runCallbacks($message)
         ->buildAttachments($message);
});
複製程式碼

我們回頭再看 send 方法,未解析的程式碼:

// First we need to parse the view, which could either be a string or an array
    // containing both an HTML and plain text versions of the view which should
    // be used when sending an e-mail. We will extract both of them out here.
    list($view, $plain, $raw) = $this->parseView($view);

    $data['message'] = $message = $this->createMessage();

    // Once we have retrieved the view content for the e-mail we will set the body
    // of this message using the HTML type, which will provide a simple wrapper
    // to creating view based emails that are able to receive arrays of data.
    call_user_func($callback, $message);

    $this->addContent($message, $view, $plain, $raw, $data);

    // If a global "to" address has been set, we will set that address on the mail
    // message. This is primarily useful during local development in which each
    // message should be delivered into a single mail address for inspection.
    if (isset($this->to['address'])) {
        $this->setGlobalTo($message);
    }

    // Next we will determine if the message should be sent. We give the developer
    // one final chance to stop this message and then we will send it to all of
    // its recipients. We will then fire the sent event for the sent message.
    $swiftMessage = $message->getSwiftMessage();

    if ($this->shouldSendMessage($swiftMessage, $data)) {
        $this->sendSwiftMessage($swiftMessage);

        $this->dispatchSentEvent($message, $data);
    }
複製程式碼

第一步無非就是將上面的陣列遍歷出來,然後再建立 Message 物件:

$data['message'] = $message = $this->createMessage();

/**
 * Create a new message instance.
 *
 * @return \Illuminate\Mail\Message
 */
protected function createMessage()
{
    $message = new Message($this->swift->createMessage('message'));

    // If a global from address has been specified we will set it on every message
    // instance so the developer does not have to repeat themselves every time
    // they create a new message. We'll just go ahead and push this address.
    if (! empty($this->from['address'])) {
        $message->from($this->from['address'], $this->from['name']);
    }

    // When a global reply address was specified we will set this on every message
    // instance so the developer does not have to repeat themselves every time
    // they create a new message. We will just go ahead and push this address.
    if (! empty($this->replyTo['address'])) {
        $message->replyTo($this->replyTo['address'], $this->replyTo['name']);
    }

    return $message;
}
複製程式碼

這個 Message 建構函式傳入的 swift 服務物件,以後通過 message 傳入的資料,都是傳給 swift 服務物件。

$message = new Message($this->swift->createMessage('message'));

...

/**
 * Create a new class instance of one of the message services.
 *
 * For example 'mimepart' would create a 'message.mimepart' instance
 *
 * @param string $service
 *
 * @return object
 */
public function createMessage($service = 'message')
{
    return Swift_DependencyContainer::getInstance()
        ->lookup('message.'.$service);
}
複製程式碼

如:

/**
 * Add a "from" address to the message.
 *
 * @param  string|array  $address
 * @param  string|null  $name
 * @return $this
 */
public function from($address, $name = null)
{
    $this->swift->setFrom($address, $name);

    return $this;
}

/**
 * Set the "sender" of the message.
 *
 * @param  string|array  $address
 * @param  string|null  $name
 * @return $this
 */
public function sender($address, $name = null)
{
    $this->swift->setSender($address, $name);

    return $this;
}
複製程式碼

這樣,我們就開始使用 MailServiceProvider 中建立的 Swift_Mailer 物件了。

好了,終於到最後一個步驟了:

// Next we will determine if the message should be sent. We give the developer
    // one final chance to stop this message and then we will send it to all of
    // its recipients. We will then fire the sent event for the sent message.
    $swiftMessage = $message->getSwiftMessage();

    if ($this->shouldSendMessage($swiftMessage, $data)) {
        $this->sendSwiftMessage($swiftMessage);

        $this->dispatchSentEvent($message, $data);
    }
複製程式碼

獲取 swift 服務物件,然後開始執行傳送邏輯,和分發傳送郵件事件了。

/**
 * Send a Swift Message instance.
 *
 * @param  \Swift_Message  $message
 * @return void
 */
protected function sendSwiftMessage($message)
{
    try {
        return $this->swift->send($message, $this->failedRecipients);
    } finally {
        $this->forceReconnection();
    }
}

...

/**
 * Dispatch the message sent event.
 *
 * @param  \Illuminate\Mail\Message  $message
 * @param  array  $data
 * @return void
 */
protected function dispatchSentEvent($message, $data = [])
{
    if ($this->events) {
        $this->events->dispatch(
            new Events\MessageSent($message->getSwiftMessage(), $data)
        );
    }
}
複製程式碼

繼續看如何利用 swift 物件傳送郵件。

/**
 * Send a Swift Message instance.
 *
 * @param  \Swift_Message  $message
 * @return void
 */
protected function sendSwiftMessage($message)
{
    try {
        return $this->swift->send($message, $this->failedRecipients);
    } finally {
        $this->forceReconnection();
    }
}
複製程式碼

$this->swift->send() 方法:

/**
 * Send the given Message like it would be sent in a mail client.
 *
 * All recipients (with the exception of Bcc) will be able to see the other
 * recipients this message was sent to.
 *
 * Recipient/sender data will be retrieved from the Message object.
 *
 * The return value is the number of recipients who were accepted for
 * delivery.
 *
 * @param Swift_Mime_SimpleMessage $message
 * @param array                    $failedRecipients An array of failures by-reference
 *
 * @return int The number of successful recipients. Can be 0 which indicates failure
 */
public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null)
{
    $failedRecipients = (array) $failedRecipients;

    if (!$this->transport->isStarted()) {
        $this->transport->start();
    }

    $sent = 0;

    try {
        $sent = $this->transport->send($message, $failedRecipients);
    } catch (Swift_RfcComplianceException $e) {
        foreach ($message->getTo() as $address => $name) {
            $failedRecipients[] = $address;
        }
    }

    return $sent;
}
複製程式碼

還記得一開始對每個傳送驅動做封裝了吧,send 動作,最終還是交給我們的郵件傳送驅動去執行,預設的是使用 SmtpTransport,即 Swift_SmtpTransport 傳送。

$sent = $this->transport->send($message, $failedRecipients);
複製程式碼

總結

過了一遍程式碼,粗略瞭解下怎麼封裝各個驅動器,將 markdown 格式轉成 html 格式,然後再封裝成 Message 物件,交給驅動器去傳送郵件。

下一步說一說 Swift_SmtpTransport 實現原理,和我們自己怎麼製作一個驅動器,最後再說一說這過程用到了哪些設計模式?

未完待續

相關文章