Laravel Mail::send() 方法发送空邮件,排队时记录模板

Laravel Mail::send() method sends empty email, and logs template when queued

提问人:DennissHun 提问时间:10/24/2023 更新时间:10/24/2023 访问量:23

问:

我在 Laravel 队列下发送电子邮件时遇到了问题。 我有一个控制器,比如

public function myFunction()
{
    .
    .

    SendWarrantyCardByEmail::dispatch(
        $request['card_number'],
        $request['email'],
        $request['is_foreign'],
        $this->warrantyCardFactory
    );
    .
    .
    .
}

我有 Job,其句柄方法如下:

public function handle(WarrantyCardFactory $warrantyCardFactory): void
{
    $firstItem = WarrantyCard::cardNumber($this->cardNumber)
                            ->first();

    $pdfString = $warrantyCardFactory->getPdfasString($this->cardNumber);

    App::setLocale(SelectableCurrency::getLocaleByCurrency($firstItem->currency));
    Mail::send(
        'emails.warranty_card.warranty_send',
        ['firstItem' => $firstItem],
        function (Message $message) use ($pdfString, $firstItem) {
            $message->to($this->email, $this->email);
            $message->from(config('mail.from.address'), config('mail.from.name'));
            $message->subject(trans('warranty_card.warranty_card_arrived'));
            $message->attachData($pdfString, $firstItem->file_name);
        }
    );
}

当然,$warrantyCardFactory AppServiceProvider 很好地解决了这个问题。

我可以将环境变量设置为

QUEUE_CONNECTION=sync

在这种情况下,一切正常。附件可以下载,嵌入图像可见,模板显示良好。 但是当我把它改成

QUEUE_CONNECTION=database

该模板在电子邮件中丢失,并在主管的stdout_logfile中显示为登录。 主管配置如下:

  [program:myprogram]
  process_name=%(program_name)s_%(process_num)02d
  command=php8.2 <myPath>artisan queue:work database --sleep=3 --tries=3 --max-time=1800
  autostart=true
  autorestart=true
  stopasgroup=true
  killasgroup=true
  user=vagrant
  numprocs=1
  redirect_stderr=true
  stdout_logfile=<myPath>/storage/logs/queueworker.log
  stopwaitsecs=3600

当我尝试从命令行运行它时

artisan queue:work database --sleep=3 --tries=3 --max-time=1800 autostart=true

结果完全相同:电子邮件与附件一起到达,似乎没有错误,但模板丢失,并打印在日志中。

我试过了

  • 将电子邮件外包给 Mailable
  • outsoure email as qued Mailable
  • 删除 Job,并直接将 Mailable 用作排队

但结果是一样的。 我怀疑,当视图被渲染时,有一个问题,但没有找到它,因为它很难调试。

也许我缺少设置或 .env 变量。 有谁知道问题出在哪里?

Laravel 电子邮件 查看 队列

评论


答: 暂无答案