Mobile ApplicationTechnologyWeb Application

How to Send Email Using Queue in Laravel?

Overview of Laravel Queue

When we are sending multiple emails in Laravel at one time, it takes a lot of time. This time-consuming process disturbs the user experience. For this problem, we have one solution in Laravel, which is called “Queue”. Laravel Queue places the task in sequential order and performs them one by one.

In case of sending multiple emails at one time Queue store the tasks in the queue table and execute them one by one after some time (whenever we want). The Queue config file is stored in config/queue.php, where you can find different connection configurations related to the Laravel queue.

Here are the Steps to Send an Email with Laravel Queue

1) For Sending an Email We Need to do the Below Configuration in our .env file

MAIL_DRIVER     = smtp 
MAIL_HOST       = smtp.gmail.com 
MAIL_PORT       = 2525 
MAIL USERNAME   = [ Username ] 
MAIL_PASSWORD   = [ Password ] 
MAIL_ENCRYPTION = tls 
MAIL_FROM       = [ Demo@gmail.com ]

2) Create a Controller

PHP artisan make:controller EmailController

In this controller create a method which is having an email address in the request.

Public function sendEmail(Request $request)

{

/* This method will call SendEmailJob Job*/

dispatch(new SendEmailJob($request));

}

3) Create Job Classes for Laravel Queue

For using queue we need to create Jobs, which defines the task to be done in the queue after a later time.

By default, all Job classes are created in App/Jobs folder. For creating job classes run the below command in the terminal.

PHP artisan make:job SendEmailJob

This command creates a file in App/Jobs folder, which looks like this:

<?php

namespace 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().
    {
    
    }
}

4) Set a Queue Driver

Drivers are stored in pending queues temporarily. We will use the database driver as of now. To create a driver we need to run the below command:

PHP artisan queue:table

PHP artisan migrate

We need to define our driver type in a .env file like this:

QUEUE_DRIVER = database

5) Define the Queue’s Task in Job Class

In SendEmailJob class we need to define our task in the handle() method like:

public function handle()

{

    $data = $this->details;
    
    Mail::send([‘html’=>’demo_email_template’], $data, function($message) use ($data)
    
    {
    
    $message->to(‘receiver@gmail.com’, ‘John’)
    
    ->subject(‘This is test Queue.’);
    
    $message->from(‘info@demo.com’,’LaravelQueue’);
    
    });

}

6) In the End, You Need to Listen to the Laravel Queue to Do the Queue Jobs

PHP artisan queue:listen

If you want to make this process automated then create a Scheduler and run this command at a particular time. So you don’t need to type this command every time.

See also  Build your own Cocoapods with help of OneClick IT Consultancy

You can see your pending jobs in the jobs table also.

Connect with our experts to hire Laravel developer to build high quality web & mobile application for your business.

Wrapping Up

Laravel Queue is a breeze to implement into your current or new project. In this tutorial, we’ve tried to make it easy by providing an example. I hope this will be helpful. Additionally, there are numerous methods and parameters available to satisfy the Laravel Queue-related requirements.

We are a mobile application, web, and software development company. Please feel free to contact us for any of your software queries or you can hire Laravel developers for any queries.

    Need an IT Experts? Get a Free Consultation!

    lets start your project

    Related Articles