Laravel更改激活电子邮件和队列

时间:2016-11-24 02:42:49

标签: laravel email notifications queue

我已经覆盖了激活通知,但发送电子邮件时遇到了一些问题。它在不使用队列时工作,否则它会保留在数据库队列中,我不知道如何修复它。 但是我知道队列正在使用数据库队列发送电子邮件。

所以这不起作用:

<?php

namespace App\Notifications;

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

class SendActivationEmail extends Notification implements ShouldQueue
{
    use Queueable;

    protected $token;

    /**
     * Create a new notification instance.
     *
     * SendActivationEmail constructor.
     * @param $token
     */
    public function __construct($token)
    {
        $this->token = $token;
        $this->onQueue('social');
    }

    /**
     * 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('Activation email')
            ->greeting('xxx - Hello!')
            ->line('You need to activate your email before you can start using all of our services.')
            ->action('Activate Email', route('authenticated.activate', ['token' => $this->token]))
            ->line('Thank you for using our application!');
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            //
        ];
    }
}

但是这项工作:

<?php

namespace App\Notifications;

use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class SendActivationEmail extends Notification
{

    protected $token;

    /**
     * Create a new notification instance.
     *
     * SendActivationEmail constructor.
     * @param $token
     */
    public function __construct($token)
    {
        $this->token = $token;
    }

    /**
     * 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('Activation email')
            ->greeting('xxx - Hello!')
            ->line('You need to activate your email before you can start using all of our services.')
            ->action('Activate Email', route('authenticated.activate', ['token' => $this->token]))
            ->line('Thank you for using our application!');
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            //
        ];
    }
}

这样的事情也有效:

Mail::to($user->email)
            ->queue(new Welcome($user));

并在我的数据库队列中:(尝试次数不断增加)

{"job":"Illuminate\\Queue\\CallQueuedHandler@call","data":{"commandName":"Illuminate\\Notifications\\SendQueuedNotifications","command":"O:48:\"Illuminate\\Notifications\\SendQueuedNotifications\":6:{s:14:\"\u0000*\u0000notifiables\";O:45:\"Illuminate\\Contracts\\Database\\ModelIdentifier\":2:{s:5:\"class\";s:15:\"App\\Models\\User\";s:2:\"id\";a:1:{i:0;i:12;}}s:15:\"\u0000*\u0000notification\";O:43:\"App\\Notifications\\SendActivationEmail\":5:{s:8:\"\u0000*\u0000token\";s:64:\"f594ec1d2c15bf5c51903b1b408100b6de449895a042ddbe701d014b46a2bd8c\";s:2:\"id\";s:36:\"20d8a21a-3a69-47cf-a37d-bc98f2a83600\";s:10:\"connection\";N;s:5:\"queue\";s:7:\"default\";s:5:\"delay\";N;}s:11:\"\u0000*\u0000channels\";a:1:{i:0;s:4:\"mail\";}s:10:\"connection\";N;s:5:\"queue\";s:7:\"default\";s:5:\"delay\";N;}"}}

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我忘了创建表格通知,并且不知道它是必需的。

只需要执行这两个命令行:

php artisan notifications:table
php artisan migrate