如何检查邮件是否成功发送并在laravel通知中保存状态?

如何检查邮件是否成功发送并在laravel通知中保存状态?,laravel,laravel-mail,laravel-notification,Laravel,Laravel Mail,Laravel Notification,我正在使用laravel通知发送电子邮件并保存到数据库。发送电子邮件后如何检查邮件状态以及如何将其保存在通知表中 public function via($notifiable) { return ['mail', DbChannel::class]; } public function toMail($notifiable) { return (new MailMessage) ->from('test@example.com', 'Example')

我正在使用laravel通知发送电子邮件并保存到数据库。发送电子邮件后如何检查邮件状态以及如何将其保存在通知表中

public function via($notifiable)
{
    return ['mail', DbChannel::class];
}

public function toMail($notifiable)
{
    return (new MailMessage)
        ->from('test@example.com', 'Example')
        ->line('...');
}

要处理这个问题,您可以定义侦听器和事件。 首先在App\Providers\EventServiceProvider中注册它

protected $listen = [
    'App\Events\Event' => [
        'App\Listeners\EventListener',
    ],
    'Illuminate\Mail\Events\MessageSending' => [
        'App\Listeners\LogSendingMessage',
    ],
    'Illuminate\Mail\Events\MessageSent' => [
        'App\Listeners\LogSentMessage',
    ],
];
然后在App\Listeners\LogSendingMessage中保存发送状态。比如说

   public function handle(MessageSending $event)
{
    $notification = Notification::where('id',$event->data['notification']->id)->first();
    $notification->status = "Sending";
    $notification->save();
}

public function failed(MessageSending $event, $exception)
{
    $notification = Notification::where('id',$event->data['notification']->id)->first();
    $notification->status = "Sending Event Failed";
    $notification->save();
}
也适用于LogSentMessage。。。。 有关更多信息,请参阅此链接
要处理此问题,您可以定义侦听器和事件。 首先在App\Providers\EventServiceProvider中注册它

protected $listen = [
    'App\Events\Event' => [
        'App\Listeners\EventListener',
    ],
    'Illuminate\Mail\Events\MessageSending' => [
        'App\Listeners\LogSendingMessage',
    ],
    'Illuminate\Mail\Events\MessageSent' => [
        'App\Listeners\LogSentMessage',
    ],
];
然后在App\Listeners\LogSendingMessage中保存发送状态。比如说

   public function handle(MessageSending $event)
{
    $notification = Notification::where('id',$event->data['notification']->id)->first();
    $notification->status = "Sending";
    $notification->save();
}

public function failed(MessageSending $event, $exception)
{
    $notification = Notification::where('id',$event->data['notification']->id)->first();
    $notification->status = "Sending Event Failed";
    $notification->save();
}
也适用于LogSentMessage。。。。 有关更多信息,请参阅此链接

这是否回答了您的问题?不,我正在使用laravel通知,我想在通过邮件通道发送到数据库后保存电子邮件状态…您的邮件驱动程序是什么?smtp,我使用gmail进行测试解决方案?这是否回答了您的问题?不,我正在使用laravel通知,我想在通过邮件通道发送到数据库后保存电子邮件状态…您的邮件驱动程序是什么?smtp,我使用gmail进行测试解决方案?