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

Laravel 5.8中Swift Mailer执行queue:work时自定义SMTP配置失效

问题:Laravel 5.8队列执行时Swift Mailer自定义SMTP配置失效

在Laravel 5.8中使用Swift Mailer实现按用户SMTP配置发送邮件,手动执行时一切正常,但通过php artisan queue:work执行队列任务时,邮件配置会自动恢复为默认设置,跳过自定义用户配置。

原代码

if ($own_stmp) {
    $transport = (new Swift_SmtpTransport($user->mail_host, $user->mail_port))
        ->setUsername($user->mail_username)
        ->setPassword($user->mail_password);
} else {
    $transport = (new Swift_SmtpTransport(\Config::get('mail.host'), \Config::get('mail.port')))
        ->setUsername(\Config::get('mail.username'))
        ->setPassword(\Config::get('mail.password'));
}

// Create the Mailer using your created Transport
$mailer = new Swift_Mailer($transport);
    
// Create a message
if ($own_stmp) {
    $message = (new Swift_Message($subject))
        ->setFrom([$user->mail_from_address => $agent->name])
        ->setTo([$lead->email => $lead->name])
        ->setReplyTo($agent->email)
        ->addPart($content, 'text/html');
} else {
    $message = (new Swift_Message($subject))
        ->setFrom([\Config::get('mail.from.address') => $agent->name])
        ->setTo([$lead->email => $lead->name])
        ->setReplyTo($agent->email)
        ->addPart($content, 'text/html');
}

// Send the message
$result = $mailer->send($message);

问题原因

  1. 变量传递/序列化问题:队列任务异步执行时,若未正确传递$own_smtp、$user等关键变量,或Eloquent模型序列化后未正确加载自定义SMTP属性,会导致代码进入默认配置分支。
  2. 变量名拼写错误:原代码中使用$own_stmp(应为$own_smtp),虽手动执行无影响,但可能在队列任务中因变量名不一致导致逻辑判断错误。
  3. 队列Worker未重启:修改任务代码后未重启队列Worker,导致Worker仍使用旧代码执行任务。

解决方案

1. 规范队列任务的参数传递与序列化

创建标准的队列任务类,确保所有必要参数被正确传递并序列化:

<?php

namespace App\Jobs;

use App\User;
use App\Agent;
use App\Lead;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Swift_SmtpTransport;
use Swift_Mailer;
use Swift_Message;

class SendCustomEmail implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $ownSmtp;
    protected $user;
    protected $agent;
    protected $lead;
    protected $subject;
    protected $content;

    public function __construct(bool $ownSmtp, User $user, Agent $agent, Lead $lead, string $subject, string $content)
    {
        $this->ownSmtp = $ownSmtp;
        $this->user = $user;
        $this->agent = $agent;
        $this->lead = $lead;
        $this->subject = $subject;
        $this->content = $content;
    }

    public function handle()
    {
        // 添加日志排查关键变量,确认数据是否正确传递
        \Log::info('队列邮件任务参数', [
            'own_smtp' => $this->ownSmtp,
            'user_mail_host' => $this->user->mail_host,
            'default_host' => \Config::get('mail.host')
        ]);

        if ($this->ownSmtp) {
            $transport = (new Swift_SmtpTransport($this->user->mail_host, $this->user->mail_port))
                ->setUsername($this->user->mail_username)
                ->setPassword($this->user->mail_password);
        } else {
            $transport = (new Swift_SmtpTransport(\Config::get('mail.host'), \Config::get('mail.port')))
                ->setUsername(\Config::get('mail.username'))
                ->setPassword(\Config::get('mail.password'));
        }

        $mailer = new Swift_Mailer($transport);
        
        if ($this->ownSmtp) {
            $message = (new Swift_Message($this->subject))
                ->setFrom([$this->user->mail_from_address => $this->agent->name])
                ->setTo([$this->lead->email => $this->lead->name])
                ->setReplyTo($this->agent->email)
                ->addPart($this->content, 'text/html');
        } else {
            $message = (new Swift_Message($this->subject))
                ->setFrom([\Config::get('mail.from.address') => $this->agent->name])
                ->setTo([$this->lead->email => $this->lead->name])
                ->setReplyTo($this->agent->email)
                ->addPart($this->content, 'text/html');
        }

        $mailer->send($message);
    }
}

2. 修正变量名拼写错误

将原代码中的$own_stmp统一改为$own_smtp,避免逻辑判断时的变量名不一致问题。

3. 重启队列Worker

修改任务代码后,必须重启队列Worker以加载新代码:

php artisan queue:restart
# 或停止旧Worker后启动新的
php artisan queue:work

4. 调用队列任务

在业务代码中通过dispatch方法触发队列任务:

use App\Jobs\SendCustomEmail;

// 传递所有必要参数
SendCustomEmail::dispatch($own_smtp, $user, $agent, $lead, $subject, $content);

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 17:42:25