Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weekly email newsletter in laravel 4

I want to send auto generated weekly newsletter from my laravel project. From a controller I want to send some result of the laravel query in a newslater form to all the user in the list.

At this moment I can send the mail to single user when they are knocking some operation themselves . Now I want a auto generation of email on a particular frequent of time (day/ week /month ) ... also I want to send those mail to all the user in the db, in a loop. thank you for helping me in this tiny research :)

like image 663
Sagiruddin Mondal Avatar asked Jan 11 '23 21:01

Sagiruddin Mondal


1 Answers

Create an artisan command:

php artisan command:make SendNewletterCommand

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

Set your command name:

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 command in artisan by editing app/start/artisan.php and adding:

Artisan::add(new SendNewletterCommand);

And add your new command to your crontab:

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

It will send your e-mails every sunday at midnight.

like image 141
Antonio Carlos Ribeiro Avatar answered Jan 16 '23 21:01

Antonio Carlos Ribeiro