简体   繁体   中英

Email are not being send when queue is processed laravel

I am trying to send notification email + database notification to multiple users. I am experiencing this strange behavior with laravel queue, it sends database notification perfectly but do not send email. And If I remove the queue it works just fine, both the database notification as well as email are being received.

Note: Queue gets processed successfully.

Laravel 7

QUEUE_CONNECTION=database

Notificaion:

<?php

namespace App\Notifications;

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

class CommentOnTask extends Notification implements ShouldQueue
{
    use Queueable;

    private $data;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct($data)
    {
        $this->data = $data;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['database', 'mail'];
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        return (new MailMessage)
                    ->line('The introduction to the notification.')
                    ->action('Notification Action', url('/'))
                    ->line('Thank you for using our application!');
    }

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

    /**
     * Get the database representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toDatabase($notifiable)
    {
        return [
            'body'       => $this->data['body'],
        ];
    }
}

Controller:

<?php
.
.
.
        $members = $task->users;
        $members = $members->merge($task->followers);

        Notification::send($members, new CommentOnTask($data));

I know this is an old question but I just got notification of this so I thought I should let fellow dev know the issue and solution.

The problem was with the SMTP server that we were using at that time, hostgator's cpanel emails' SMTP does not work with queue in order words background process, they only work if emails on runtime so we had to change our SMTP and everything started to work properly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM