Notifications bulk email Laravel 5.3

Posted 1 year ago by eddieace

Hi!

I have a problem sending bulk emails with notifications in Laravel 5.3

The problem is that it's a loop, but it does only send out one email, the first one. Any clues?

Here is the handle method

public function handle()
{
    $users = User::all();

    $users->each(function ($user) {

        $user->notify(new SendSomething($user));

    });
}

And here is the notification itself

namespace App\Notifications;

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

class SendSomething extends Notification
{
    use Queueable;

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

    }

    /**
     * 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)
    {

        $user = $this->user;

        return (new MailMessage)
            ->to($user->email)
            ->from('[email protected]', 'example')
            ->subject('Some subject')
            ->greeting('Hello ' . $user->name . '!')
            ->line('How are you?')
            ->action('Do something', url('/somewhere/'))
            ->line('Kind regards, <br> u bro');
    }

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

Please sign in or create an account to participate in this conversation.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.