Laravel 9 Send Email Using Queue Tutorial Example

Hello artisan,

In this tutorial, I will going to show you how to send email in Laravel 9 using queue. Assume a web application that has some tasks, such as parsing and storing an uploaded CSV file, that take too long to perform during a typical web request. Fortunately, Laravel provides you to easily create queued jobs that may be processed in the background. 

Don't know how to send email in Laravel 9 with queue or don't know how to set up queue in Laravel 9? Don't worry. I am here to show you a complete guide on how to use queue in Laravel and how to send email in Laravel 9.

I made a complete guide on how to upload a million records in Laravel using queue job batching. You can follow this example to know more about queue. In this example I will show you how to send email with Laravel 9 using queue.

This tutorial will guide you laravel 9 send email using queue. I will explain how to send mail using queue in laravel 9 step by step. I will help you to give an example of laravel 9 send mail using queue. I would like to give you an complete example of laravel 9 send mail in queue. We have to just need to do some steps to do laravel 9 send an email with a queue.

In this laravel 9 queue tutorial example, I will use a database driver. I will create one mail class and create a job class. you can send test mail using a queue. just follow the below step and make it done this example:

 

Step 1: Install Laravel 9

We need a fresh Laravel 9 application to complete this tutorial. So run the below command:

composer create-project laravel/laravel example-app

 

Step 2: Create Mail Class

I am going from scratch and in the first step, I will create an email for testing using the Laravel Mail facade. So let's simply run the bellow command.

php artisan make:mail SendEmailTest

 

Now in this step, update the file SendEmailTest.php file. So let's copy the bellow code and paste it on that file.

app/Mail/SendEmailTest.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');
    }
}

 

Now, we require to create an email blade view file using. So I will create a simple view file and copy the below code om the following path.

resources/views/emails/test.blade.php

 

Now configure your mail configuration like:

.env

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

 

Read also: Upload Large CSV File using Queue Job Batching in Laravel

 

Step 3: Queue Configuration

In this step, we need to configure the queue driver that we are going to use. Update like below:

.env

QUEUE_CONNECTION=database

 

Once the migration has been created, We may migrate your database using the migrate command:

php artisan queue:table
 
php artisan migrate

 

Step 4: Create Job

In this step, we will create a job for the processing queue in the background. So run the below command to create a queue job:

php artisan make:job SendEmailJob

 

In this step, we have the SendEmailJob.php file in the "Jobs" directory. So let's update that file and put the below code on that file.

app/Jobs/SendEmailJob.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 5: Add Route and Process Job

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

routes/web.php

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

 

Now run this command php artisan queue:work and start the development server and visit the below url and test:

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

 

Read also: Laravel 9 Notification | Send Notification in Laravel

 

Hope it can help you.

 

#laravel #laravel-9x #laravel-queue