When we developed a web application, sometimes we get such requests that take too long to perform during a typical web request. In this scenario, Laravel allows you to easily create queued jobs that may be processed in the background without breaking the response. Laravel's queue configuration file is located in your config/queue.php path where we can configure the queue option from there. 

In this tutorial, I will show you Laravel 9 send email using queue. You will get step by step explain how to send mail using queue in laravel 9. I will help you to give an example of laravel 9 send mail using queue. I want to show you laravel 9 send mail in queue. You just need to follow some steps to learn laravel 9 send an email with a queue.

In this example tutorial, I will create one mail class and create a job class. We can send a test mail using a queue. just follow the below step and make it done in this example:

laravel-9-queue-tutorial

Step 1: Install Laravel 9

If you do not have the Laravel app, then you may go ahead and execute the below command:

composer create-project laravel/laravel example-app

 

Step 2: Make Configuration

In the first step, before sending mail using Laravel 9 queue, we need to configure mail like mail host, mail port, mail username, and mail password so Laravel 9 will use that sender configuration for sending an email. So you can simply add the following.

.env

MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=your_mail_address
MAIL_PASSWORD=your_mail_password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=mygoogle@gmail.com
MAIL_FROM_NAME="${APP_NAME}"

 

Read also: How To Send Mail In Laravel 9 Application

 

Step 3: Create Mail

Laravel provides an artisan command to create mail. So run make:mail command to create a mail class. Run the below command to create a mail class.

php artisan make:mail SendEmailTest

 

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

app/Mail/SendEmailTest.php

<?php
  
namespace App\Mail;
  
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
  
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 the below code on the following path.

resources/views/emails/test.blade.php

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

 

Read also: Laravel 9 CRUD Tutorial Example Using Eloquent ORM

 

Step 4: Configure Queue

Now in the next step, we will make a configuration 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 Redis too. So here define the database driver on the .env file:

.env

QUEUE_CONNECTION=database

 

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

php artisan queue:table

//then

php artisan migrate

 

Step 5: Create Job

There is also an artisan command to create a queue job. So run the below command and create:

php artisan make:job SendEmailJob

 

Now you have the SendEmailJob.php file in the Jobs directory. So let's see that file and put below code on that file.

app/Jobs/SendEmailJob.php

<?php
  
namespace App\Jobs;
  
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
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 6: Add route

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

routes/web.php

<?php

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

 

Next, you must have to run the following command to see the queue process, you must have to keep starting this command:

php artisan queue:work

 

Now run the command php artisan serve and visit the below URL and check it:

 

url
http://localhost:8000/email-test

 

Read also: Central Place Of Laravel | Service Provider Explanation

 

Hope this Laravel 9 queue email tutorial will help you.