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 :)
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.
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.