How to Send Email using Queue in Laravel 11

In this article, we'll see how to send email using a queue in laravel 11. Here, we'll learn the concept of the queue in laravel 11 and send a mail with the help of a queue in laravel. Laravel allows you to easily create queued jobs that may be processed in the background.

Laravel queues provide a unified queueing API across a variety of different queue backends, such as Amazon SQSRedis, or even a relational database.

Laravel queue configuration options are stored in your application's config/queue.php configuration file.

Step by Step: Laravel 11 Send Email using Queue

how_to_send_email_using_queue_in_laravel_11

 

Step 1: Install Laravel 11 Application

 To install a Laravel 11 application, execute the following composer command:

composer create-project laravel/laravel laravel-11-example

 

Step 2: Queue Configuration

In the .env file, update the QUEUE_CONNECTION to the database.

.env

QUEUE_CONNECTION=database

Then, we'll create a migration table for the queue using the following command.

php artisan make:queue-table

Next, migrate the table into the database using the following command.

php artisan migrate

 

 

Step 3: Make Mail Configuration

Let's add mail configuration into the .env file including MAIL_HOST, MAIL_PORT, MAIL_USERNAME, and MAIL_PASSWORD. Here, we're using Gmail for sending mail, but you can also use Mailtrap for testing purposes.

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

 

Step 4: Create Mail Class

To create a mail class named TestMail for sending emails, run the following command:

php artisan make:mail TestMail

app/Mail/TestMail.php

<?php
  
namespace App\Mail;
  
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;
  
class TestMail extends Mailable
{
    use Queueable, SerializesModels;
  
    /**
     * Create a new message instance.
     */
    public function __construct(public $mailData)
    {
        //
    }
  
    /**
     * Get the message envelope.
     */
    public function envelope(): Envelope
    {
        return new Envelope(
            subject: 'Test Mail',
        );
    }
  
    /**
     * Get the message content definition.
     */
    public function content(): Content
    {
        return new Content(
            view: 'emails.testMail'
        );
    }
  
    /**
     * Get the attachments for the message.
     *
     * @return array
     */
    public function attachments(): array
    {
        return [];
    }
}

 

Step 5: Create Controller

In this step, we'll create a TestController with an index() method. Inside this method, we'll write code to send an email using a queue to a specified email address.

php artisan make:controller TestController

app/Http/Controllers/TestController.php

<?PHP
    
namespace App\Http\Controllers;
    
use Illuminate\Http\Request;
use Mail;
use App\Mail\TestMail;
    
class TestController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $mailData = [
            'title' => 'Mail from Techsolutionstuff',
            'body' => 'This is testing email using Queue.'
        ];
           
        Mail::to('test@gmail.com')->queue(new TestMail($mailData));
             
        dd("Email is sent successfully.");
    }
}

 

 

Step 6: Create Routes

Next, let's define the routes for sending emails in the web.php file.

routes/web.php

<?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\TestController;
    
Route::get('send-mail', [TestController::class, 'index']);
 
Step 7: Create Blade View

Next, create a file named testMail.blade.php and add HTML content for sending emails.

resources/views/emails/testMail.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>How to Send Email using Queue in Laravel 11 - Techsolutionstuff</title>
</head>
<body>
    <h1>{{ $mailData['title'] }}</h1>
    <p>{{ $mailData['body'] }}</p>
  
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p>
     
    <p>Thank you</p>
</body>
</html>
 
Step 8: Run the Laravel Application

To run the Laravel 11 application, execute the following command:

php artisan serve

To run the queue process, execute the following command:

php artisan queue:work

 


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