Notifications bulk email Laravel 5.3

Posted 2 years 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.

Reply to

Use Markdown with GitHub-flavored code blocks.