Laravel 5.8 : 管理来自队列的失败邮件通知
Laravel 5.8 : manage failed mail notifications from a queue
我对队列中失败的邮件通知的管理有点困惑。
我创建了一个邮件通知 class,我用它向多个用户发送相同的通知。
该过程运行良好,但我正在尝试为失败的通知设置管理(例如向管理员用户发送邮件以提醒他们有关失败的通知)。
这是邮件通知 class :
class MyCustomMailNotification extends Notification implements
ShouldQueue {
use Queueable;
/**
* The number of times the job may be attempted.
*
* @var int
*/
public $tries = 3;
/**
* The number of seconds the job can run before timing out.
*
* @var int
*/
//public $timeout = 90;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->subject('My Subject')
->greeting('My greeting')
->line('My mail body')
->salutation('My salutations');
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
public function failed(Exception $e)
{
dd('Entered failed from MyCustomMailNotification : ' . $e));
}
}
我已经设置了一个侦听器“LogNotification”以获取通知事件的句柄,并带有生成失败的特定指令:
事件服务提供商:
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
'Illuminate\Notifications\Events\NotificationSent' => [
'App\Listeners\LogNotification',
],
];
听众:
namespace App\Listeners;
use Illuminate\Notifications\Events\NotificationSent; use
Illuminate\Queue\InteractsWithQueue; use
Illuminate\Contracts\Queue\ShouldQueue;
class LogNotification {
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param NotificationSent $event
* @return void
*/
public function handle(NotificationSent $event)
{
$result = 1/0;
}
}
邮寄是在这样的控制器中进行的:
$when = Carbon::now()->addSeconds(5);
foreach ($users as $user) {
$user->notify((new MyCustomMailNotification())->delay($when));
}
在失败的功能中,我没有得到失败通知的任何信息,我的问题是:
如何将失败与失败的通知相关联?
目标是能够获取未收到通知电子邮件的用户的信息。
感谢您提供任何帮助、想法或解决方案!
嗯,太简单了,我没看到...
由于失败的函数在通知中 class,我可以简单地与通知相关联:
$this->id
然后当然会从 model/table 获取有关通知的所有信息,例如用户 ID (notifiable_id) 和带有自定义信息的数据字段。
我对队列中失败的邮件通知的管理有点困惑。
我创建了一个邮件通知 class,我用它向多个用户发送相同的通知。 该过程运行良好,但我正在尝试为失败的通知设置管理(例如向管理员用户发送邮件以提醒他们有关失败的通知)。
这是邮件通知 class :
class MyCustomMailNotification extends Notification implements ShouldQueue { use Queueable; /** * The number of times the job may be attempted. * * @var int */ public $tries = 3; /** * The number of seconds the job can run before timing out. * * @var int */ //public $timeout = 90; /** * Create a new notification instance. * * @return void */ public function __construct() { // } /** * Get the notification's delivery channels. * * @param mixed $notifiable * @return array */ public function via($notifiable) { return ['mail']; } /** * Get the mail representation of the notification. * * @param mixed $notifiable * @return \Illuminate\Notifications\Messages\MailMessage */ public function toMail($notifiable) { return (new MailMessage) ->subject('My Subject') ->greeting('My greeting') ->line('My mail body') ->salutation('My salutations'); } /** * Get the array representation of the notification. * * @param mixed $notifiable * @return array */ public function toArray($notifiable) { return [ // ]; } public function failed(Exception $e) { dd('Entered failed from MyCustomMailNotification : ' . $e)); } }
我已经设置了一个侦听器“LogNotification”以获取通知事件的句柄,并带有生成失败的特定指令:
事件服务提供商:
/** * The event listener mappings for the application. * * @var array */ protected $listen = [ 'Illuminate\Notifications\Events\NotificationSent' => [ 'App\Listeners\LogNotification', ], ];
听众:
namespace App\Listeners; use Illuminate\Notifications\Events\NotificationSent; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Contracts\Queue\ShouldQueue; class LogNotification { /** * Create the event listener. * * @return void */ public function __construct() { // } /** * Handle the event. * * @param NotificationSent $event * @return void */ public function handle(NotificationSent $event) { $result = 1/0; } }
邮寄是在这样的控制器中进行的:
$when = Carbon::now()->addSeconds(5); foreach ($users as $user) { $user->notify((new MyCustomMailNotification())->delay($when)); }
在失败的功能中,我没有得到失败通知的任何信息,我的问题是: 如何将失败与失败的通知相关联?
目标是能够获取未收到通知电子邮件的用户的信息。
感谢您提供任何帮助、想法或解决方案!
嗯,太简单了,我没看到...
由于失败的函数在通知中 class,我可以简单地与通知相关联:
$this->id
然后当然会从 model/table 获取有关通知的所有信息,例如用户 ID (notifiable_id) 和带有自定义信息的数据字段。