Laravel 8 Send Mail Using Queue

In this article, we will see laravel 8 send mail using the queue. Sometimes we can see some processes take more time to load like email send, upload CSV file, cron job, etc. Laravel allows you to easily create queued jobs that may be processed in the background. Laravel's queue configuration options are stored in your application's config/queue.php configuration file.

We will learn how to send mail using queue in laravel 8. You can also learn how to send mail in laravel 8 using queue with mailtrap.

So, let's see, send mail using queue in laravel 8.

Step 1: Install Laravel 8

In this step, we will install the laravel application using the below composer command.

composer create-project laravel/laravel example-app

 

 

Step 2: Create Mailable Class

Create mail file SendEmailDemo using the below command.

php artisan make:mail SendEmailDemo

You can find the SendEmailDemo.php file in the app/Mail/ folder. And this file should look like this.

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 SendEmailDemo 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->subject('Test Mail using Queue in Larvel 8')            
            ->view('email.demo');
    }
}

After that, we need to create an email view using a blade file. So, we will create demo.blade.php following the path.

 

 

resources/views/email/demo.blade.php

<!DOCTYPE html>
<html>
<head>
	<title>How To Send Mail Using Queue In Laravel 8 - Techsolutionstuff</title>
</head>
<body>
   
<center>
<h2 style="padding: 23px;border: 6px red solid;">
	<a href="https://websolutionstuff.com">How To Send Mail Using Queue In Laravel 8 - Techsolutionstuff</a>
</h2>
</center>
  
<p>Hi,</p>
<p>This is test mail. This mail send using queue listen in laravel 8.</p>  
<strong>Thanks & Regards.</strong>

</body>
</html>

Now, we will configure the .env file.

MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=xxxx
MAIL_PASSWORD=xxxx
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"

 

Step 3: Configuration of Queue

Now, we are set up the configuration of the queue driver. So, 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.

QUEUE_CONNECTION=database

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

php artisan queue:table

Now, migrate the table using the below command.

php artisan migrate

 

 

Step 4: Create Queue Job

In this step, we will create a new queue job. So, run the below command in your terminal.

php artisan make:job SendEmailJob

As of now, you can find the app/Jobs/SendEmailJob.php file.

<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Mail\SendEmailDemo;
use Mail;

class SendEmailJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    protected $send_mail;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($send_mail)
    {
        $this->send_mail = $send_mail;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $email = new SendEmailDemo();        
        Mail::to($this->send_mail)->send($email);
    }
}

 

Step 5: Test Queue Job

Now, we will test our queue job. add the below code in the web.php file.

Route::get('test/email', function(){
  
	$send_mail = 'test@gmail.com';
  
    dispatch(new App\Jobs\SendEmailJob($send_mail));
  
    dd('send mail successfully !!');
});

 

 

Now, clear the config cache using the below command.

php artisan config:clear

Now, run the laravel application using the below command.

php artisan serve

 


You might also like:

techsolutionstuff

Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of techsolutionstuff.com. Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.

RECOMMENDED POSTS

FEATURE POSTS