laravel - 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
解决方案
上面代码中的问题是, 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());
}
}
推荐阅读
- azure-devops - 从 Azure DevOps (VSTS) 中的测试计划运行 SpecFlow 方案大纲
- css - 在子组件中覆盖父组件样式
- xml - 请参考问题“Escape backslash in XML and split as separate xml node using XSLT”
- mongodb - Strapi GraphQL Where Null - 转换为 ObjectId 的值失败
- php - 非活动用户的 Laravel 单元测试登录
- c - libsodium crypto_box_seal_open 失败
- vue.js - 在 vuejs 组件的非命名插槽中从父级访问方法或属性
- random - 与随机字符串相比,UUID 或 GUID 的用途是什么?
- sql - sql server 中的内联用户定义函数
- python - 将值插入行类型的 Pyspark 中的 Hive 表