【问题标题】:Get Message-ID from Swift Mailer callback function Laravel Error从 Swift Mailer 回调函数 Laravel 错误中获取 Message-ID
【发布时间】:2021-01-21 21:00:44
【问题描述】:

我已经被困在这很长一段时间了。我正在尝试通过回调函数提取电子邮件的 messageID 并存储在数据库中以备后用。我在基于 gmail 的服务器中模拟了相同的条件,它可以工作。但我不认为电子邮件服务器与任何事情有关。这是我尝试提取 messageID 的可邮寄类:

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use App\EmailLog;
use App\EmailLogDetail;

class GenericSendEmailMailable extends Mailable
{
    //use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     *
     * @return void
     */

    private $applicationTemplate;
    public $subject;
    private $data;
    private $emailLogId;

    public function __construct(String $applicationTemplate,Array $data,String $subject, $emailLogId)
    {
        //
        $this->applicationTemplate = $applicationTemplate;
        $this->data = $data;
        $this->subject = $subject;
        $this->emailLogId = $emailLogId;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        $emailLogId = $this->emailLogId;
        $view = $this->applicationTemplate;
        $data = $this->data;
        $subject = $this->subject;
        return $this->subject($subject)
                    ->view($view,$data)
                    ->withSwiftMessage(function ($message) use ($emailLogId){
                        $emailLog = EmailLog::where('id', $emailLogId)->first();
                        if(isset($emailLog->email_log_details)){
                            foreach($emailLog->email_log_details as $emailLogDetail){
                                $emailLogDetail->message_id = $message->getId();
                                $emailLogDetail->save();
                            }
                        }
                    });
    }
}

这里是 $message->getId();应该可以正常工作,但是当我发送邮件并检查数据库时,这不是原因,未存储消息 ID。顺便说一句,电子邮件发送操作是作为作业分派的。关于为什么这不起作用的任何想法?

【问题讨论】:

  • 查看 storage/log/laravel.log 中的日志

标签: php laravel email


【解决方案1】:

如果有人对未来感兴趣,这是由于工作中的繁重处理。我正面临执行超时。我做了两件事,首先我在派遣工作之前做了一些琐碎的处理,而不是在工作本身。最后我通过添加来增加超时时间

public $timeout = 300;

在工作类中。此外,我将我正在使用的驱动程序中的“retry_after”增加到比实际超时(在我的情况下为 320)多一点。
识别队列问题的最佳方法是观察 worker.log 文件并确定是否有任何作业失败。如果失败,可以通过在数据库中创建 failed_jobs 表来捕获原因(或异常)。

【讨论】:

    猜你喜欢
    • 2019-09-17
    • 2017-07-01
    • 2020-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多