Friday, June 24, 2016

Task Scheduling in Laravel | How to send Email using Cron job? | Cron Job in Laravel.


cron job in laravel



There are some times when you want to run some administrative tasks in your application periodically. Such as, you want to send SMS to users or you want to clean up the database at some specific time. So Here we need some task scheduling mechanism to do these periodic tasks.
Laravel command scheduler will help you to this job with a single command and the easiest way.

So Let's Begin the session.

1. Let's make a command with artisan. Open cmd in your project repository and run :
php artisan make:console SendEmails --command=emails:send
As you have remembered we gonna send bulk email to list of users. So by this command a file called SendEmails.php has  generated which is in App\Console\Commands in your project repository.

2. Go through the file SendEmails.php carefully. There is $signature,  $description, function __construct() and handle() Method. We have to work on this handle method means our controller logic for sending  E-mail will be written here. You can change/rewrite the $description if you want.

3.  Now add this command in kernel.php inside protected $commands :
Commands\SendEmails::class,
4.  Now write your logic in handle() method in SendEmails.php. As I'm gonna sending bulk email to all the user. I'm gonna use my logic, you can use your own.


 public function handle(Request $request)
    {
        $user=User::all();
            Mail::queue('send', ['user' => $user], function($m) use ($user)
                {
                    foreach ($user as $user) {
                        $m->to($user->email)->subject('Email Confirmation');
                    }                     
                });
    }

Here I'm sending those Email using Queue so you should configured with Queue Setting in your project.

Go through the link if you want to know how you can use Queue in your project.


5. Now if we run the command which we have created earlier, it will send automatically email to all the Users.  Here is the command:
php artisan emails:send
6. Ok, Till Now  you have learned how you can create your own command and how you can do some particular task with the use of that command. Now let's come to the Cron Job.

Let's say we want to send the same email daily to all the users, so what to do?  It's very easy, Just add below line in protected function schedule()  in kernel.php

$schedule->command('SendEmails')
                 ->daily();

7. That's it. Now you don't need to run the command also. It will automatically send Email to all the users daily at Midnight with this Cron Job.

So That's it for today. Hope you like the session. Stay connected with my blog as new and exciting topics will be covered soon. And if you find any difficulty of today's session, don't hesitate to mention it in comment.

Thank you.


Related Articles

3 comments:

  1. I have try this whole process step by step by still schedule function is not working. plzz tell me solution of this.

    ReplyDelete
  2. Your Blog is amazing. If you want to know that HOW TO SETUP A CRON JOB then,
    Click here for more information:
    HOW TO SETUP A CRON JOB

    ReplyDelete
  3. php artisan make:console SendEmails --command=emails:send
    should be
    php artisan make:command SendEmails --command=emails:send

    ReplyDelete