You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改Laravel Breeze将邮件改为通过Jobs/队列异步发送

Laravel 10 + Breeze 异步发送验证/找回密码邮件

1. 配置队列环境

  • 修改.env文件,将队列连接改为非同步驱动(以database为例):
    QUEUE_CONNECTION=database
    
  • 生成队列任务表并执行迁移:
    php artisan queue:table
    php artisan migrate
    
  • 启动队列工作进程(开发环境):
    php artisan queue:work
    
    生产环境建议用Supervisor或类似工具守护队列进程,避免进程意外退出。

2. 让邮件通知支持队列

Laravel的通知类只需实现ShouldQueue接口,就能自动转为异步发送,无需修改控制器原有逻辑:

验证邮件通知

编辑app/Notifications/VerifyEmail.php,添加队列支持:

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class VerifyEmail extends Notification implements ShouldQueue
{
    use Queueable;

    // 原有的类代码保持不变
}

找回密码邮件通知

编辑app/Notifications/ResetPassword.php,同样添加队列支持:

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class ResetPassword extends Notification implements ShouldQueue
{
    use Queueable;

    // 原有的类代码保持不变
}

3. 自定义队列行为(可选)

如果需要指定队列名称或延迟发送,可以在通知类的构造方法中添加配置:

public function __construct()
{
    $this->onQueue('emails'); // 指定邮件任务专属队列
    $this->delay(now()->addMinutes(5)); // 延迟5分钟发送
}

测试验证

现在注册新用户或请求重置密码,页面会立即响应,邮件发送任务会被放入队列,由队列工作进程异步处理。

内容的提问来源于stack exchange,提问作者Rohan Prajapati

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 02:22:37