Laravel发送邮件需先配置mail.php和.env文件,再创建邮件类与Blade视图,最后通过Mail门面发送;支持队列异步处理、附件添加及Markdown模板。

Laravel 中发送邮件,其实就是利用框架提供的 Mail 组件,简化了传统 PHP 发送邮件的复杂流程。核心在于配置好邮件服务,然后使用 Mail facade 或者 Mailer 类来构建和发送邮件。
配置邮件服务,编写邮件逻辑,然后发送,就这么简单。
Laravel 集成邮件发送功能,主要涉及以下几个步骤:
配置邮件服务
首先,你需要配置 config/mail.php 文件。这个文件定义了 Laravel 应用使用的邮件驱动、主机、端口、加密方式、用户名和密码等信息。
常见的邮件驱动有 smtp、sendmail、mailgun、mandrill、ses、log 和 array。smtp 适用于大多数场景,你需要根据你选择的邮件服务提供商(例如:Gmail, 阿里云邮件推送等)提供的信息来配置 SMTP 服务器。
例如,使用 Gmail 的配置如下:
'mailers' => [
'smtp' => [
'transport' => 'smtp',
'host' => env('MAIL_HOST', 'smtp.gmail.com'),
'port' => env('MAIL_PORT', 587),
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'), // 你的 Gmail 邮箱地址
'password' => env('MAIL_PASSWORD'), // 你的 Gmail 邮箱密码或应用专用密码
'timeout' => null,
'local_domain' => env('MAIL_EHLO_DOMAIN'),
],
],
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'hello@example.com'), // 默认发件人邮箱
'name' => env('MAIL_FROM_NAME', 'Example'), // 默认发件人名称
],记得在 .env 文件中设置相应的环境变量,比如 MAIL_HOST、MAIL_PORT、MAIL_USERNAME 和 MAIL_PASSWORD。 特别注意,Gmail 可能需要你启用 "允许不太安全的应用访问" 或者设置应用专用密码。
创建邮件类
接下来,你需要创建一个邮件类,用于定义邮件的内容和格式。可以使用 Artisan 命令 php artisan make:mail YourMailClass 来生成一个邮件类。
例如,创建一个名为 OrderShipped 的邮件类:
php artisan make:mail OrderShipped
这会在 app/Mail 目录下生成 OrderShipped.php 文件。
打开 OrderShipped.php 文件,你会看到一个构造函数和一个 build 方法。在构造函数中,你可以传递需要在邮件模板中使用的数据。在 build 方法中,你可以指定邮件的主题、发件人、收件人、邮件视图等。
<?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
{
use Queueable, SerializesModels;
public $order;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($order)
{
$this->order = $order;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->from('example@example.com', 'Example')
->subject('Your Order Has Shipped!')
->view('emails.orders.shipped')
->with([
'order_id' => $this->order->id,
]);
}
}在这个例子中,我们传递了一个 $order 对象到构造函数中,并在 build 方法中使用了 view 方法来指定邮件视图,并使用 with 方法将数据传递给视图。
创建邮件视图
邮件视图是一个 Blade 模板,用于定义邮件的 HTML 内容。你需要在 resources/views/emails/orders 目录下创建一个名为 shipped.blade.php 的文件。
<!-- resources/views/emails/orders/shipped.blade.php -->
<h1>Order Shipped!</h1>
<p>Your order with ID {{ $order_id }} has been shipped.</p>
<p>Thank you for your order!</p>发送邮件
最后,你可以使用 Mail facade 或 Mailer 类来发送邮件。
使用 Mail facade:
use Illuminate\Support\Facades\Mail;
use App\Mail\OrderShipped;
$order = App\Models\Order::find(1);
Mail::to('recipient@example.com')->send(new OrderShipped($order));或者,使用 Mailer 类:
use Illuminate\Contracts\Mail\Mailer;
use App\Mail\OrderShipped;
$order = App\Models\Order::find(1);
app(Mailer::class)->to('recipient@example.com')->send(new OrderShipped($order));这两种方式都会发送一封邮件到 recipient@example.com,邮件内容是 emails.orders.shipped 视图渲染后的 HTML。
如何处理邮件发送失败?
Laravel 提供了队列功能,可以将邮件发送任务放入队列中异步执行。这样可以避免邮件发送失败阻塞用户请求,并提高应用的响应速度。
你可以使用 Artisan 命令 php artisan queue:work 来启动队列监听器。
此外,你还可以使用 try-catch 块来捕获邮件发送异常,并进行相应的处理,例如记录日志、重试发送等。
use Illuminate\Support\Facades\Mail;
use App\Mail\OrderShipped;
use Exception;
$order = App\Models\Order::find(1);
try {
Mail::to('recipient@example.com')->send(new OrderShipped($order));
} catch (Exception $e) {
\Log::error('Failed to send email: ' . $e->getMessage());
// 可以选择重试发送,或者通知管理员
}如何测试邮件发送?
在开发环境中,你可能不想真的发送邮件。Laravel 提供了两种测试邮件发送的方式:
log 驱动:将 config/mail.php 中的 driver 设置为 log。这样,所有发送的邮件都会被记录到 Laravel 的日志文件中。array 驱动:将 config/mail.php 中的 driver 设置为 array。这样,所有发送的邮件都会被存储到一个内存数组中,你可以使用 Mail::failures() 方法来检查是否有邮件发送失败。邮件模板最佳实践是什么?
如何发送带附件的邮件?
在邮件类的 build 方法中使用 attach 方法来添加附件。
public function build()
{
return $this->from('example@example.com', 'Example')
->subject('Your Order Has Shipped!')
->view('emails.orders.shipped')
->attach('/path/to/your/file.pdf', [
'as' => 'order_details.pdf',
'mime' => 'application/pdf',
]);
}attach 方法接受文件路径作为第一个参数,可选的第二个参数是一个数组,用于指定附件的名称和 MIME 类型。
如何使用 Markdown 模板?
Laravel 提供了 Markdown 邮件模板,可以让你使用 Markdown 语法来编写邮件内容。
首先,你需要创建一个 Markdown 邮件模板,例如 resources/views/emails/orders/shipped.md:
# Order Shipped!
Your order with ID {{ $order_id }} has been shipped.
Thank you for your order!然后,在邮件类的 build 方法中使用 markdown 方法来指定 Markdown 邮件模板:
public function build()
{
return $this->from('example@example.com', 'Example')
->subject('Your Order Has Shipped!')
->markdown('emails.orders.shipped')
->with([
'order_id' => $this->order->id,
]);
}Laravel 会自动将 Markdown 模板转换为 HTML 邮件内容。
以上就是Laravel如何发送邮件_邮件发送功能集成的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号