dsla94915 2018-04-17 06:02
浏览 122

Laravel Mailable队列,如何将自定义数据传递给MessageSent事件

I am using Laravel Mailable to send email, and I want to log the email that was successfully sent.

Laravel Mailable have default event that was fired after the email was sent

https://laravel.com/docs/5.6/mail#events

So I hook my listener to this event

protected $listen = [
        'App\Events\Event' => [
            'App\Listeners\EventListener',
        ],
        'Illuminate\Mail\Events\MessageSent' => [
            'App\Listeners\LogSentEmailNotification',
        ],
    ];

Listener handler

public function handle(MessageSent $event)
{

    //get extra data 
    $job_request_id = $event->message->job_request_id;

    $message = $event->message;

    $data = [
        'job_request_id' => $job_request_id,
        'to' => $message->getHeaders()->get('To'),
        'from' => $message->getHeaders()->get('From'),
        'cc' => $message->getHeaders()->get('Cc'),
        'bcc' => $message->getHeaders()->get('Bcc'),
        'subject' => $message->getHeaders()->get('Subject')->getFieldBody(),
        'body' => $message->getBody(),
    ];

    $email_notification_log = $this->email_notification_log->create($data);
}

The extra data job_request_id is passed from the build() method in Mailable class, CustomEmailNotification.php

class CustomEmailNotification extends Mailable implements ShouldQueue
{

    public function build()
    {
        $job_request_id = 1;

        //pass extra data mail message            

        $this->withSwiftMessage(function ($message) use($job_request_id){
            $message->job_request_id = $job_request_id;
        });
    }
}

Right now this line on Listener class is working fine without queue, however when using queue it will return null

 //get extra data 
 $job_request_id = $event->message->job_request_id;
 var_dump($job_request_id);
 //null when using queue

Question is, what is the correct way to pass custom data to MailSent event when using queue?

Or is there possibility job_request_id is lost when using queue and pass to withSwiftMessage(), so the Event Listener just received null value?

Thanks

  • 写回答

3条回答 默认 最新

  • dosi8657 2018-05-25 04:41
    关注

    If you're using Supervisor as queue worker. Try restarting all the process. Supervisor must be reloaded to reflect the recent changes from your code.

    sudo supervisorctl restart all
    
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP