邮件
简介
发送电子邮件不必很复杂。Laravel 提供了一个干净、简单的电子邮件 API,由流行的 Symfony Mailer 组件提供支持。Laravel 和 Symfony Mailer 提供了通过 SMTP、Mailgun、Postmark、Resend、Amazon SES 和 sendmail 发送电子邮件的驱动程序,让您可以快速开始通过您选择的本地或基于云的服务发送邮件。
配置
Laravel 的电子邮件服务可以通过您应用程序的 config/mail.php 配置文件进行配置。在此文件中配置的每个邮件程序都可以有自己独特的配置,甚至有自己独特的“传输”,让您的应用程序可以使用不同的电子邮件服务来发送某些电子邮件。例如,您的应用程序可能使用 Postmark 发送事务性电子邮件,而使用 Amazon SES 发送批量电子邮件。
在您的 mail 配置文件中,您会找到一个 mailers 配置数组。此数组包含 Laravel 支持的每个主要邮件驱动程序/传输的示例配置条目,而 default 配置值决定了当您的应用程序需要发送电子邮件时将默认使用哪个邮件程序。
驱动程序 / 传输先决条件
基于 API 的驱动程序,例如 Mailgun、Postmark、Resend 和 MailerSend,通常比通过 SMTP 服务器发送邮件更简单、更快。只要有可能,我们建议您使用这些驱动程序之一。
Mailgun 驱动程序
要使用 Mailgun 驱动程序,请通过 Composer 安装 Symfony 的 Mailgun Mailer 传输:
composer require symfony/mailgun-mailer symfony/http-client
接下来,您需要在应用程序的 config/mail.php 配置文件中进行两项更改。首先,将您的默认邮件程序设置为 mailgun:
'default' => env('MAIL_MAILER', 'mailgun'),
其次,将以下配置数组添加到您的 mailers 数组中:
'mailgun' => [
'transport' => 'mailgun',
// 'client' => [
// 'timeout' => 5,
// ],
],
配置应用程序的默认邮件程序后,将以下选项添加到您的 config/services.php 配置文件中:
'mailgun' => [
'domain' => env('MAILGUN_DOMAIN'),
'secret' => env('MAILGUN_SECRET'),
'endpoint' => env('MAILGUN_ENDPOINT', 'api.mailgun.net'),
'scheme' => 'https',
],
如果您不使用美国 Mailgun 区域,您可以在 services 配置文件中定义您所在区域的端点:
'mailgun' => [
'domain' => env('MAILGUN_DOMAIN'),
'secret' => env('MAILGUN_SECRET'),
'endpoint' => env('MAILGUN_ENDPOINT', 'api.eu.mailgun.net'),
'scheme' => 'https',
],
Postmark 驱动程序
要使用 Postmark 驱动程序,请通过 Composer 安装 Symfony 的 Postmark Mailer 传输:
composer require symfony/postmark-mailer symfony/http-client
接下来,将应用程序的 config/mail.php 配置文件中的 default 选项设置为 postmark。配置应用程序的默认邮件程序后,请确保您的 config/services.php 配置文件包含以下选项:
'postmark' => [
'token' => env('POSTMARK_TOKEN'),
],
如果您想指定给定邮件程序应使用的 Postmark 消息流,您可以将 message_stream_id 配置选项添加到邮件程序的配置数组中。此配置数组可以在您应用程序的 config/mail.php 配置文件中找到:
'postmark' => [
'transport' => 'postmark',
'message_stream_id' => env('POSTMARK_MESSAGE_STREAM_ID'),
// 'client' => [
// 'timeout' => 5,
// ],
],
这样您还可以设置具有不同消息流的多个 Postmark 邮件程序。
Resend 驱动程序
要使用 Resend 驱动程序,请通过 Composer 安装 Resend 的 PHP SDK:
composer require resend/resend-php
接下来,将应用程序的 config/mail.php 配置文件中的 default 选项设置为 resend。配置应用程序的默认邮件程序后,请确保您的 config/services.php 配置文件包含以下选项:
'resend' => [
'key' => env('RESEND_KEY'),
],
SES 驱动程序
要使用 Amazon SES 驱动程序,您必须首先安装 Amazon AWS SDK for PHP。您可以通过 Composer 包管理器安装此库:
composer require aws/aws-sdk-php
接下来,将您的 config/mail.php 配置文件中的 default 选项设置为 ses 并验证您的 config/services.php 配置文件是否包含以下选项:
'ses' => [
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
],
要通过会话令牌使用 AWS 临时凭证,您可以将 token 键添加到应用程序的 SES 配置中:
'ses' => [
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
'token' => env('AWS_SESSION_TOKEN'),
],
要与 SES 的 订阅管理功能 进行交互,您可以在邮件消息的 headers 方法返回的数组中返回 X-Ses-List-Management-Options 标头:
/**
* Get the message headers.
*/
public function headers(): Headers
{
return new Headers(
text: [
'X-Ses-List-Management-Options' => 'contactListName=MyContactList;topicName=MyTopic',
],
);
}
如果您想定义 Laravel 在发送电子邮件时应传递给 AWS SDK 的 SendEmail 方法的 附加选项,您可以在您的 ses 配置中定义一个 options 数组:
'ses' => [
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
'options' => [
'ConfigurationSetName' => 'MyConfigurationSet',
'EmailTags' => [
['Name' => 'foo', 'Value' => 'bar'],
],
],
],
MailerSend 驱动程序
MailerSend,一个事务性电子邮件和短信服务,为 Laravel 维护自己的基于 API 的邮件驱动程序。包含该驱动程序的包可以通过 Composer 包管理器安装:
composer require mailersend/laravel-driver
安装包后,将 MAILERSEND_API_KEY 环境变量添加到您的应用程序的 .env 文件中。此外,MAIL_MAILER 环境变量应定义为 mailersend:
MAIL_MAILER=mailersend
MAIL_FROM_ADDRESS=app@yourdomain.com
MAIL_FROM_NAME="App Name"
MAILERSEND_API_KEY=your-api-key
最后,将 MailerSend 添加到您应用程序的 config/mail.php 配置文件中的 mailers 数组中:
'mailersend' => [
'transport' => 'mailersend',
],
要了解有关 MailerSend 的更多信息,包括如何使用托管模板,请查阅 MailerSend 驱动程序文档。
故障转移配置
有时,您配置的用于发送应用程序邮件的外部服务可能会宕机。在这种情况下,定义一个或多个备用邮件传递配置会很有用,以便在您的主传递驱动程序宕机时使用。
为此,您应该在应用程序的 mail 配置文件中定义一个使用 failover 传输的邮件程序。您的应用程序的 failover 邮件程序的配置数组应包含一个 mailers 数组,该数组引用应选择配置的邮件程序进行传递的顺序:
'mailers' => [
'failover' => [
'transport' => 'failover',
'mailers' => [
'postmark',
'mailgun',
'sendmail',
],
'retry_after' => 60,
],
// ...
],
定义故障转移邮件程序后,您应该通过在应用程序的 mail 配置文件中的 default 配置键的值中指定其名称,将其设置为应用程序使用的默认邮件程序:
'default' => env('MAIL_MAILER', 'failover'),
轮询配置
roundrobin 传输允许您将邮件工作负载分配给多个邮件程序。首先,在应用程序的 mail 配置文件中定义一个使用 roundrobin 传输的邮件程序。您的应用程序的 roundrobin 邮件程序的配置数组应包含一个 mailers 数组,该数组引用应使用哪些配置的邮件程序进行传递:
'mailers' => [
'roundrobin' => [
'transport' => 'roundrobin',
'mailers' => [
'ses',
'postmark',
],
'retry_after' => 60,
],
// ...
],
定义轮询邮件程序后,您应该通过在应用程序的 mail 配置文件中的 default 配置键的值中指定其名称,将其设置为应用程序使用的默认邮件程序:
'default' => env('MAIL_MAILER', 'roundrobin'),
轮询传输从配置的邮件程序列表中选择一个随机邮件程序,然后为每个后续电子邮件切换到下一个可用的邮件程序。与有助于实现 高可用性 的 failover 传输相反,roundrobin 传输提供 负载均衡。
生成可邮寄类
在构建 Laravel 应用程序时,您的应用程序发送的每种电子邮件都表示为一个“可邮寄”类。这些类存储在 app/Mail 目录中。如果您在应用程序中没有看到此目录,请不要担心,因为当您使用 make:mail Artisan 命令创建第一个可邮寄类时,它将为您生成:
php artisan make:mail OrderShipped
编写可邮寄类
生成可邮寄类后,让我们打开它,以便我们可以探索其内容。可邮寄类配置在几个方法中完成,包括 envelope、content 和 attachments 方法。
envelope 方法返回一个 Illuminate\Mail\Mailables\Envelope 对象,该对象定义了主题,有时还定义了消息的收件人。content 方法返回一个 Illuminate\Mail\Mailables\Content 对象,该对象定义了将用于生成消息内容的 Blade 模板。
配置发件人
使用信封
首先,让我们探索如何配置电子邮件的发件人。或者,换句话说,电子邮件将“来自”谁。有两种方法可以配置发件人。首先,您可以在消息的信封上指定“发件人”地址:
use Illuminate\Mail\Mailables\Address;
use Illuminate\Mail\Mailables\Envelope;
/**
* Get the message envelope.
*/
public function envelope(): Envelope
{
return new Envelope(
from: new Address('jeffrey@example.com', 'Jeffrey Way'),
subject: 'Order Shipped',
);
}
如果您愿意,您还可以指定一个 replyTo 地址:
return new Envelope(
from: new Address('jeffrey@example.com', 'Jeffrey Way'),
replyTo: [
new Address('taylor@example.com', 'Taylor Otwell'),
],
subject: 'Order Shipped',
);
使用全局 from 地址
但是,如果您的应用程序对所有电子邮件都使用相同的“发件人”地址,那么将它添加到您生成的每个可邮寄类中可能会变得很麻烦。相反,您可以在您的 config/mail.php 配置文件中指定一个全局“发件人”地址。如果可邮寄类中没有指定其他“发件人”地址,则将使用此地址:
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'hello@example.com'),
'name' => env('MAIL_FROM_NAME', 'Example'),
],
此外,您可以在您的 config/mail.php 配置文件中定义一个全局“回复到”地址:
'reply_to' => [
'address' => 'example@example.com',
'name' => 'App Name',
],
配置视图
在可邮寄类的 content 方法中,您可以定义 view,或者在渲染电子邮件内容时应使用哪个模板。由于每封电子邮件通常都使用 Blade 模板 来渲染其内容,因此您在构建电子邮件的 HTML 时拥有 Blade 模板引擎的全部功能和便利:
/**
* Get the message content definition.
*/
public function content(): Content
{
return new Content(
view: 'mail.orders.shipped',
);
}
resources/views/mail 目录来存放您的所有电子邮件模板;但是,您可以将它们放置在 resources/views 目录中的任何位置。纯文本电子邮件
如果您想定义电子邮件的纯文本版本,您可以在创建消息的 Content 定义时指定纯文本模板。像 view 参数一样,text 参数应该是一个将用于渲染电子邮件内容的模板名称。您可以自由地定义消息的 HTML 和纯文本版本:
/**
* Get the message content definition.
*/
public function content(): Content
{
return new Content(
view: 'mail.orders.shipped',
text: 'mail.orders.shipped-text'
);
}
为清晰起见,html 参数可以用作 view 参数的别名:
return new Content(
html: 'mail.orders.shipped',
text: 'mail.orders.shipped-text'
);
视图数据
通过公共属性
通常,您会希望将一些数据传递给您的视图,以便在渲染电子邮件的 HTML 时使用。有两种方法可以使数据对您的视图可用。首先,在您的可邮寄类上定义的任何公共属性都将自动对视图可用。因此,例如,您可以将数据传递到可邮寄类的构造函数中,并将该数据设置为在类上定义的公共属性:
<?php
namespace App\Mail;
use App\Models\Order;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Queue\SerializesModels;
class OrderShipped extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*/
public function __construct(
public Order $order,
) {}
/**
* Get the message content definition.
*/
public function content(): Content
{
return new Content(
view: 'mail.orders.shipped',
);
}
}
一旦数据被设置为公共属性,它将自动在您的视图中可用,因此您可以像访问 Blade 模板中的任何其他数据一样访问它:
<div>
Price: {{ $order->price }}
</div>
通过 with 参数:
如果您想在将电子邮件数据发送到模板之前自定义其格式,您可以通过 Content 定义的 with 参数手动将数据传递给视图。通常,您仍然会通过可邮寄类的构造函数传递数据;但是,您应该将此数据设置为 protected 或 private 属性,以便数据不会自动对模板可用:
<?php
namespace App\Mail;
use App\Models\Order;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Queue\SerializesModels;
class OrderShipped extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*/
public function __construct(
protected Order $order,
) {}
/**
* Get the message content definition.
*/
public function content(): Content
{
return new Content(
view: 'mail.orders.shipped',
with: [
'orderName' => $this->order->name,
'orderPrice' => $this->order->price,
],
);
}
}
一旦数据通过 with 参数传递,它将自动在您的视图中可用,因此您可以像访问 Blade 模板中的任何其他数据一样访问它:
<div>
Price: {{ $orderPrice }}
</div>
附件
要向电子邮件添加附件,您将向消息的 attachments 方法返回的数组添加附件。首先,您可以通过向 Attachment 类提供的 fromPath 方法提供文件路径来添加附件:
use Illuminate\Mail\Mailables\Attachment;
/**
* Get the attachments for the message.
*
* @return array<int, \Illuminate\Mail\Mailables\Attachment>
*/
public function attachments(): array
{
return [
Attachment::fromPath('/path/to/file'),
];
}
在向消息附加文件时,您还可以使用 as 和 withMime 方法为附件指定显示名称和/或 MIME 类型:
/**
* Get the attachments for the message.
*
* @return array<int, \Illuminate\Mail\Mailables\Attachment>
*/
public function attachments(): array
{
return [
Attachment::fromPath('/path/to/file')
->as('name.pdf')
->withMime('application/pdf'),
];
}
从磁盘附加文件
如果您已将文件存储在您的 文件系统磁盘 之一上,您可以使用 fromStorage 附件方法将其附加到电子邮件中:
/**
* Get the attachments for the message.
*
* @return array<int, \Illuminate\Mail\Mailables\Attachment>
*/
public function attachments(): array
{
return [
Attachment::fromStorage('/path/to/file'),
];
}
当然,您还可以指定附件的名称和 MIME 类型:
/**
* Get the attachments for the message.
*
* @return array<int, \Illuminate\Mail\Mailables\Attachment>
*/
public function attachments(): array
{
return [
Attachment::fromStorage('/path/to/file')
->as('name.pdf')
->withMime('application/pdf'),
];
}
如果您需要指定除默认磁盘之外的存储磁盘,可以使用 fromStorageDisk 方法:
/**
* Get the attachments for the message.
*
* @return array<int, \Illuminate\Mail\Mailables\Attachment>
*/
public function attachments(): array
{
return [
Attachment::fromStorageDisk('s3', '/path/to/file')
->as('name.pdf')
->withMime('application/pdf'),
];
}
原始数据附件
fromData 附件方法可用于将原始字节字符串作为附件附加。例如,如果您在内存中生成了一个 PDF 并希望将其附加到电子邮件中而不将其写入磁盘,则可以使用此方法。fromData 方法接受一个闭包,该闭包解析原始数据字节以及应分配给附件的名称:
/**
* Get the attachments for the message.
*
* @return array<int, \Illuminate\Mail\Mailables\Attachment>
*/
public function attachments(): array
{
return [
Attachment::fromData(fn () => $this->pdf, 'Report.pdf')
->withMime('application/pdf'),
];
}
内联附件
将内联图像嵌入到电子邮件中通常很麻烦;但是,Laravel 提供了一种方便的方式来将图像附加到您的电子邮件中。要嵌入内联图像,请在您的电子邮件模板中使用 $message 变量上的 embed 方法。Laravel 会自动将 $message 变量提供给您的所有电子邮件模板,因此您无需担心手动传递它:
<body>
Here is an image:
<img src="{{ $message->embed($pathToImage) }}">
</body>
$message 变量在纯文本消息模板中不可用,因为纯文本消息不使用内联附件。嵌入原始数据附件
如果您已经有一个要嵌入到电子邮件模板中的原始图像数据字符串,您可以在 $message 变量上调用 embedData 方法。调用 embedData 方法时,您需要提供一个应分配给嵌入图像的文件名:
<body>
Here is an image from raw data:
<img src="{{ $message->embedData($data, 'example-image.jpg') }}">
</body>
可附加对象
虽然通过简单的字符串路径将文件附加到消息通常就足够了,但在许多情况下,应用程序中可附加的实体由类表示。例如,如果您的应用程序正在将照片附加到消息,您的应用程序也可能有一个表示该照片的 Photo 模型。在这种情况下,简单地将 Photo 模型传递给 attach 方法会不会很方便?可附加对象允许您做到这一点。
首先,在将可附加到消息的对象上实现 Illuminate\Contracts\Mail\Attachable 接口。此接口要求您的类定义一个 toMailAttachment 方法,该方法返回一个 Illuminate\Mail\Attachment 实例:
<?php
namespace App\Models;
use Illuminate\Contracts\Mail\Attachable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Mail\Attachment;
class Photo extends Model implements Attachable
{
/**
* Get the attachable representation of the model.
*/
public function toMailAttachment(): Attachment
{
return Attachment::fromPath('/path/to/file');
}
}
定义可附加对象后,您可以在构建电子邮件消息时从 attachments 方法返回该对象的一个实例:
/**
* Get the attachments for the message.
*
* @return array<int, \Illuminate\Mail\Mailables\Attachment>
*/
public function attachments(): array
{
return [$this->photo];
}
当然,附件数据可以存储在远程文件存储服务上,例如 Amazon S3。因此,Laravel 还允许您从存储在应用程序的 文件系统磁盘 之一上的数据生成附件实例:
// Create an attachment from a file on your default disk...
return Attachment::fromStorage($this->path);
// Create an attachment from a file on a specific disk...
return Attachment::fromStorageDisk('backblaze', $this->path);
此外,您可以通过内存中拥有的数据创建附件实例。为此,请向 fromData 方法提供一个闭包。该闭包应返回表示附件的原始数据:
return Attachment::fromData(fn () => $this->content, 'Photo Name');
Laravel 还提供了其他方法,您可以使用这些方法来自定义您的附件。例如,您可以使用 as 和 withMime 方法来自定义文件的名称和 MIME 类型:
return Attachment::fromPath('/path/to/file')
->as('Photo Name')
->withMime('image/jpeg');
标头
有时您可能需要将附加标头附加到发出的消息。例如,您可能需要设置自定义 Message-Id 或其他任意文本标头。
为此,请在您的可邮寄类上定义一个 headers 方法。headers 方法应返回一个 Illuminate\Mail\Mailables\Headers 实例。此类接受 messageId、references 和 text 参数。当然,您只可以提供您的特定消息所需的参数:
use Illuminate\Mail\Mailables\Headers;
/**
* Get the message headers.
*/
public function headers(): Headers
{
return new Headers(
messageId: 'custom-message-id@example.com',
references: ['previous-message@example.com'],
text: [
'X-Custom-Header' => 'Custom Value',
],
);
}
标签和元数据
一些第三方电子邮件提供商(如 Mailgun 和 Postmark)支持消息“标签”和“元数据”,可用于对您的应用程序发送的电子邮件进行分组和跟踪。您可以通过您的 Envelope 定义将标签和元数据添加到电子邮件中:
use Illuminate\Mail\Mailables\Envelope;
/**
* Get the message envelope.
*
* @return \Illuminate\Mail\Mailables\Envelope
*/
public function envelope(): Envelope
{
return new Envelope(
subject: 'Order Shipped',
tags: ['shipment'],
metadata: [
'order_id' => $this->order->id,
],
);
}
如果您的应用程序正在使用 Mailgun 驱动程序,您可以查阅 Mailgun 的文档以获取有关 标签 和 元数据 的更多信息。同样,也可以查阅 Postmark 文档以获取有关其对标签和元数据支持的更多信息。
如果您的应用程序使用 Amazon SES 发送电子邮件,您应该使用 metadata 方法将 SES“标签” 附加到消息中。
自定义 Symfony 消息
Laravel 的邮件功能由 Symfony Mailer 提供支持。Laravel 允许您注册自定义回调,该回调将在发送消息之前与 Symfony 消息实例一起调用。这让您有机会在发送消息之前对其进行深度自定义。为此,请在您的 Envelope 定义上定义一个 using 参数:
use Illuminate\Mail\Mailables\Envelope;
use Symfony\Component\Mime\Email;
/**
* Get the message envelope.
*/
public function envelope(): Envelope
{
return new Envelope(
subject: 'Order Shipped',
using: [
function (Email $message) {
// ...
},
]
);
}
Markdown 可邮寄类
Markdown 可邮寄消息允许您在可邮寄类中利用 邮件通知 的预构建模板和组件。由于消息是用 Markdown 编写的,因此 Laravel 能够为消息渲染美观、响应迅速的 HTML 模板,同时自动生成纯文本对应物。
生成 Markdown 可邮寄类
要生成具有相应 Markdown 模板的可邮寄类,您可以使用 make:mail Artisan 命令的 --markdown 选项:
php artisan make:mail OrderShipped --markdown=mail.orders.shipped
然后,在可邮寄类的 content 方法中配置可邮寄类 Content 定义时,使用 markdown 参数而不是 view 参数:
use Illuminate\Mail\Mailables\Content;
/**
* Get the message content definition.
*/
public function content(): Content
{
return new Content(
markdown: 'mail.orders.shipped',
with: [
'url' => $this->orderUrl,
],
);
}
编写 Markdown 消息
Markdown 可邮寄类使用 Blade 组件和 Markdown 语法的组合,让您可以在利用 Laravel 预构建的电子邮件 UI 组件的同时轻松构建邮件消息:
<x-mail::message>
# Order Shipped
Your order has been shipped!
<x-mail::button :url="$url">
View Order
</x-mail::button>
Thanks,<br>
{{ config('app.name') }}
</x-mail::message>
按钮组件
按钮组件渲染一个居中的按钮链接。该组件接受两个参数:url 和一个可选的 color。支持的颜色是 primary、success 和 error。您可以在一个消息中添加任意数量的按钮组件:
<x-mail::button :url="$url" color="success">
View Order
</x-mail::button>
面板组件
面板组件在面板中渲染给定的文本块,该面板的背景颜色与消息的其余部分略有不同。这让您可以将注意力吸引到给定的文本块:
<x-mail::panel>
This is the panel content.
</x-mail::panel>
表格组件
表格组件允许您将 Markdown 表格转换为 HTML 表格。该组件接受 Markdown 表格作为其内容。使用默认的 Markdown 表格对齐语法支持表格列对齐:
<x-mail::table>
| Laravel | Table | Example |
| ------------- | :-----------: | ------------: |
| Col 2 is | Centered | $10 |
| Col 3 is | Right-Aligned | $20 |
</x-mail::table>
自定义组件
您可以将所有 Markdown 邮件组件导出到您自己的应用程序中进行自定义。要导出组件,请使用 vendor:publish Artisan 命令发布 laravel-mail 资产标签:
php artisan vendor:publish --tag=laravel-mail
此命令会将 Markdown 邮件组件发布到 resources/views/vendor/mail 目录。mail 目录将包含一个 html 和一个 text 目录,每个目录都包含每个可用组件的各自表示。您可以随意自定义这些组件。
自定义 CSS
导出组件后,resources/views/vendor/mail/html/themes 目录将包含一个 default.css 文件。您可以自定义此文件中的 CSS,您的样式将自动转换为 Markdown 邮件消息的 HTML 表示中的内联 CSS 样式。
如果您想为 Laravel 的 Markdown 组件构建一个全新的主题,您可以将 CSS 文件放置在 html/themes 目录中。命名并保存您的 CSS 文件后,更新应用程序的 config/mail.php 配置文件中的 theme 选项以匹配您的新主题的名称。
要为单个可邮寄类自定义主题,您可以将可邮寄类的 $theme 属性设置为发送该可邮寄类时应使用的主题名称。
发送邮件
要发送消息,请在 Mail Facade 上使用 to 方法。to 方法接受电子邮件地址、用户实例或用户集合。如果您传递一个对象或对象集合,邮件程序将自动使用它们的 email 和 name 属性来确定电子邮件的收件人,因此请确保这些属性在您的对象上可用。指定收件人后,您可以将您的可邮寄类实例传递给 send 方法:
<?php
namespace App\Http\Controllers;
use App\Mail\OrderShipped;
use App\Models\Order;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
class OrderShipmentController extends Controller
{
/**
* Ship the given order.
*/
public function store(Request $request): RedirectResponse
{
$order = Order::findOrFail($request->order_id);
// Ship the order...
Mail::to($request->user())->send(new OrderShipped($order));
return redirect('/orders');
}
}
发送消息时,您不仅限于指定“收件人”。您可以自由地通过将各自的方法链接在一起来设置“收件人”、“抄送人”和“密送人”:
Mail::to($request->user())
->cc($moreUsers)
->bcc($evenMoreUsers)
->send(new OrderShipped($order));
遍历收件人
有时,您可能需要通过遍历收件人/电子邮件地址数组来向收件人列表发送可邮寄类。但是,由于 to 方法会将电子邮件地址附加到可邮寄类的收件人列表,因此循环中的每次迭代都会向每个先前的收件人发送另一封电子邮件。因此,您应该始终为每个收件人重新创建可邮寄类实例:
foreach (['taylor@example.com', 'dries@example.com'] as $recipient) {
Mail::to($recipient)->send(new OrderShipped($order));
}
通过特定邮件程序发送邮件
默认情况下,Laravel 将使用您应用程序的 mail 配置文件中配置为 default 邮件程序的邮件程序发送电子邮件。但是,您可以使用 mailer 方法使用特定的邮件程序配置来发送消息:
Mail::mailer('postmark')
->to($request->user())
->send(new OrderShipped($order));
邮件排队
将邮件消息排队
由于发送电子邮件消息会对应用程序的响应时间产生负面影响,许多开发人员选择将电子邮件消息排队以进行后台发送。Laravel 使用其内置的 统一队列 API 使这变得容易。要将邮件消息排队,请在指定消息收件人后使用 Mail Facade 上的 queue 方法:
Mail::to($request->user())
->cc($moreUsers)
->bcc($evenMoreUsers)
->queue(new OrderShipped($order));
此方法将自动将作业推送到队列中,以便在后台发送消息。您需要在使用此功能之前 配置您的队列。
延迟消息排队
如果您希望延迟发送排队的电子邮件消息,可以使用 later 方法。作为其第一个参数,later 方法接受一个 DateTime 实例,指示何时应发送消息:
Mail::to($request->user())
->cc($moreUsers)
->bcc($evenMoreUsers)
->later(now()->addMinutes(10), new OrderShipped($order));
推送到特定队列
由于使用 make:mail 命令生成的所有可邮寄类都使用 Illuminate\Bus\Queueable Trait,您可以在任何可邮寄类实例上调用 onQueue 和 onConnection 方法,允许您为消息指定连接和队列名称:
$message = (new OrderShipped($order))
->onConnection('sqs')
->onQueue('emails');
Mail::to($request->user())
->cc($moreUsers)
->bcc($evenMoreUsers)
->queue($message);
默认排队
如果您有想要始终排队的可邮寄类,您可以在该类上实现 ShouldQueue 契约。现在,即使您在发送邮件时调用 send 方法,可邮寄类仍将被排队,因为它实现了契约:
use Illuminate\Contracts\Queue\ShouldQueue;
class OrderShipped extends Mailable implements ShouldQueue
{
// ...
}
排队的可邮寄类和数据库事务
当排队的可邮寄类在数据库事务中分派时,它们可能会在数据库事务提交之前被队列处理。发生这种情况时,您在数据库事务期间对模型或数据库记录所做的任何更新可能尚未反映在数据库中。此外,在事务中创建的任何模型或数据库记录可能不存在于数据库中。如果您的可邮寄类依赖于这些模型,则在处理发送排队的可邮寄类的作业时可能会发生意外错误。
如果您的队列连接的 after_commit 配置选项设置为 false,您仍然可以通过在发送邮件消息时调用 afterCommit 方法来指示某个特定的排队可邮寄类应在所有打开的数据库事务提交后分派:
Mail::to($request->user())->send(
(new OrderShipped($order))->afterCommit()
);
或者,您可以从可邮寄类的构造函数中调用 afterCommit 方法:
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class OrderShipped extends Mailable implements ShouldQueue
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*/
public function __construct()
{
$this->afterCommit();
}
}
排队的电子邮件失败
当排队的电子邮件失败时,如果已定义,将调用排队的可邮寄类上的 failed 方法。导致排队的电子邮件失败的 Throwable 实例将传递给 failed 方法:
<?php
namespace App\Mail;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Throwable;
class OrderDelayed extends Mailable implements ShouldQueue
{
use SerializesModels;
/**
* Handle a queued email's failure.
*/
public function failed(Throwable $exception): void
{
// ...
}
}
渲染可邮寄类
有时您可能希望捕获可邮寄类的 HTML 内容而不发送它。为此,您可以调用可邮寄类的 render 方法。此方法将返回可邮寄类的评估 HTML 内容作为字符串:
use App\Mail\InvoicePaid;
use App\Models\Invoice;
$invoice = Invoice::find(1);
return (new InvoicePaid($invoice))->render();
在浏览器中预览可邮寄类
在设计可邮寄类的模板时,在浏览器中像典型的 Blade 模板一样快速预览渲染的可邮寄类很方便。因此,Laravel 允许您直接从路由闭包或控制器返回任何可邮寄类。当返回可邮寄类时,它将在浏览器中渲染和显示,让您可以快速预览其设计,而无需将其发送到实际的电子邮件地址:
Route::get('/mailable', function () {
$invoice = App\Models\Invoice::find(1);
return new App\Mail\InvoicePaid($invoice);
});
本地化可邮寄类
Laravel 允许您以除请求当前区域设置之外的区域设置发送可邮寄类,如果邮件已排队,它甚至会记住此区域设置。
为此,Mail Facade 提供了一个 locale 方法来设置所需的语言。当评估可邮寄类的模板时,应用程序将更改为此区域设置,然后在评估完成后恢复为先前的区域设置:
Mail::to($request->user())->locale('es')->send(
new OrderShipped($order)
);
用户首选区域设置
有时,应用程序会存储每个用户的首选区域设置。通过在一个或多个模型上实现 HasLocalePreference 契约,您可以指示 Laravel 在发送邮件时使用此存储的区域设置:
use Illuminate\Contracts\Translation\HasLocalePreference;
class User extends Model implements HasLocalePreference
{
/**
* Get the user's preferred locale.
*/
public function preferredLocale(): string
{
return $this->locale;
}
}
一旦您实现了该接口,Laravel 将在向模型发送可邮寄类和通知时自动使用首选区域设置。因此,在使用此接口时无需调用 locale 方法:
Mail::to($request->user())->send(new OrderShipped($order));
测试
测试可邮寄类内容
Laravel 提供了多种检查可邮寄类结构的方法。此外,Laravel 还提供了几种方便的方法来测试您的可邮寄类是否包含您期望的内容:
use App\Mail\InvoicePaid;
use App\Models\User;
test('mailable content', function () {
$user = User::factory()->create();
$mailable = new InvoicePaid($user);
$mailable->assertFrom('jeffrey@example.com');
$mailable->assertTo('taylor@example.com');
$mailable->assertHasCc('abigail@example.com');
$mailable->assertHasBcc('victoria@example.com');
$mailable->assertHasReplyTo('tyler@example.com');
$mailable->assertHasSubject('Invoice Paid');
$mailable->assertHasTag('example-tag');
$mailable->assertHasMetadata('key', 'value');
$mailable->assertSeeInHtml($user->email);
$mailable->assertDontSeeInHtml('Invoice Not Paid');
$mailable->assertSeeInOrderInHtml(['Invoice Paid', 'Thanks']);
$mailable->assertSeeInText($user->email);
$mailable->assertDontSeeInText('Invoice Not Paid');
$mailable->assertSeeInOrderInText(['Invoice Paid', 'Thanks']);
$mailable->assertHasAttachment('/path/to/file');
$mailable->assertHasAttachment(Attachment::fromPath('/path/to/file'));
$mailable->assertHasAttachedData($pdfData, 'name.pdf', ['mime' => 'application/pdf']);
$mailable->assertHasAttachmentFromStorage('/path/to/file', 'name.pdf', ['mime' => 'application/pdf']);
$mailable->assertHasAttachmentFromStorageDisk('s3', '/path/to/file', 'name.pdf', ['mime' => 'application/pdf']);
});
use App\Mail\InvoicePaid;
use App\Models\User;
public function test_mailable_content(): void
{
$user = User::factory()->create();
$mailable = new InvoicePaid($user);
$mailable->assertFrom('jeffrey@example.com');
$mailable->assertTo('taylor@example.com');
$mailable->assertHasCc('abigail@example.com');
$mailable->assertHasBcc('victoria@example.com');
$mailable->assertHasReplyTo('tyler@example.com');
$mailable->assertHasSubject('Invoice Paid');
$mailable->assertHasTag('example-tag');
$mailable->assertHasMetadata('key', 'value');
$mailable->assertSeeInHtml($user->email);
$mailable->assertDontSeeInHtml('Invoice Not Paid');
$mailable->assertSeeInOrderInHtml(['Invoice Paid', 'Thanks']);
$mailable->assertSeeInText($user->email);
$mailable->assertDontSeeInText('Invoice Not Paid');
$mailable->assertSeeInOrderInText(['Invoice Paid', 'Thanks']);
$mailable->assertHasAttachment('/path/to/file');
$mailable->assertHasAttachment(Attachment::fromPath('/path/to/file'));
$mailable->assertHasAttachedData($pdfData, 'name.pdf', ['mime' => 'application/pdf']);
$mailable->assertHasAttachmentFromStorage('/path/to/file', 'name.pdf', ['mime' => 'application/pdf']);
$mailable->assertHasAttachmentFromStorageDisk('s3', '/path/to/file', 'name.pdf', ['mime' => 'application/pdf']);
}
正如您所料,“HTML”断言断言您的可邮寄类的 HTML 版本包含给定的字符串,而“文本”断言断言您的可邮寄类的纯文本版本包含给定的字符串。
测试可邮寄类发送
我们建议将可邮寄类的内容测试与断言给定可邮寄类“已发送”给特定用户的测试分开。通常,可邮寄类的内容与您正在测试的代码不相关,简单地断言 Laravel 被指示发送给定可邮寄类就足够了。
您可以使用 Mail Facade 的 fake 方法来阻止邮件实际发送。在调用 Mail Facade 的 fake 方法后,您可以断言可邮寄类被指示发送给用户,甚至检查可邮寄类收到的数据:
<?php
use App\Mail\OrderShipped;
use Illuminate\Support\Facades\Mail;
test('orders can be shipped', function () {
Mail::fake();
// Perform order shipping...
// Assert that no mailables were sent...
Mail::assertNothingSent();
// Assert that a mailable was sent...
Mail::assertSent(OrderShipped::class);
// Assert a mailable was sent twice...
Mail::assertSent(OrderShipped::class, 2);
// Assert a mailable was sent to an email address...
Mail::assertSent(OrderShipped::class, 'example@laravel.com');
// Assert a mailable was sent to multiple email addresses...
Mail::assertSent(OrderShipped::class, ['example@laravel.com', '...']);
// Assert a mailable was not sent...
Mail::assertNotSent(AnotherMailable::class);
// Assert a mailable was sent twice...
Mail::assertSentTimes(OrderShipped::class, 2);
// Assert 3 total mailables were sent...
Mail::assertSentCount(3);
});
<?php
namespace Tests\Feature;
use App\Mail\OrderShipped;
use Illuminate\Support\Facades\Mail;
use Tests\TestCase;
class ExampleTest extends TestCase
{
public function test_orders_can_be_shipped(): void
{
Mail::fake();
// Perform order shipping...
// Assert that no mailables were sent...
Mail::assertNothingSent();
// Assert that a mailable was sent...
Mail::assertSent(OrderShipped::class);
// Assert a mailable was sent twice...
Mail::assertSent(OrderShipped::class, 2);
// Assert a mailable was sent to an email address...
Mail::assertSent(OrderShipped::class, 'example@laravel.com');
// Assert a mailable was sent to multiple email addresses...
Mail::assertSent(OrderShipped::class, ['example@laravel.com', '...']);
// Assert a mailable was not sent...
Mail::assertNotSent(AnotherMailable::class);
// Assert a mailable was sent twice...
Mail::assertSentTimes(OrderShipped::class, 2);
// Assert 3 total mailables were sent...
Mail::assertSentCount(3);
}
}
如果您正在将可邮寄类排队以在后台发送,则应使用 assertQueued 方法而不是 assertSent:
Mail::assertQueued(OrderShipped::class);
Mail::assertNotQueued(OrderShipped::class);
Mail::assertNothingQueued();
Mail::assertQueuedCount(3);
您可以将闭包传递给 assertSent、assertNotSent、assertQueued 或 assertNotQueued 方法,以断言已发送通过给定“真值测试”的可邮寄类。如果至少有一个通过给定真值测试的可邮寄类被发送,则断言将成功:
Mail::assertSent(function (OrderShipped $mail) use ($order) {
return $mail->order->id === $order->id;
});
调用 Mail Facade 的断言方法时,提供的闭包接受的可邮寄类实例公开了用于检查可邮寄类的有用方法:
Mail::assertSent(OrderShipped::class, function (OrderShipped $mail) use ($user) {
return $mail->hasTo($user->email) &&
$mail->hasCc('...') &&
$mail->hasBcc('...') &&
$mail->hasReplyTo('...') &&
$mail->hasFrom('...') &&
$mail->hasSubject('...') &&
$mail->usesMailer('ses');
});
可邮寄类实例还包括几个用于检查可邮寄类上附件的有用方法:
use Illuminate\Mail\Mailables\Attachment;
Mail::assertSent(OrderShipped::class, function (OrderShipped $mail) {
return $mail->hasAttachment(
Attachment::fromPath('/path/to/file')
->as('name.pdf')
->withMime('application/pdf')
);
});
Mail::assertSent(OrderShipped::class, function (OrderShipped $mail) {
return $mail->hasAttachment(
Attachment::fromStorageDisk('s3', '/path/to/file')
);
});
Mail::assertSent(OrderShipped::class, function (OrderShipped $mail) use ($pdfData) {
return $mail->hasAttachment(
Attachment::fromData(fn () => $pdfData, 'name.pdf')
);
});
您可能已经注意到,有两个方法可以断言邮件未发送:assertNotSent 和 assertNotQueued。有时您可能希望断言没有发送或排队任何邮件。为此,您可以使用 assertNothingOutgoing 和 assertNotOutgoing 方法:
Mail::assertNothingOutgoing();
Mail::assertNotOutgoing(function (OrderShipped $mail) use ($order) {
return $mail->order->id === $order->id;
});
邮件和本地开发
在开发发送电子邮件的应用程序时,您可能不想实际向真实的电子邮件地址发送电子邮件。Laravel 提供了几种在本地开发期间“禁用”电子邮件实际发送的方法。
日志驱动程序
log 邮件驱动程序将所有电子邮件消息写入您的日志文件以供检查,而不是发送您的电子邮件。通常,此驱动程序仅在本地开发期间使用。有关按环境配置应用程序的更多信息,请查看 配置文档。
HELO / Mailtrap / Mailpit
或者,您可以使用像 HELO 或 Mailtrap 这样的服务和 smtp 驱动程序将您的电子邮件消息发送到“虚拟”邮箱,您可以在其中在真实的电子邮件客户端中查看它们。这种方法的好处是允许您在 Mailtrap 的消息查看器中实际检查最终的电子邮件。
如果您正在使用 Laravel Sail,您可以使用 Mailpit 预览您的消息。当 Sail 运行时,您可以在以下位置访问 Mailpit 界面:http://localhost:8025。
使用全局 to 地址
最后,您可以通过调用 Mail Facade 提供的 alwaysTo 方法来指定全局“收件人”地址。通常,应在应用程序的服务提供者之一的 boot 方法中调用此方法:
use Illuminate\Support\Facades\Mail;
/**
* Bootstrap any application services.
*/
public function boot(): void
{
if ($this->app->environment('local')) {
Mail::alwaysTo('taylor@example.com');
}
}
事件
Laravel 在发送邮件消息时会分派两个事件。在发送消息之前分派 MessageSending 事件,而在发送消息之后分派 MessageSent 事件。请记住,这些事件是在邮件正在发送时分派的,而不是在它排队时分派的。您可以在应用程序中为这些事件创建 事件监听器:
use Illuminate\Mail\Events\MessageSending;
// use Illuminate\Mail\Events\MessageSent;
class LogMessage
{
/**
* Handle the event.
*/
public function handle(MessageSending $event): void
{
// ...
}
}
自定义传输
Laravel 包含各种邮件传输;但是,您可能希望编写自己的传输来通过 Laravel 不开箱即用支持的其他服务发送电子邮件。首先,定义一个扩展 Symfony\Component\Mailer\Transport\AbstractTransport 类的类。然后,在您的传输上实现 doSend 和 __toString 方法:
<?php
namespace App\Mail;
use MailchimpTransactional\ApiClient;
use Symfony\Component\Mailer\SentMessage;
use Symfony\Component\Mailer\Transport\AbstractTransport;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\MessageConverter;
class MailchimpTransport extends AbstractTransport
{
/**
* Create a new Mailchimp transport instance.
*/
public function __construct(
protected ApiClient $client,
) {
parent::__construct();
}
/**
* {@inheritDoc}
*/
protected function doSend(SentMessage $message): void
{
$email = MessageConverter::toEmail($message->getOriginalMessage());
$this->client->messages->send(['message' => [
'from_email' => $email->getFrom(),
'to' => collect($email->getTo())->map(function (Address $email) {
return ['email' => $email->getAddress(), 'type' => 'to'];
})->all(),
'subject' => $email->getSubject(),
'text' => $email->getTextBody(),
]]);
}
/**
* Get the string representation of the transport.
*/
public function __toString(): string
{
return 'mailchimp';
}
}
定义自定义传输后,您可以通过 Mail Facade 提供的 extend 方法注册它。通常,这应该在您的应用程序的 AppServiceProvider 的 boot 方法中完成。一个 $config 参数将传递给提供给 extend 方法的闭包。此参数将包含应用程序的 config/mail.php 配置文件中为邮件程序定义的配置数组:
use App\Mail\MailchimpTransport;
use Illuminate\Support\Facades\Mail;
use MailchimpTransactional\ApiClient;
/**
* Bootstrap any application services.
*/
public function boot(): void
{
Mail::extend('mailchimp', function (array $config = []) {
$client = new ApiClient;
$client->setApiKey($config['key']);
return new MailchimpTransport($client);
});
}
定义并注册您的自定义传输后,您可以在应用程序的 config/mail.php 配置文件中创建一个使用新传输的邮件程序定义:
'mailchimp' => [
'transport' => 'mailchimp',
'key' => env('MAILCHIMP_API_KEY'),
// ...
],
附加的 Symfony 传输
Laravel 包含对某些现有 Symfony 维护的邮件传输(如 Mailgun 和 Postmark)的支持。但是,您可能希望通过对附加的 Symfony 维护的传输的支持来扩展 Laravel。您可以通过 Composer 引入必要的 Symfony 邮件程序并向 Laravel 注册传输来做到这一点。例如,您可以安装和注册“Brevo”(以前的“Sendinblue”)Symfony 邮件程序:
composer require symfony/brevo-mailer symfony/http-client
安装 Brevo 邮件程序包后,您可以将 Brevo API 凭据的条目添加到应用程序的 services 配置文件中:
'brevo' => [
'key' => env('BREVO_API_KEY'),
],
接下来,您可以使用 Mail Facade 的 extend 方法向 Laravel 注册传输。通常,这应该在服务提供者的 boot 方法中完成:
use Illuminate\Support\Facades\Mail;
use Symfony\Component\Mailer\Bridge\Brevo\Transport\BrevoTransportFactory;
use Symfony\Component\Mailer\Transport\Dsn;
/**
* Bootstrap any application services.
*/
public function boot(): void
{
Mail::extend('brevo', function () {
return (new BrevoTransportFactory)->create(
new Dsn(
'brevo+api',
'default',
config('services.brevo.key')
)
);
});
}
注册传输后,您可以在应用程序的 config/mail.php 配置文件中创建一个使用新传输的邮件程序定义:
'brevo' => [
'transport' => 'brevo',
// ...
],