In this tutorial, we will learn Laravel 7/6 Queue Tutorial | Laravel 7/6 Send Mail with Queue Example

In this section, you will know how to create a queue with a database driver for test email sending. 

You will understand how to work with the queue and will know how much easy it is. 


Step 1: 

Setup Laravel 6 

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

Step 2: 

Create Mail Setup

 

php artisan make:mail SendEmailTest

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');
    }
}

resources/views/emails/test.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>How to send mail using queue in Laravel 6? - rathorji.in</title>
</head>
<body>
   
<center>
<h2 style="padding: 23px;background: #b3deb8a1;border-bottom: 6px green solid;">
    <a href="https://rathorji.in">Visit Our Website : rathorji.in</a>
</h2>
</center>
  
<p>Hi, Sir</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>

.env

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=xyz@gmail.com
MAIL_PASSWORD=123456
MAIL_ENCRYPTION=tls


Step 3: 

Configuration of Queue 

.env

QUEUE_CONNECTION=database

Generate Migration:

php artisan queue:table

Run Migration:

php artisan migrate


Step 4: 

Create Queue Job 

php artisan make:job SendEmailJob


app/Jobs/SendEmailJob.php

<?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\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: 

Test Queue Job routes/web.php

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

let's run bellow command:

php artisan queue:listen

 clear config cache using bellow command:

php artisan config:clear

run project using bellow command:

php artisan serve

run your project and bellow link:

http://localhost:8000/email-test

May this example help you.