🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
<article><h1>Laravel 的 邮件发送功能</h1><ul><li><a href="#introduction">简介</a><ul><li><a href="#driver-prerequisites">驱动前提</a></li></ul></li><li><a href="#generating-mailables">生成 mailables</a></li><li><a href="#writing-mailables">编写 mailables</a><ul><li><a href="#configuring-the-sender">配置发送者</a></li><li><a href="#configuring-the-view">配置视图</a></li><li><a href="#view-data">视图数据</a></li><li><a href="#attachments">附件</a></li><li><a href="#inline-attachments">内部附件</a></li></ul></li><li><a href="#markdown-mailables">Markdown 格式的邮件</a><ul><li><a href="#generating-markdown-mailables">生成 Markdown 格式的邮件</a></li><li><a href="#writing-markdown-messages">编写 Markdown 格式的邮件</a></li><li><a href="#customizing-the-components">自定义组件</a></li></ul></li><li><a href="#sending-mail">发送邮件</a><ul><li><a href="#queueing-mail">队列邮件</a></li></ul></li><li><a href="#mail-and-local-development">邮件与本地开发</a></li><li><a href="#events">事件</a></li></ul><p><a name="introduction"></a></p><h2><a href="#introduction">简介</a></h2><p>Laravel 基于 <a href="http://swiftmailer.org">SwiftMailer</a> 函数库提供了一套干净、简洁的邮件 API ,Laravel 为 SMTP 、Mailgun 、SparkPost 、 Amazon SES 、 PHP 的 <code class=" language-php">mail</code> 函数及 <code class=" language-php">sendmail</code> 提供驱动,让你可以快速从本地或云端服务自由地发送邮件。</p><p><a name="driver-prerequisites"></a></p><h3>驱动前提</h3><p>基于 API 的驱动,例如 Mailgun 和 SparkPost 通常比 SMTP 服务器更简单快速。如果可能,你应该尽可能使用这些驱动。所有的 API 驱动都需要 Guzzle HTTP 函数库,你可以使用 Composer 包管理器安装它:</p><pre class=" language-php"><code class=" language-php">composer <span class="token keyword">require</span> guzzlehttp<span class="token operator">/</span>guzzle</code></pre><h4>Mailgun 驱动</h4><p>要使用 Mailgun 驱动,首先必须安装 Guzzle,之后将 <code class=" language-php">config<span class="token operator">/</span>mail<span class="token punctuation">.</span>php</code> 配置文件中的 <code class=" language-php">driver</code> 选项设置为 <code class=" language-php">mailgun</code>。接下来,确认 <code class=" language-php">config<span class="token operator">/</span>services<span class="token punctuation">.</span>php</code> 配置文件包含以下选项:</p><pre class=" language-php"><code class=" language-php"><span class="token string">'mailgun'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">[</span> <span class="token string">'domain'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token string">'your-mailgun-domain'</span><span class="token punctuation">,</span> <span class="token string">'secret'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token string">'your-mailgun-key'</span><span class="token punctuation">,</span> <span class="token punctuation">]</span><span class="token punctuation">,</span></code></pre><h4>SparkPost 驱动</h4><p>要使用 SparkPost 驱动,首先必须安装 Guzzle,之后将 <code class=" language-php">config<span class="token operator">/</span>mail<span class="token punctuation">.</span>php</code> 配置文件中的 <code class=" language-php">driver</code> 选项设置为 <code class=" language-php">sparkpost</code>。接下来,确认 <code class=" language-php">config<span class="token operator">/</span>services<span class="token punctuation">.</span>php</code> 配置文件包含以下选项:</p><pre class=" language-php"><code class=" language-php"><span class="token string">'sparkpost'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">[</span> <span class="token string">'secret'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token string">'your-sparkpost-key'</span><span class="token punctuation">,</span> <span class="token punctuation">]</span><span class="token punctuation">,</span></code></pre><h4>SES 驱动</h4><p>要使用 Amazon SES 驱动,必须安装 PHP 的 Amazon AWS SDK。你可以在 <code class=" language-php">composer<span class="token punctuation">.</span>json</code> 文件的 <code class=" language-php"><span class="token keyword">require</span></code> 段落加入下面这一行并运行 <code class=" language-php">composer update</code> 命令:</p><pre class=" language-php"><code class=" language-php"><span class="token string">"aws/aws-sdk-php"</span><span class="token punctuation">:</span> <span class="token string">"~3.0"</span></code></pre><p>接下来,将 <code class=" language-php">config<span class="token operator">/</span>mail<span class="token punctuation">.</span>php</code> 配置文件中的 <code class=" language-php">driver</code> 设置为 <code class=" language-php">ses</code>。然后确认 <code class=" language-php">config<span class="token operator">/</span>services<span class="token punctuation">.</span>php</code> 配置文件包含下列选项:</p><pre class=" language-php"><code class=" language-php"><span class="token string">'ses'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">[</span> <span class="token string">'key'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token string">'your-ses-key'</span><span class="token punctuation">,</span> <span class="token string">'secret'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token string">'your-ses-secret'</span><span class="token punctuation">,</span> <span class="token string">'region'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token string">'ses-region'</span><span class="token punctuation">,</span> <span class="token comment" spellcheck="true"> // e.g. us-east-1 </span><span class="token punctuation">]</span><span class="token punctuation">,</span></code></pre><p><a name="generating-mailables"></a></p><h2><a href="#generating-mailables">生成 mailables</a></h2><p>在 Laravel 中,每种类型的邮件都代表一个「mailables」对象。这些对象存储在 <code class=" language-php">app<span class="token operator">/</span>Mail</code> 目录中。如果在你的应用中没有看见这个目录,别担心,在首次使用 <code class=" language-php">make<span class="token punctuation">:</span>mail</code> 命令创建 mailables 类时这个目录会被创建,例如:</p><pre class=" language-php"><code class=" language-php">php artisan make<span class="token punctuation">:</span>mail OrderShipped</code></pre><p><a name="writing-mailables"></a></p><h2><a href="#writing-mailables">编写 mailables</a></h2><p>所有的 「mailables」类都在其 <code class=" language-php">build</code> 方法中完成配置。在这个方法内,你可以调用其他各种方法,如 <code class=" language-php">from</code> 、 <code class=" language-php">subject</code> 、 <code class=" language-php">view</code> 和 <code class=" language-php">attach</code> 来配置完成邮件的详情。</p><p><a name="configuring-the-sender"></a></p><h3>配置发送者</h3><h4>使用 <code class=" language-php">from</code> 方法</h4><p>首先,演示配置邮件的发送者,也就是邮件的参数 「from」,既谁发送了邮件。有两种方法配置发送者。第一种是你可以在 <code class=" language-php">build</code> 方法中使用 <code class=" language-php">from</code> 方法:</p><pre class=" language-php"><code class=" language-php"><span class="token comment" spellcheck="true">/** * Build the message. * * @return $this */</span> <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">build<span class="token punctuation">(</span></span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token this">$this</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">from<span class="token punctuation">(</span></span><span class="token string">'example@example.com'</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">view<span class="token punctuation">(</span></span><span class="token string">'emails.orders.shipped'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span></code></pre><h4>使用一个全局 <code class=" language-php">from</code> 地址</h4><p>然而,如果应用使用相同的 <code class=" language-php">from</code> 地址,你每次发送邮件这种设置方式显得笨拙,替代的方法就是在 <code class=" language-php">config<span class="token operator">/</span>mail<span class="token punctuation">.</span>php</code> 配置文件中设置一个全局 <code class=" language-php">from</code> 地址,这个配置在没有单独指定 「from」时是默认的 <code class=" language-php">from</code> :</p><pre class=" language-php"><code class=" language-php"><span class="token string">'from'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">[</span><span class="token string">'address'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token string">'example@example.com'</span><span class="token punctuation">,</span> <span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token string">'App Name'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></code></pre><p><a name="configuring-the-view"></a></p><h3>配置视图</h3><p>在 <code class=" language-php">build</code>方法内,你可以使用 <code class=" language-php">view</code> 方法指定邮件的模板,以渲染邮件的内容。因为所有邮件都会使用 <a href="/docs/5.4/blade">Blade 模板</a> 渲染内容,你能很容易的使用 Blade 模板引擎构建邮件的 HTML:</p><pre class=" language-php"><code class=" language-php"><span class="token comment" spellcheck="true">/** * Build the message. * * @return $this */</span> <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">build<span class="token punctuation">(</span></span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token this">$this</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">view<span class="token punctuation">(</span></span><span class="token string">'emails.orders.shipped'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span></code></pre><blockquote class="has-icon tip"><p><div class="flag"><span class="svg"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:a="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/" version="1.1" x="0px" y="0px" width="56.6px" height="87.5px" viewBox="0 0 56.6 87.5" enable-background="new 0 0 56.6 87.5" xml:space="preserve"><path fill="#FFFFFF" d="M28.7 64.5c-1.4 0-2.5-1.1-2.5-2.5v-5.7 -5V41c0-1.4 1.1-2.5 2.5-2.5s2.5 1.1 2.5 2.5v10.1 5 5.8C31.2 63.4 30.1 64.5 28.7 64.5zM26.4 0.1C11.9 1 0.3 13.1 0 27.7c-0.1 7.9 3 15.2 8.2 20.4 0.5 0.5 0.8 1 1 1.7l3.1 13.1c0.3 1.1 1.3 1.9 2.4 1.9 0.3 0 0.7-0.1 1.1-0.2 1.1-0.5 1.6-1.8 1.4-3l-2-8.4 -0.4-1.8c-0.7-2.9-2-5.7-4-8 -1-1.2-2-2.5-2.7-3.9C5.8 35.3 4.7 30.3 5.4 25 6.7 14.5 15.2 6.3 25.6 5.1c13.9-1.5 25.8 9.4 25.8 23 0 4.1-1.1 7.9-2.9 11.2 -0.8 1.4-1.7 2.7-2.7 3.9 -2 2.3-3.3 5-4 8L41.4 53l-2 8.4c-0.3 1.2 0.3 2.5 1.4 3 0.3 0.2 0.7 0.2 1.1 0.2 1.1 0 2.2-0.8 2.4-1.9l3.1-13.1c0.2-0.6 0.5-1.2 1-1.7 5-5.1 8.2-12.1 8.2-19.8C56.4 12 42.8-1 26.4 0.1zM43.7 69.6c0 0.5-0.1 0.9-0.3 1.3 -0.4 0.8-0.7 1.6-0.9 2.5 -0.7 3-2 8.6-2 8.6 -1.3 3.2-4.4 5.5-7.9 5.5h-4.1H28h-0.5 -3.6c-3.5 0-6.7-2.4-7.9-5.7l-0.1-0.4 -1.8-7.8c-0.4-1.1-0.8-2.1-1.2-3.1 -0.1-0.3-0.2-0.5-0.2-0.9 0.1-1.3 1.3-2.1 2.6-2.1H41C42.4 67.5 43.6 68.2 43.7 69.6zM37.7 72.5H26.9c-4.2 0-7.2 3.9-6.3 7.9 0.6 1.3 1.8 2.1 3.2 2.1h4.1 0.5 0.5 3.6c1.4 0 2.7-0.8 3.2-2.1L37.7 72.5z"></path></svg></span></div> 你可以创建一个 <code class=" language-php">resources<span class="token operator">/</span>views<span class="token operator">/</span>emails</code> 目录来存放所有的邮件模板;然而,这不是强制要求,你可以在有的将邮件模板放在 <code class=" language-php">resources<span class="token operator">/</span>views</code> 目录的任意位置。</p></blockquote><h4>纯文本邮件</h4><p>如果你想要定义一个纯文本邮件,你可以使用 <code class=" language-php">text</code> 方法。如同 <code class=" language-php">view</code> 方法,<code class=" language-php">text</code> 方法接受一个模板名称,这个名称告诉方法使用哪个模板来渲染邮件,可以自由定义邮件 HTML 和纯文本消息:</p><pre class=" language-php"><code class=" language-php"><span class="token comment" spellcheck="true">/** * Build the message. * * @return $this */</span> <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">build<span class="token punctuation">(</span></span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token this">$this</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">view<span class="token punctuation">(</span></span><span class="token string">'emails.orders.shipped'</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">text<span class="token punctuation">(</span></span><span class="token string">'emails.orders.shipped_plain'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span></code></pre><p><a name="view-data"></a></p><h3>视图数据</h3><h4>通过公共属性</h4><p>通常,你会希望传递一些数据来渲染你的邮件的 HTML 。那么有两种方法可以让视图获得数据,第一种,mailable 类任何公共属性都可以在视图中使用。所以,例如你可以传递数据到 mailable 类的构造函数并且在类中定义数据的公共属性:</p><pre class=" language-php"><code class=" language-php"><span class="token delimiter">&lt;?php</span> <span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Mail</span><span class="token punctuation">;</span> <span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Order</span><span class="token punctuation">;</span> <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Bus<span class="token punctuation">\</span>Queueable</span><span class="token punctuation">;</span> <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Mail<span class="token punctuation">\</span>Mailable</span><span class="token punctuation">;</span> <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Queue<span class="token punctuation">\</span>SerializesModels</span><span class="token punctuation">;</span> <span class="token keyword">class</span> <span class="token class-name">OrderShipped</span> <span class="token keyword">extends</span> <span class="token class-name">Mailable</span> <span class="token punctuation">{</span> <span class="token keyword">use</span> <span class="token package">Queueable</span><span class="token punctuation">,</span> SerializesModels<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">/** * order 实例。 * * @var Order */</span> <span class="token keyword">public</span> <span class="token variable">$order</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">/** * 创建一个新消息实例。 * * @return void */</span> <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">__construct<span class="token punctuation">(</span></span>Order <span class="token variable">$order</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token this">$this</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">order</span> <span class="token operator">=</span> <span class="token variable">$order</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">/** * 构建消息。 * * @return $this */</span> <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">build<span class="token punctuation">(</span></span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token this">$this</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">view<span class="token punctuation">(</span></span><span class="token string">'emails.orders.shipped'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span></code></pre><p>一旦数据被设置为公共属性,它们将自动在视图中加载,所以你可以访问像访问其他 Blade 模板数据一样访问它们:</p><pre class=" language-php"><code class=" language-php"><span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span><span class="token punctuation">&gt;</span></span></span> Price<span class="token punctuation">:</span> <span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$order</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">price</span> <span class="token punctuation">}</span><span class="token punctuation">}</span> <span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span></span></code></pre><h4>通过 <code class=" language-php">with</code> 方法:</h4><p>你可以使用 <code class=" language-php">with</code> 方法来传递数据给模板。一般情况下,你仍然是使用 mailable 类的构造函数来接受数据传参。然而你需要为这些数据属性设置 <code class=" language-php"><span class="token keyword">protected</span></code> 或 <code class=" language-php"><span class="token keyword">private</span></code> 声明,否则这些数据会被自动加载到模板中。接下来你可以使用 <code class=" language-php">with</code> 方法接受键值数组传参来传递数据给模板,就如控制器里为视图传参一样:</p><pre class=" language-php"><code class=" language-php"><span class="token delimiter">&lt;?php</span> <span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Mail</span><span class="token punctuation">;</span> <span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Order</span><span class="token punctuation">;</span> <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Bus<span class="token punctuation">\</span>Queueable</span><span class="token punctuation">;</span> <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Mail<span class="token punctuation">\</span>Mailable</span><span class="token punctuation">;</span> <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Queue<span class="token punctuation">\</span>SerializesModels</span><span class="token punctuation">;</span> <span class="token keyword">class</span> <span class="token class-name">OrderShipped</span> <span class="token keyword">extends</span> <span class="token class-name">Mailable</span> <span class="token punctuation">{</span> <span class="token keyword">use</span> <span class="token package">Queueable</span><span class="token punctuation">,</span> SerializesModels<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">/** * order 实例。 * * @var Order */</span> <span class="token keyword">protected</span> <span class="token variable">$order</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">/** * 创建一个新消息实例。 * * @return void */</span> <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">__construct<span class="token punctuation">(</span></span>Order <span class="token variable">$order</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token this">$this</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">order</span> <span class="token operator">=</span> <span class="token variable">$order</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">/** * 构建消息。 * * @return $this */</span> <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">build<span class="token punctuation">(</span></span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token this">$this</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">view<span class="token punctuation">(</span></span><span class="token string">'emails.orders.shipped'</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">with<span class="token punctuation">(</span></span><span class="token punctuation">[</span> <span class="token string">'orderName'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token this">$this</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">order</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">name</span><span class="token punctuation">,</span> <span class="token string">'orderPrice'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token this">$this</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">order</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">price</span><span class="token punctuation">,</span> <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span></code></pre><p>一旦数据已经用 <code class=" language-php">with</code> 方法传递,它们将自动在视图中加载,所以你可以访问像访问其他 Blade 模板数据一样访问它们:</p><pre class=" language-php"><code class=" language-php"><span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span><span class="token punctuation">&gt;</span></span></span> Price<span class="token punctuation">:</span> <span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$orderPrice</span> <span class="token punctuation">}</span><span class="token punctuation">}</span> <span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span></span></code></pre><p><a name="attachments"></a></p><h3>附件</h3><p>要在邮件中加入附件,在 <code class=" language-php">build</code> 方法中使用 <code class=" language-php">attach</code> 方法。<code class=" language-php">attach</code> 方法接受文件的绝对路径作为它的第一个参数:</p><pre class=" language-php"><code class=" language-php"> <span class="token comment" spellcheck="true">/** * Build the message. * * @return $this */</span> <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">build<span class="token punctuation">(</span></span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token this">$this</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">view<span class="token punctuation">(</span></span><span class="token string">'emails.orders.shipped'</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">attach<span class="token punctuation">(</span></span><span class="token string">'/path/to/file'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span></code></pre><p>附加文件到消息时,你也可以传递 <code class=" language-php">数组</code> 给 <code class=" language-php">attache</code> 方法作为第二个参数,以指定显示名称和 / 或是 MIME 类型:</p><pre class=" language-php"><code class=" language-php"> <span class="token comment" spellcheck="true">/** * Build the message. * * @return $this */</span> <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">build<span class="token punctuation">(</span></span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token this">$this</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">view<span class="token punctuation">(</span></span><span class="token string">'emails.orders.shipped'</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">attach<span class="token punctuation">(</span></span><span class="token string">'/path/to/file'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token string">'as'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token string">'name.pdf'</span><span class="token punctuation">,</span> <span class="token string">'mime'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token string">'application/pdf'</span><span class="token punctuation">,</span> <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span></code></pre><h4>原始数据附件</h4><p><code class=" language-php">attachData</code> 可以使用字节数据作为附件。例如,你可以使用这个方法将内存中生成而没有保存到磁盘中的 PDF 附加到邮件中。<code class=" language-php">attachData</code> 方法第一个参数接收原始字节数据,第二个参数为文件名,第三个参数接受一个数组以指定其他参数:</p><pre class=" language-php"><code class=" language-php"> <span class="token comment" spellcheck="true">/** * Build the message. * * @return $this */</span> <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">build<span class="token punctuation">(</span></span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token this">$this</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">view<span class="token punctuation">(</span></span><span class="token string">'emails.orders.shipped'</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">attachData<span class="token punctuation">(</span></span><span class="token this">$this</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">pdf</span><span class="token punctuation">,</span> <span class="token string">'name.pdf'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token string">'mime'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token string">'application/pdf'</span><span class="token punctuation">,</span> <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span></code></pre><p><a name="inline-attachments"></a></p><h3>内部附件</h3><p>在邮件中嵌入内部图标通常是件麻烦事;然而 Laravel 提供了一个便利的方法,让你在邮件中附件图片并获取适当的 CID 。要嵌入内部图片,在邮件模板的 <code class=" language-php"><span class="token variable">$message</span></code> 变量上调用 <code class=" language-php">embed</code> 方法,Laravel 会自动让你所有邮件模板都能够获取到 <code class=" language-php"><span class="token variable">$message</span></code> 变量,所以不必担心如何手动传递它:</p><pre class=" language-php"><code class=" language-php"><span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>body</span><span class="token punctuation">&gt;</span></span></span> 这是一张图片: <span class="token markup">&lt;img src="{{ $message-&gt;</span><span class="token function">embed<span class="token punctuation">(</span></span><span class="token variable">$pathToFile</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">}</span>"<span class="token operator">&gt;</span> <span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>body</span><span class="token punctuation">&gt;</span></span></span></code></pre><h4>嵌入原始数据附件</h4><p>如果你已经有想嵌入原始数据,希望嵌入邮件模板,你可以调用 <code class=" language-php"><span class="token variable">$message</span></code> 变量的 <code class=" language-php">embedData</code> 方法:</p><pre class=" language-php"><code class=" language-php"><span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>body</span><span class="token punctuation">&gt;</span></span></span> 这是一张原始数据图片: <span class="token markup">&lt;img src="{{ $message-&gt;</span><span class="token function">embedData<span class="token punctuation">(</span></span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token variable">$name</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">}</span>"<span class="token operator">&gt;</span> <span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>body</span><span class="token punctuation">&gt;</span></span></span></code></pre><p><a name="markdown-mailables"></a></p><h2><a href="#markdown-mailables">Markdown 格式的 Mailables 类</a></h2><p>Markdown 格式的 mailable 消息允许你从预编译的模板和你的 mailables 类中的邮件提醒组件中受益。因为消息是用 Markdown 格式写的, Laravel 能为消息体渲染出漂亮、响应式的 HTML 模板,也能自动生成一个纯文本的副本。</p><p><a name="generating-markdown-mailables"></a></p><h3>生成 Markdown 格式的 Mailables</h3><p>要生成一个包含友好的 Markdown 模板的 mailable 类,你在使用 <code class=" language-php">make<span class="token punctuation">:</span>mail</code> 这个 Artisan 命令时,要加上 <code class=" language-php"><span class="token operator">--</span>markdown</code> 选项:</p><pre class=" language-php"><code class=" language-php">php artisan make<span class="token punctuation">:</span>mail OrderShipped <span class="token operator">--</span>markdown<span class="token operator">=</span>emails<span class="token punctuation">.</span>orders<span class="token punctuation">.</span>shipped</code></pre><p>然后,在使用 <code class=" language-php">build</code> 方法配置 mailable 时,用 <code class=" language-php">markdown</code> 方法来换掉 <code class=" language-php">view</code> 方法, <code class=" language-php">markdown</code> 方法接受一个 Markdown 模板的名称和一个将在模板中可用的选项数组:</p><pre class=" language-php"><code class=" language-php"><span class="token comment" spellcheck="true">/** * 构建消息。 * * @return $this */</span> <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">build<span class="token punctuation">(</span></span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token this">$this</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">from<span class="token punctuation">(</span></span><span class="token string">'example@example.com'</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">markdown<span class="token punctuation">(</span></span><span class="token string">'emails.orders.shipped'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span></code></pre><p><a name="writing-markdown-messages"></a></p><h3>编写 Markdown 格式的消息</h3><p>Markdown mailables 使用 Blade 组件和 Markdown 语法的组合,允许你轻松地构建邮件消息,同时利用 Laravel 的预制组件。</p><pre class=" language-php"><code class=" language-php">@<span class="token function">component<span class="token punctuation">(</span></span><span class="token string">'mail::message'</span><span class="token punctuation">)</span><span class="token comment" spellcheck="true"> # Order Shipped </span> Your order has been shipped<span class="token operator">!</span> @<span class="token function">component<span class="token punctuation">(</span></span><span class="token string">'mail::button'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string">'url'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token variable">$url</span><span class="token punctuation">]</span><span class="token punctuation">)</span> View Order @endcomponent Thanks<span class="token punctuation">,</span><span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>br</span><span class="token punctuation">&gt;</span></span></span> <span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token function">config<span class="token punctuation">(</span></span><span class="token string">'app.name'</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">}</span> @endcomponent</code></pre><blockquote class="has-icon tip"><p><div class="flag"><span class="svg"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:a="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/" version="1.1" x="0px" y="0px" width="56.6px" height="87.5px" viewBox="0 0 56.6 87.5" enable-background="new 0 0 56.6 87.5" xml:space="preserve"><path fill="#FFFFFF" d="M28.7 64.5c-1.4 0-2.5-1.1-2.5-2.5v-5.7 -5V41c0-1.4 1.1-2.5 2.5-2.5s2.5 1.1 2.5 2.5v10.1 5 5.8C31.2 63.4 30.1 64.5 28.7 64.5zM26.4 0.1C11.9 1 0.3 13.1 0 27.7c-0.1 7.9 3 15.2 8.2 20.4 0.5 0.5 0.8 1 1 1.7l3.1 13.1c0.3 1.1 1.3 1.9 2.4 1.9 0.3 0 0.7-0.1 1.1-0.2 1.1-0.5 1.6-1.8 1.4-3l-2-8.4 -0.4-1.8c-0.7-2.9-2-5.7-4-8 -1-1.2-2-2.5-2.7-3.9C5.8 35.3 4.7 30.3 5.4 25 6.7 14.5 15.2 6.3 25.6 5.1c13.9-1.5 25.8 9.4 25.8 23 0 4.1-1.1 7.9-2.9 11.2 -0.8 1.4-1.7 2.7-2.7 3.9 -2 2.3-3.3 5-4 8L41.4 53l-2 8.4c-0.3 1.2 0.3 2.5 1.4 3 0.3 0.2 0.7 0.2 1.1 0.2 1.1 0 2.2-0.8 2.4-1.9l3.1-13.1c0.2-0.6 0.5-1.2 1-1.7 5-5.1 8.2-12.1 8.2-19.8C56.4 12 42.8-1 26.4 0.1zM43.7 69.6c0 0.5-0.1 0.9-0.3 1.3 -0.4 0.8-0.7 1.6-0.9 2.5 -0.7 3-2 8.6-2 8.6 -1.3 3.2-4.4 5.5-7.9 5.5h-4.1H28h-0.5 -3.6c-3.5 0-6.7-2.4-7.9-5.7l-0.1-0.4 -1.8-7.8c-0.4-1.1-0.8-2.1-1.2-3.1 -0.1-0.3-0.2-0.5-0.2-0.9 0.1-1.3 1.3-2.1 2.6-2.1H41C42.4 67.5 43.6 68.2 43.7 69.6zM37.7 72.5H26.9c-4.2 0-7.2 3.9-6.3 7.9 0.6 1.3 1.8 2.1 3.2 2.1h4.1 0.5 0.5 3.6c1.4 0 2.7-0.8 3.2-2.1L37.7 72.5z"></path></svg></span></div> 当你在写 Markdown 格式邮件的时候千万不要有多余缩进,不然 Markdown 解析器很容易会把缩进的内容渲染成代码块。</p></blockquote><h4>按钮组件</h4><p>按钮连组件渲染一个居中的连接按钮,组件接受两个参数,一个 <code class=" language-php">url</code> 和一个可选的 <code class=" language-php">color</code> 。支持的颜色有 <code class=" language-php">blue</code> 、 <code class=" language-php">green</code> 、 和 <code class=" language-php">red</code> 。你可以在邮件消息体中加入随便多个你想要的按钮。</p><pre class=" language-php"><code class=" language-php">@<span class="token function">component<span class="token punctuation">(</span></span><span class="token string">'mail::button'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string">'url'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token variable">$url</span><span class="token punctuation">,</span> <span class="token string">'color'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token string">'green'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> View Order @endcomponent</code></pre><h4>面板组件</h4><p>面板组件将面板中给定的一块文字的背景渲染成跟其他的消息体背景稍微不同。这样可以让这块文字引起人们的注意。</p><pre class=" language-php"><code class=" language-php">@<span class="token function">component<span class="token punctuation">(</span></span><span class="token string">'mail::panel'</span><span class="token punctuation">)</span> This is the panel content<span class="token punctuation">.</span> @endcomponent</code></pre><h4>表格组件</h4><p>表格组件允许你将一个 Markdown 格式的表格转换成一个 HTML 格式的表格。组件接受 Markdown 格式的表格作为它的内容。表格列对齐方式按照默认的 Markdown 对齐风格而定。</p><pre class=" language-php"><code class=" language-php">@<span class="token function">component<span class="token punctuation">(</span></span><span class="token string">'mail::table'</span><span class="token punctuation">)</span> <span class="token operator">|</span> Laravel <span class="token operator">|</span> Table <span class="token operator">|</span> Example <span class="token operator">|</span> <span class="token operator">|</span> <span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span> <span class="token operator">|</span><span class="token punctuation">:</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span><span class="token punctuation">:</span><span class="token operator">|</span> <span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token punctuation">:</span><span class="token operator">|</span> <span class="token operator">|</span> Col <span class="token number">2</span> is <span class="token operator">|</span> Centered <span class="token operator">|</span> <span class="token variable">$10</span> <span class="token operator">|</span> <span class="token operator">|</span> Col <span class="token number">3</span> is <span class="token operator">|</span> Right<span class="token operator">-</span>Aligned <span class="token operator">|</span> <span class="token variable">$20</span> <span class="token operator">|</span> @endcomponent</code></pre><p><a name="customizing-the-components"></a></p><h3>自定义组件</h3><p>你或许会为了自定义而导出你应用中所有的 Markdown 邮件组件。要导出这些组件,使用 <code class=" language-php">vendor<span class="token punctuation">:</span>publish</code> 这个 Artisan 命令来发布资源文件标签。</p><pre class=" language-php"><code class=" language-php">php artisan vendor<span class="token punctuation">:</span>publish <span class="token operator">--</span>tag<span class="token operator">=</span>laravel<span class="token operator">-</span>mail</code></pre><p>这个命令会发布 Markdown 邮件组件到 <code class=" language-php">resources<span class="token operator">/</span>views<span class="token operator">/</span>vendor<span class="token operator">/</span>mail</code> 文件夹。而 <code class=" language-php">mail</code> 文件夹会包含一个 <code class=" language-php">html</code> 文件夹和一个 <code class=" language-php">markdown</code> 文件夹,每个文件夹都包含他们的可用组件的描述。你可以按照你的意愿自定义这些组件。</p><h4>自定义样式表 CSS</h4><p>在导出组件之后,<code class=" language-php">resources<span class="token operator">/</span>views<span class="token operator">/</span>vendor<span class="token operator">/</span>mail<span class="token operator">/</span>html<span class="token operator">/</span>themes</code> 文件夹将包含一个默认的 <code class=" language-php"><span class="token keyword">default</span><span class="token punctuation">.</span>css</code> 文件。你可以在这个文件中自定义 CSS ,你定义的这些样式将会在 Markdown 格式消息体转换成 HTML 格式时自动得到应用。</p><blockquote class="has-icon tip"><p><div class="flag"><span class="svg"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:a="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/" version="1.1" x="0px" y="0px" width="56.6px" height="87.5px" viewBox="0 0 56.6 87.5" enable-background="new 0 0 56.6 87.5" xml:space="preserve"><path fill="#FFFFFF" d="M28.7 64.5c-1.4 0-2.5-1.1-2.5-2.5v-5.7 -5V41c0-1.4 1.1-2.5 2.5-2.5s2.5 1.1 2.5 2.5v10.1 5 5.8C31.2 63.4 30.1 64.5 28.7 64.5zM26.4 0.1C11.9 1 0.3 13.1 0 27.7c-0.1 7.9 3 15.2 8.2 20.4 0.5 0.5 0.8 1 1 1.7l3.1 13.1c0.3 1.1 1.3 1.9 2.4 1.9 0.3 0 0.7-0.1 1.1-0.2 1.1-0.5 1.6-1.8 1.4-3l-2-8.4 -0.4-1.8c-0.7-2.9-2-5.7-4-8 -1-1.2-2-2.5-2.7-3.9C5.8 35.3 4.7 30.3 5.4 25 6.7 14.5 15.2 6.3 25.6 5.1c13.9-1.5 25.8 9.4 25.8 23 0 4.1-1.1 7.9-2.9 11.2 -0.8 1.4-1.7 2.7-2.7 3.9 -2 2.3-3.3 5-4 8L41.4 53l-2 8.4c-0.3 1.2 0.3 2.5 1.4 3 0.3 0.2 0.7 0.2 1.1 0.2 1.1 0 2.2-0.8 2.4-1.9l3.1-13.1c0.2-0.6 0.5-1.2 1-1.7 5-5.1 8.2-12.1 8.2-19.8C56.4 12 42.8-1 26.4 0.1zM43.7 69.6c0 0.5-0.1 0.9-0.3 1.3 -0.4 0.8-0.7 1.6-0.9 2.5 -0.7 3-2 8.6-2 8.6 -1.3 3.2-4.4 5.5-7.9 5.5h-4.1H28h-0.5 -3.6c-3.5 0-6.7-2.4-7.9-5.7l-0.1-0.4 -1.8-7.8c-0.4-1.1-0.8-2.1-1.2-3.1 -0.1-0.3-0.2-0.5-0.2-0.9 0.1-1.3 1.3-2.1 2.6-2.1H41C42.4 67.5 43.6 68.2 43.7 69.6zM37.7 72.5H26.9c-4.2 0-7.2 3.9-6.3 7.9 0.6 1.3 1.8 2.1 3.2 2.1h4.1 0.5 0.5 3.6c1.4 0 2.7-0.8 3.2-2.1L37.7 72.5z"></path></svg></span></div> 如果你想为 Markdown 组件构建一个全新的主题,只要写一个新的 CSS 文件,放在 <code class=" language-php">html<span class="token operator">/</span>themes</code> 文件夹,然后在你的 <code class=" language-php">mail</code> 配置文件中改变 <code class=" language-php">theme</code> 选项就可以了。</p></blockquote><p><a name="sending-mail"></a></p><h2><a href="#sending-mail">发送邮件</a></h2><p>要发送邮件,使用 <code class=" language-php">Mail</code> <a href="/docs/5.4/facades">facade</a> 的 <code class=" language-php">to</code> 方法。 <code class=" language-php">to</code> 方法接受一个邮件地址,一个 user 实现或一个 users 集合。如果传递一个对象或集合,mailer 将自动使用 <code class=" language-php">email</code> 和 <code class=" language-php">name</code> 属性来设置邮件收件人,所以确保你的对象里有这些属性。一旦指定收件人,你可以传递一个实现到 mailable 类的 <code class=" language-php">send</code> 方法:</p><pre class=" language-php"><code class=" language-php"><span class="token delimiter">&lt;?php</span> <span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers</span><span class="token punctuation">;</span> <span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Order</span><span class="token punctuation">;</span> <span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Mail<span class="token punctuation">\</span>OrderShipped</span><span class="token punctuation">;</span> <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Request</span><span class="token punctuation">;</span> <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Mail</span><span class="token punctuation">;</span> <span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers<span class="token punctuation">\</span>Controller</span><span class="token punctuation">;</span> <span class="token keyword">class</span> <span class="token class-name">OrderController</span> <span class="token keyword">extends</span> <span class="token class-name">Controller</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">/** * Ship the given order. * * @param Request $request * @param int $orderId * @return Response */</span> <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">ship<span class="token punctuation">(</span></span>Request <span class="token variable">$request</span><span class="token punctuation">,</span> <span class="token variable">$orderId</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token variable">$order</span> <span class="token operator">=</span> <span class="token scope">Order<span class="token punctuation">::</span></span><span class="token function">findOrFail<span class="token punctuation">(</span></span><span class="token variable">$orderId</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true"> // Ship order... </span> <span class="token scope">Mail<span class="token punctuation">::</span></span><span class="token function">to<span class="token punctuation">(</span></span><span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">user<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">send<span class="token punctuation">(</span></span><span class="token keyword">new</span> <span class="token class-name">OrderShipped</span><span class="token punctuation">(</span><span class="token variable">$order</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span></code></pre><p>当然,不局限于只使用「to」给收件人发送邮件,你可以通过一个单一的链式调用来自由的设置 「to」,「cc」和 「bcc」接收者:</p><pre class=" language-php"><code class=" language-php"><span class="token scope">Mail<span class="token punctuation">::</span></span><span class="token function">to<span class="token punctuation">(</span></span><span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">user<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">cc<span class="token punctuation">(</span></span><span class="token variable">$moreUsers</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">bcc<span class="token punctuation">(</span></span><span class="token variable">$evenMoreUsers</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">send<span class="token punctuation">(</span></span><span class="token keyword">new</span> <span class="token class-name">OrderShipped</span><span class="token punctuation">(</span><span class="token variable">$order</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="queueing-mail"></a></p><h3>队列 Mail</h3><h4>将邮件消息加入队列</h4><p>由于发送消息会大幅延迟应用响应时间,许多开发者选择将邮件消息加入队列在后台进行发送。Laravel 使用内置的 <a href="/docs/5.4/queues">统一的队列 API</a> 来轻松完成此工作。将邮件消息加入队列,使用 <code class=" language-php">Mail</code> facade 的 <code class=" language-php">queue</code> 方法:</p><pre class=" language-php"><code class=" language-php"><span class="token scope">Mail<span class="token punctuation">::</span></span><span class="token function">to<span class="token punctuation">(</span></span><span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">user<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">cc<span class="token punctuation">(</span></span><span class="token variable">$moreUsers</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">bcc<span class="token punctuation">(</span></span><span class="token variable">$evenMoreUsers</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">queue<span class="token punctuation">(</span></span><span class="token keyword">new</span> <span class="token class-name">OrderShipped</span><span class="token punctuation">(</span><span class="token variable">$order</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>这个方法会自动将工作加入队列,以便后台发送邮件。当然,在使用这个功能前,你需要 <a href="/docs/5.4/queues">设置你的队列</a> 。</p><h4>延迟邮件消息队列</h4><p>如果你希望延迟发送已加入队列的邮件消息,你可以使用 <code class=" language-php">later</code> 方法。<code class=" language-php">later</code> 第一个参数接受一个 <code class=" language-php">DateTime</code> 实现以告知这个消息应该何时发送:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$when</span> <span class="token operator">=</span> <span class="token scope">Carbon<span class="token punctuation">\</span>Carbon<span class="token punctuation">::</span></span><span class="token function">now<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">addMinutes<span class="token punctuation">(</span></span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token scope">Mail<span class="token punctuation">::</span></span><span class="token function">to<span class="token punctuation">(</span></span><span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">user<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">cc<span class="token punctuation">(</span></span><span class="token variable">$moreUsers</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">bcc<span class="token punctuation">(</span></span><span class="token variable">$evenMoreUsers</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">later<span class="token punctuation">(</span></span><span class="token variable">$when</span><span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token class-name">OrderShipped</span><span class="token punctuation">(</span><span class="token variable">$order</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><h4>推送到特定队列</h4><p>因为所有 mailable 类是通过 <code class=" language-php">make<span class="token punctuation">:</span>mail</code> 命令生成并使用 <code class=" language-php">Illuminate\<span class="token package">Bus<span class="token punctuation">\</span>Queueable</span></code> trait ,你可以在任何 mailable 类实现中调用 <code class=" language-php">onQueue</code> 来指定队列名称,还有 <code class=" language-php">onConnection</code> 方法来指定队列链接名称:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$message</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">OrderShipped</span><span class="token punctuation">(</span><span class="token variable">$order</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">onConnection<span class="token punctuation">(</span></span><span class="token string">'sqs'</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">onQueue<span class="token punctuation">(</span></span><span class="token string">'emails'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token scope">Mail<span class="token punctuation">::</span></span><span class="token function">to<span class="token punctuation">(</span></span><span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">user<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">cc<span class="token punctuation">(</span></span><span class="token variable">$moreUsers</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">bcc<span class="token punctuation">(</span></span><span class="token variable">$evenMoreUsers</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">queue<span class="token punctuation">(</span></span><span class="token variable">$message</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><h4>默认队列</h4><p>如果你的 mailable 类想要默认使用队列,你可以在类中实现 <code class=" language-php">ShouldQueue</code> 接口契约。现在,即便你调用 <code class=" language-php">send</code> 方法来发送邮件, mailable 类仍将邮件放入队列中发送。</p><pre class=" language-php"><code class=" language-php"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Contracts<span class="token punctuation">\</span>Queue<span class="token punctuation">\</span>ShouldQueue</span><span class="token punctuation">;</span> <span class="token keyword">class</span> <span class="token class-name">OrderShipped</span> <span class="token keyword">extends</span> <span class="token class-name">Mailable</span> <span class="token keyword">implements</span> <span class="token class-name">ShouldQueue</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true"> // </span><span class="token punctuation">}</span></code></pre><p><a name="mail-and-local-development"></a></p><h2><a href="#mail-and-local-development">邮件和本地开发</a></h2><p>当应用开发发送邮件时,你或许不想真实的发送邮件到真实的邮件地址。Laravel 提供几种方法以在本地开发时真实发送 「失去能力」。</p><h4>日志驱动</h4><p>代替真实发送,<code class=" language-php">log</code> 邮件驱动将所有邮件消息写入日志文件以供检查,需要更多根据环境来设置应用程序的信息,可参考 <a href="/docs/5.4/installation#environment-configuration">配置文件</a>.</p><h4>通用收件者</h4><p>另一个由 Laravel 提供的解决方案是设置一个通用邮件接收者,由框架发送邮件。这个方法将所有邮件发送到一个邮件地址,而不是发送给实际收件人。这可以通过 <code class=" language-php">config<span class="token operator">/</span>mail<span class="token punctuation">.</span>php</code> 配置文件的 <code class=" language-php">to</code> 选项来完成:</p><pre class=" language-php"><code class=" language-php"><span class="token string">'to'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">[</span> <span class="token string">'address'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token string">'example@example.com'</span><span class="token punctuation">,</span> <span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token string">'Example'</span> <span class="token punctuation">]</span><span class="token punctuation">,</span></code></pre><h4>Mailtrap</h4><p>最后,你可以使用像 <a href="https://mailtrap.io">Mailtrap</a> 和 <code class=" language-php">smtp</code> 驱动来将你的邮件消息发送到一个 「虚假的」邮箱中,你却可以在一个真实的邮件客户端中查看它们。这个方法的好处是让你可以在 Mailtrap 的消息阅读器中查看最终的实际邮件。</p><p><a name="events"></a></p><h2><a href="#events">事件</a></h2><p>Laravel 会在发送邮件消息之前触发一个事件。切记,这个事件只会在邮件 <em>发送</em> 时触发,在加入队列时不触发。你可以在你的 <code class=" language-php">EventServiceProvider</code> 注册一个事件监听器:</p><pre class=" language-php"><code class=" language-php"><span class="token comment" spellcheck="true">/** * 应用事件监听映射。 * * @var array */</span> <span class="token keyword">protected</span> <span class="token variable">$listen</span> <span class="token operator">=</span> <span class="token punctuation">[</span> <span class="token string">'Illuminate\Mail\Events\MessageSending'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">[</span> <span class="token string">'App\Listeners\LogSentMessage'</span><span class="token punctuation">,</span> <span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">]</span><span class="token punctuation">;</span></code></pre><h2>译者署名</h2><table><thead><tr><th>用户名</th><th>头像</th><th>职能</th><th>签名</th></tr></thead><tbody><tr><td><a href="https://github.com/qufo">@qufo</a></td><td><img class="avatar-66 rm-style" src="https://avatars1.githubusercontent.com/u/2526883?v=3&amp;s=460?imageView2/1/w/100/h/100"></td><td>翻译</td><td>欢迎共同探讨。<a href="https://github.com/qufo">@Qufo</a></td></tr></tbody></table></article>