首页 > 解决方案 > Laravel 5.4 队列驱动程序说电子邮件通知已处理,但未发送电子邮件

问题描述

我正在尝试在触发事件时实现电子邮件通知。当一个事件被触发时,监听器将触发一个电子邮件通知。

当我没有实现队列时发送电子邮件。如果我实施了队列方法,则不会发送电子邮件。

当我运行队列工作者时,它给了我以下输出。但是没有发送电子邮件。

[2019-02-24 11:10:25] Processing: App\Notifications\CustomRequestListener
[2019-02-24 11:10:25] Processed:  App\Notifications\CustomRequestListener

我已经配置了监听器、事件、通知,如下所示。

听众

 class CustomRequestListener 
 {   
    public function handle(CustomRequestCreated $event)   
    {
      $user->notify(new CustomRequestEmail());   
    } 
 }

通知类

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;

class CustomRequestEmail extends Notification implements ShouldQueue
{
    use Queueable;

    public function via($notifiable)
    {
        return ['mail'];
    }

    public function toMail($notifiable)
    {
        return (new MailMessage)
            ->subject('Test Mail')
            ->view('layout.testmail.template');
    }
}

队列.php

'default' => env('QUEUE_DRIVER', 'database'),

'连接' => [

    'sync' => [
        'driver' => 'sync',
    ],

    'database' => [
        'driver' => 'database',
        'table' => 'jobs',
        'queue' => 'default',
        'retry_after' => 90,
    ],

.env

QUEUE_DRIVER=database

标签: laravelqueue

解决方案


上面代码中的问题是, shouldQueue 是在通知类上实现的,而它确实需要在侦听器上实现。

下面给出的代码解决了这个问题。

从通知中删除 ShouldQueue

class CustomRequestEmail extends Notification
{
    public function via($notifiable)
    {
        return ['mail'];
    }

    public function toMail($notifiable)
    {
        return (new MailMessage)
          ->subject('Test Mail')
          ->view('layout.testmail.template');
    }
}

将 shouldQueue 添加到监听器

class CustomRequestListener implements ShouldQueue
 {   
    public function handle(CustomRequestCreated $event)   
    {
      $user->notify(new CustomRequestEmail());   
    } 
 }

推荐阅读