How to Send E-mail Using Queue in Laravel 7/8

Websolutionstuff | Oct-30-2020 | Categories : Laravel PHP

Today I will show you how to send e-mail using queue in laravel 7/8, many times we can see some processes take more time to load like payment gateway, email sending, etc. Whenever you are sending an email for verification then it loads time to send mail because it is a service. If you don't want to wait for the user to send an email or other process on loading the server-side process then you can use a queue.

So let's see send mail using queue in laravel 7/8 and laravel 7/8 mail queue example.

Laravel 7/8 Send Email Using Queue

Step 1: Install Laravel 8

Install the laravel application using the composer command.

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

 

 

Step 2: Create Mail Setup

Run the below command in your terminal.

php artisan make:mail SendEmailDemo

Now you will find a new Mail folder in the app directory with the SendEmailDemo.php file. So copy the below code on this 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 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 7/8 ?</title>
</head>
<body>
   
<center>
<h2 style="padding: 23px;border: 6px red solid;">
	<a href="https://websolutionstuff.com">Visit Our Website : websolutionstuff.com</a>
</h2>
</center>
  
<p>Hi,</p>
<p>This is test mail. This mail send using queue listen in laravel 7/8.</p>  
<strong>Thanks & Regards.</strong>

</body>
</html>

Now, we will configure of view file, we have to set it up for email send, So let's set the configuration in the .env file.

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

 

 

Step 3: Configuration of Queue

Now, we are setup the 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.

QUEUE_CONNECTION=database

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

php artisan queue:table

Now, run the migration in your terminal.

php artisan migrate

 

 

Step 4: Create Queue Job

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

php artisan make:job SendEmailJob

As of now, you will find the SendEmailJob.php file in this path app/Jobs/SendEmailJob.php. So, copy the below code in that location.

<?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 to your web.php file

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

Now, clear the config cache using the below command for sending mail with queue laravel 7/8.

php artisan config:clear

Now, run this laravel 7/8 queue example with the artisan command.

php artisan serve

Finally, we are able to run this queue example. you will get results like the below images.

send_mail_using_queue_listen

send_mail_queue

 


You might also like:

Recommended Post
Featured Post
Laravel 9 CRUD With Image Upload Example
Laravel 9 CRUD With Image Uplo...

In this article, we will see the laravel 9 crud with an image upload example. Here, we will learn how to image upload wi...

Read More

Dec-09-2022

Special Characters Not Allowed Validation In Laravel 9
Special Characters Not Allowed...

In this article, we will see special characters not allowed validation in laravel 9. Here, we will learn special ch...

Read More

Dec-23-2022

How To Send Email In Laravel 9 Using Mailgun
How To Send Email In Laravel 9...

In this article, how to send email in laravel 9 using mailgun. we will learn laravel 9 to send emails using mailgun...

Read More

Jul-29-2022

Load More Data in Laravel Using Ajax jQuery
Load More Data in Laravel Usin...

In this article, we will see example of load more data in laravel using ajax jquery, In many websites, has huge content&...

Read More

Feb-26-2021