DEV Community

Cover image for Laravel 8 Send Mail using Queue Tutorial
Suresh Ramani
Suresh Ramani

Posted on • Originally published at techvblogs.com

Laravel 8 Send Mail using Queue Tutorial

What is Queue in Laravel?

Laravel 8 Queues allow you to delay a time-consuming task until a later time. By delaying the time-consuming task, you can improve the performance of the Laravel application significantly.

In this tutorial, we will see how we can send emails using queue in laravel 8. Sometimes sending emails to multiple users may take time. so to overcome this we can push emails in queue. A queue will send emails in the background without delaying the response time.

Sometimes, the Mail send process takes some time. And you don’t want to wait to send an email or another process on loading the server-side process. So, you can use the queue job for sending mail in laravel 8 app. So, This laravel 8 send email using queue example tutorial will create a simple and easy queue job with database driver for test email sending.

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

Step 1: Install Laravel Project

First, open Terminal and run the following command to create a fresh laravel project:

composer create-project --prefer-dist laravel/laravel laravel-mail-queue
Enter fullscreen mode Exit fullscreen mode

or, if you have installed the Laravel Installer as a global composer dependency:

laravel new laravel-mail-queue
Enter fullscreen mode Exit fullscreen mode

Step 2. Configuration SMTP & Database

You need to configure SMTP details in the .env file like the following:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=<YOUR DATABASE NAME>
DB_USERNAME=<YOUR DATABASE USERNAME>
DB_PASSWORD=<YOUR DATABASE PASSWORD>

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io 
MAIL_PORT=2525 
MAIL_USERNAME=<ADD YOUR SMTP USERNAME>
MAIL_PASSWORD=<ADD YOU SMTP PASSWORD>
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=<YOUR EMAIL ADDRESS>
Enter fullscreen mode Exit fullscreen mode

Step 3. Create Mail Setup

Run the following command:

php artisan make:mail TestMail
Enter fullscreen mode Exit fullscreen mode

Now you will have a new folder Mail in the app directory with the TestMail.php file. So let's copy the below code and paste into that file.

app/Mail/TestMail.php

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class TestMail 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.testMail');
    }
}
Enter fullscreen mode Exit fullscreen mode

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 into that blade file.

resources/views/emails/testMail.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Laravel 8 Send Mail using Queue Tutorial - TechvBlogs</title>
</head>
<body>
    <p>Hi</p>
    <p>This is test queue mail.</p>
    <strong>Thank you</strong>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Step 4. Queue Configuration

In this step, configuration on queue driver. So open the .env file and define the database queue driver into the .env file:

QUEUE_CONNECTION=database
Enter fullscreen mode Exit fullscreen mode

We need to generate migration and create tables for a queue. Run the following command:

php artisan queue:table
Enter fullscreen mode Exit fullscreen mode

Now, run the migration command in your terminal.

php artisan migrate
Enter fullscreen mode Exit fullscreen mode

Step 5. Create Queue Job

Now we will create a queue job by the following command. This command will create a queue job file with Queueable. So let's run the below command:

php artisan make:job TestEmailJob
Enter fullscreen mode Exit fullscreen mode

Now you have the TestEmailJob.php file in the "Jobs" directory. So let's see that file and put the below code into that file.
app/Jobs/TestEmailJob.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\TestMail;
use Mail;

class TestEmailJob 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 TestMail();
        Mail::to($this->details['email'])->send($email);
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 6. Create Route

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

routes/web.php

Route::get('send-email-queue', function(){
    $details['email'] = '<EMAIL ADDRESS>';
    dispatch(new App\Jobs\TestEmailJob($details));
    return response()->json(['message'=>'Mail Send Successfully!!']);
});
Enter fullscreen mode Exit fullscreen mode

Step 7. Test Queue Jobs

Now you can watch your queue process using the laravel queue command. Run the following command:

php artisan queue:listen
Enter fullscreen mode Exit fullscreen mode

You can also clear config cache using below command:

php artisan config:clear
Enter fullscreen mode Exit fullscreen mode

Now we can run Laravel, run the following command:

php artisan serve
Enter fullscreen mode Exit fullscreen mode

Now run your project and open below link:

http://localhost:8000/send-email-queue

Thank you for reading this blog.

Top comments (0)