【问题标题】:Laravel - Notification Mail ShouldQueue does't workLaravel - 通知邮件应该队列不起作用
【发布时间】:2020-06-10 22:24:07
【问题描述】:

我想使用队列发送电子邮件通知。

我已经创建了队列表并跟踪了与该主题相关的所有文档,但通知是在不经过队列的情况下发送的。

在我的控制器中:

Notification::send(User::role('team')->get(), new NewExchangeToCollaboratorNotification($user, $exchange, $firstMessage));

我的通知代码是:

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Setting;

class NewExchangeToCollaboratorNotification extends Notification implements ShouldQueue
{
    use Queueable;

    protected $user; protected $exchange; protected $exchangeMessage; protected $replyToAddress;

    public function __construct($user, $exchange, $exchangeMessage)
    {
        $this->user = $user;
        $this->exchange = $exchange;
        $this->exchangeMessage = $exchangeMessage;
        $this->replyToAddress = Setting::get('MAIL_REPLY_TO_ADDRESS', env('MAIL_FROM_ADDRESS'));
    }

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

    public function toMail($notifiable)
    {
        return (new MailMessage)->view(
            'emails.exchanges.new',
            [
                'user' => $this->user,
                'exchangeMessage' => $this->exchangeMessage,
                'exchange' => $this->exchange
            ]
        )   ->subject('New exchange: ' .  $this->exchange->title)
            ->from(env('MAIL_FROM_ADDRESS'))
            ->replyTo($this->replyToAddress);
    }
}

通知是否可排队?是不是我做错了什么? 谢谢你的回答:)

编辑: 添加延迟也不起作用。

$when = now()->addMinutes(10);
Notification::send(User::role('team')->get(), (new NewExchangeToCollaboratorNotification($user, $exchange, $firstMessage))->delay($when));

编辑 2: 没有失败的工作

【问题讨论】:

  • 您是否尝试过运行队列工作者? php工匠队列:工作
  • 是的@Adnan,但并不总是有效。我也尝试在发送时添加延迟,但这不起作用:$when = now()->addMinutes(10); Notification::send(User::role('team')->get(), (newNewExchangeToCollaboratorNotification($user, $exchange, $firstMessage))->delay($w​​hen));
  • 您是否通过php artisan queue:failed检查了失败的作业?
  • 你的 .env 是 QUEUE_CONNECTION=database,对吧?
  • 我很高兴。我也添加了一个答案。 =)

标签: php laravel queue laravel-7


【解决方案1】:

确保您的 .env 是: QUEUE_CONNECTION=database

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-04
    • 1970-01-01
    • 1970-01-01
    • 2016-12-08
    • 1970-01-01
    • 2018-11-30
    • 1970-01-01
    • 2017-05-31
    相关资源
    最近更新 更多