How to send mail using queue in Laravel?

Raviya Technical
4 min readAug 27, 2021

In this tutorial, I will demonstrate how to send emails using queue with laravel 5.7. I will show how to use queue jobs in laravel from scratch. we will simple create email send using queue in this article.

Sometimes, you see some processes take time to load like email send, payment gateway, etc. When you send an email for verification or send an invoice then it loads time to send mail because it is services. If you don’t want to wait for the user to send an email or other process on loading server-side process then you can use a queue. because it’s very fast and visitors will happy to see loading time.

Here, I am going to share a very simple example to create a queue with a database driver for test email sending. You can definitely understand how to work queue and how it’s easy. If you haven’t used before then don’t worry, here is from starch and very simple. This is for startup developers on queue tasks. Just see the below step:

Content Overview

Step 1: Setup Laravel 5.7

Step 2: Create Mail Setup

Step 3: Configuration of Queue

Step 4: Create Queue Job

Step 5: Test Queue Job

Step 1: Setup Laravel

first of all, we need to get a fresh Laravel version application using bellow command, So open your terminal OR command prompt and run bellow command:

composer create-project --prefer-dist laravel/laravel blog

Step 2: Create Mail Setup

We are going from scratch and in the first step, we will create an email for testing using the Laravel Mail facade. So let’s simply run the below command.

php artisan make:mail SendEmailTest

Now you will have a new folder “Mail” in-app directory with SendEmailTest.php file. So let’s simply copy the below code and paste it on that file.

Location:- app/Mail/SendEmailTest.php

<?phpnamespace App\Mail;use Illuminate\Bus\Queueable;use Illuminate\Mail\Mailable;use Illuminate\Queue\SerializesModels;use Illuminate\Contracts\Queue\ShouldQueue;class SendEmailTest extends Mailable{use Queueable, SerializesModels;/*** Create a new message instance.** @return void*/public function __construct(){}/*** Build the message.** @return $this*/public function build(){return $this->view('emails.test');}}

Ok, now we require to create an email view using a blade file. So we will create a simple view file and copy bellow code om the following path.

Location:- resources/views/emails/test.blade.php

<!DOCTYPE html><html><head><title>How to send mail using queue in Laravel 5.7? - ItSolutionStuff.com</title></head><body><center><h2 style="padding: 23px;background: #b3deb8a1;border-bottom: 6px green solid;"><a href="https://itsolutionstuff.com">Visit Our Website : ItSolutionStuff.com</a></h2></center><p>Hi, Sir</p><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmodtempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodoconsequat. Duis aute irure dolor in reprehenderit in voluptate velit essecillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat nonproident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p><strong>Thank you Sir. :)</strong></body></html>

after configuration of the view file, we have to set up for email send, So let’ set configuration in the .env file:

Location:- .env

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=xyz@gmail.com
MAIL_PASSWORD=123456
MAIL_ENCRYPTION=tls

Step 3: Configuration of Queue

Now in the next step, we will make configurations on the queue driver so first of all, we will set the queue driver “database”. You can set it as you want also we will define the driver as is too. So here define database driver on the “.env” file:

Location:- .env

QUEUE_CONNECTION=database

After that, we need to generate migration and create tables for the queue. So let’s run bellow command for queue database tables:

Generate Migration:

php artisan queue:table

Run Migration:

php artisan migrate

Step 4: Create Queue Job

now we will create a queue job by following the command, this command will create a queue job file with Queueable. So let’s run bellow command:

php artisan make:job SendEmailJob

now you have the SendEmailJob.php file in the “Jobs” directory. So let’s see that file and put the bellow code on that file.

app/Jobs/SendEmailJob.php

<?phpnamespace App\Jobs;use Illuminate\Bus\Queueable;use Illuminate\Queue\SerializesModels;use Illuminate\Queue\InteractsWithQueue;use Illuminate\Contracts\Queue\ShouldQueue;use Illuminate\Foundation\Bus\Dispatchable;use App\Mail\SendEmailTest;use Mail;class SendEmailJob implements ShouldQueue{use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;protected $details;/*** Create a new job instance.** @return void*/public function __construct($details){$this->details = $details;}/*** Execute the job.** @return void*/public function handle(){$email = new SendEmailTest();Mail::to($this->details['email'])->send($email);}}

Step 5: Test Queue Job

Now time is use and test created queue job, so let’s simply create a route with the following code for the testing created queue.

Location:- routes/web.php

Route::get('email-test', function(){$details['email'] = 'your_email@gmail.com';dispatch(new App\Jobs\SendEmailJob($details));dd('done');});

The ok route is defined, you can watch your queue process using the laravel queue command, so let’s run bellow command:

php artisan queue:listen

Now run your project and bellow link:

http://localhost:8000/email-test

--

--