Queue Emails In Laravel | Save Load Time
Hi, Robin this side with some new stuff today. Basically when I was sending emails from my applicationd with laravel at my first time. I was thinking...
Gurpreet Kait
Author
Hi, Robin this side with some new stuff today. Basically when I was sending emails from my applicationd with laravel at my first time. I was thinking...
Gurpreet Kait
Author
Hi, Robin this side with some new stuff today. Basically when I was sending emails from my applicationd with laravel at my first time. I was thinking that why it is taking so much time in sending mails. Because I didn't know that there is something interesting in Laravel that I haven't discovered. By using Queue you can do a lot stuff in laravel. But sending emails through queue system I think I love this part of laravel most.
Come to the topic, If you are getting problems when sending emails or if your mails is taking seconds to load while sending them. Then this article/blog for you. Because I have also faced the same problem.
I want you to know the flow before I'll start explaining you how you can queue your emails and schedule them.
Problem statement is: We have a client form by we will create clients and after that we have also created a mail. Which is welcome mail but we will send this mail to the created user/client in background using our queue system with scheduler.
It is totally depends on you how do you want to use it. Because I hope you get the point and now let's configure the database and create queue table to insert the queued mails. And along with this we also need to create Mail. So, First of all Let's start from the beginning.
I am assuming that you have installed the application and also have the form(your choice). I just want to show you how we can use it. Then you can move according to your logic.
//creates migration file for queue table
php artisan queue:table
//migrate queue table
php artisan migrate
Well, Now we have the queue table. I have inserted some data but you will have this table with empty records.
Well, after doing this you will have a variable called QUEUE_CONNECTION in your env file that you should change from sync to database.
//before
QUEUE_CONNECTION=sync
//after
QUEUE_CONNECTION=database
For testing purpose you can use Mailtrap which can help you to test Emails and stuff like this. I am personally using this service.
Now, I will create a email called WelcomMail which I want send when any new user will be created. So, We have to use the artisan command.
php artisan make:mail WelcomeMail --markdown
Now, we have created the mail and the view file as well. You can see that a folder with email/mail name have been created in your views directory. That you can use with your mail as a template.
//app/Mail/WelcomeMail.php
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
// class WelcomeMail extends Mailable
class WelcomeMail extends Mailable implements ShouldQueue
{
use Queueable, SerializesModels;
public $name;
public $email;
/**s
* Create a new message instance.
*
* @return void
*/
public function __construct($name, $email)
{
$this->name = $name;
$this->email = $email;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
$this->afterCommit();
return $this->markdown('email.welcome')->subject("Welcome to " . $this->name . "'s Financial Dashboard");
}
}
Send The Mail. In your markdown welcome mail template you can use any test text.
Well, As you can see in the above welcomemail file that some traits and an interface called ShouldQueue. So, This is something that we have to use while working with Queue. You also can send mails directly but it's the right way I think.
In simple explanation, a queue means some place where something waiting one after one in a line. So, it's a place where is flexible line where all the jobs will be processed. But all those jobs will work under queue.
I hope you understood what I have tried to say. If not then I'll recommend you to read this Queues on Official Doc.
Well, you can use mail function anywhere not just in controller what let me show how I am doing this. I'll initialize the Mail class in controller after saving the client data. It will look something like this (see in the code below).
// I am getting this data which we need in our WelcomeMail Class.
//
Mail::to($email)->send(new WelcomeMail($name, $email);
Now it has been added to the queue table.
//run this command to process the queued jobs locally
php artisan queue:listen
I'll recommend to use Mailtrap for testing because I use it. and check if you are getting the mail there.
As I have tested you can see above screenshot.
Basically, Scheduling the mail means that we have to schedule the command in kernal.php under Console directory.
Which will look like as I have described in the code below
protected function schedule(Schedule $schedule)
{
$schedule->command('queue:listen')->everyMinute();
}
//you can modify this according to you in terms of when you want it to be executed
// after every minute or hour totally up to you
When you are just testing that if you command is working or not then you can just run the artisan command.
php artisan schedule:work
//for local
On production we directly will be putting it to the cron job. And I think laravel provided a way more better option that the traditional cron job. Well, in cron job we just need to pass the artisan file path.
//this is my way to do the cron job you might have different option
/usr/bin/php74 -f /path to your project /artisan schedule:run >> /dev/null 2>&1
I hope you get the answer. And Queue is something that is more interesting part in laravel because it can handle a lot of stuff in background.
In the mail point of view I really like it.