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

Laravel邮件发送队列配置无效排查:已做相关设置但未生效

邮件队列无法正常工作的排查方案

问题描述

我已将.env文件中的QUEUE_CONNECTION设置为database,将邮件发送方式从send改为queue,且给CertificateEmail类实现了ShouldQueue接口,但邮件队列仍无法正常工作,不清楚遗漏了什么配置。

相关代码

SendMail控制器

public function sendEmail(Request $request)
{
    $users = StudentApplicants::whereIn("id", $request->ids)->get();

    foreach ($users as $key => $user) {
        $data = [
            'fname' => $user->users->first_name,
            'mname' => $user->users->middle_name,
            'lname' => $user->users->last_name,
            'gwa'  => $user->gwa,
            'sy'  => $user->school_year
        ];

        $qrcode = base64_encode(QrCode::format('svg')->color(128, 0, 0)->size(200)->errorCorrection('H')->generate($user->users->stud_num));
        $pdf = app('dompdf.wrapper');
        $pdf->loadView('admin.send-awardees-certificates.certificate', $data, array('qrcode' => $qrcode));
        $pdf->setPaper('A4', 'landscape');
        Mail::to(config('mail.notification_recipient'))->queue(new CertificateEmail($user, $pdf));
    }
    return response()->json(['success' => 'Send email successfully. Refresh the page']);
}

邮件类CertificateEmail

class CertificateEmail extends Mailable implements ShouldQueue
{
use Queueable, SerializesModels;
public $pdf;
public $user;

/**
 * Create a new message instance.
 *
 * @return void
 */
public function __construct($user, $pdf)
{
    $this->pdf = $pdf;
    $this->user = $user;
}

/**
 * Build the message.
 *
 * @return $this
 */
public function build()
{
    // return $this->view('view.name');

    return $this->from(env('info@gmail.com'))
        ->subject('Certificate from ABC EFG')
        ->view('email.certificate-email')
        ->attachData($this->pdf->output(), 'stock_report.pdf');
}
}

排查关键点

  • 生成队列数据表:运行php artisan queue:table创建jobs表迁移文件,再执行php artisan migrate完成建表,同时会自动生成failed_jobs表用于存储失败任务
  • 启动队列Worker:队列任务不会自动执行,必须手动启动Worker进程,开发环境可运行php artisan queue:work或php artisan queue:listen;生产环境建议使用Supervisor等工具守护Worker进程,避免进程意外终止
  • 修复PDF序列化问题:Dompdf实例无法被队列正确序列化,建议在控制器中先生成PDF内容字符串,再传递给邮件类:
    // 控制器中修改
    $pdfContent = $pdf->output();
    Mail::to(config('mail.notification_recipient'))->queue(new CertificateEmail($user, $pdfContent));
    
    然后在邮件类中调整:
    // 邮件类中修改
    public $pdfContent;
    public function __construct($user, $pdfContent)
    {
        $this->pdfContent = $pdfContent;
        $this->user = $user;
    }
    public function build()
    {
        return $this->from('info@gmail.com')
            ->subject('Certificate from ABC EFG')
            ->view('email.certificate-email')
            ->attachData($this->pdfContent, 'stock_report.pdf');
    }
    
  • 清除配置缓存:运行php artisan config:clear确保.env中的QUEUE_CONNECTION配置生效
  • 修正发件人地址:env('info@gmail.com')写法错误,应直接写邮箱地址'info@gmail.com',或使用正确的环境变量名如env('MAIL_FROM_ADDRESS', 'info@gmail.com')
  • 查看失败任务:运行php artisan queue:failed查看失败任务详情,或查看Laravel日志文件(storage/logs/laravel.log)获取具体错误信息

内容的提问来源于stack exchange,提问作者Sharmae Reyes

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 02:15:38