Laravel 4 Weekly Email Newsletter

I want to send an automatically generated weekly newsletter from my laravel project. From the controller, I want to send some result of the laravel request in the newslater form to all the users in the list.

At this point, I can send mail to one user when they themselves knock down some operations. Now I want to automatically create an email for a certain time (day / week / month) ... also I want to send this email to all users in db in a loop. thanks for helping me in this tiny research :)

+4
source share
1 answer

Create the command artisan command :

php artisan command:make SendNewletterCommand

In the application / commands, edit SendNewletterCommand.php and:

Name your team:

protected $name = 'newsletter:send';

Create your fire () method:

public function fire()
{
   foreach(User::all() as $user)
   {
        Mail::send('emails.newletter', $data, function($message) use ($user)
        {
            $message->to($user->email, $user->name)->subject('Welcome!');
        });
   }
}

Register your team in the artisan by editing app / start / artisan.php and adding:

Artisan::add(new SendNewletterCommand);

And add a new command to your crontab:

0 0 * * sun php /your/project/path/artisan newsletter:send

He will send your emails every day at midnight.

+8
source

All Articles