Laravel 12 Send Email Using Queue: Step-by-Step Guide

In this Laravel tutorial, you will learn in laravel 12 send email using queue: step-by-step guide with example.

Sending emails is a common requirement in almost every web application. Whether it’s user registration confirmation, password reset, order notification, or system alerts, emails play a crucial role in communication. However, sending emails synchronously can slow down your Laravel application and negatively impact user experience.

In this step-by-step guide, you’ll learn how to send emails using queues in Laravel 12, why queues are important, and how to implement them correctly for better performance and scalability.

Why Use Queue for Sending Emails?

When emails are sent without queues, Laravel waits for the mail to be delivered before responding to the user. This can cause noticeable delays, especially when email servers are slow.

By using queues:

  • Emails are sent in the background
  • Users get faster responses
  • Application performance improves
  • Server load is reduced
  • The system becomes more scalable

In short, queued emails make your Laravel application faster, smoother, and more professional.

Step 1: Install Laravel 12

If you’ve already installed Laravel, you can skip this step. To create a new Laravel project, run the following command:

composer create-project laravel/laravel send-mail-using-queue

Step 2: Configure Mail in Laravel 12

Open your .env file and configure mail settings. Example using SMTP:

MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=your_username
MAIL_PASSWORD=your_password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=admin@example.com
MAIL_FROM_NAME="Laravel App"

You can use Mailtrap for testing emails locally.

Step 3: Set Queue Driver

Run the following commands to generate the queue table. If the queue table already exists, you can safely skip this step:

php artisan queue:table
php artisan migrate

These commands will create the jobs table, which is required to store and manage queued tasks in your application.

Laravel supports multiple queue drivers such as database, redis, and sync. For beginners, the database queue is the easiest.

Update .env file:

QUEUE_CONNECTION=database

Read Also : How to Send Mail in Laravel 12 Using Gmail SMTP Step-by-Step Tutorial

Step 4: Create a Mailable Class & View

Generate a mailable class along with its corresponding Markdown template by running the following command:

php artisan make:mail ExampleMail --markdown=mail.example_mail

This command will create a new mailable class and a Markdown view, making it easier to design and manage email content in Laravel.

Update Markdown Template

Now this command will generate a mailable class and a markdown template. Let’s open markdown to update the template: resources\views\mail\example_mail.blade.php

<x-mail::message>
{{$data['title']}}

{{$data['message']}}

Thanks,<br>
{{ config('app.name') }}
</x-mail::message>

Update The Mailable

Open UserWelcomeMail.php and update it with the following logic:

<?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 ExampleMail extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     */
    public function __construct(public $data)
    {}

    /**
     * Get the message envelope.
     */
    public function envelope(): Envelope
    {
        return new Envelope(
            subject: 'Example Mail',
        );
    }

    /**
     * Get the message content definition.
     */
    public function content(): Content
    {
        return new Content(
            markdown: 'mail.example_mail',
        );
    }

    /**
     * Get the attachments for the message.
     *
     * @return array<int, \Illuminate\Mail\Mailables\Attachment>
     */
    public function attachments(): array
    {
        return [];
    }

}

Step 5: Create Controller

In this step, we will create a controller and then add the logic required to send an email using a queue.

Run the following command to generate the controller:

php artisan make:controller SendMailController

Open the app/Http/Controllers/SendMailController.php and update it with the following code:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Mail\ExampleMail;
use Mail;

class SendMailController extends Controller
{
    public function sendMail(){
        $data = [
            "title" => "Welcome to ITStuffSolutions",
            "message"=>'This is an example of send mail using queue'
        ];
        Mail::to('itstuffsolutions@gmail.com')->queue(new ExampleMail($data));
       
        dd("Mail Send Successfully");   
    }
}

This controller method prepares email data and sends the email using Laravel Queue instead of sending it instantly.

Mail::to()->queue() pushes the email job to the queue, so it is processed in the background. queues make your application faster, smoother, and production-ready.

Step 6: Create Routes

Now create a route to test the email sending functionality.

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\SendMailController;

Route::get('send-mail', [SendMailController::class, 'sendMail']);

Read Also : How to Import Large Excel and CSV Files in Laravel 12 Complete Guide

Step 7: Test The App

All the previous steps have been completed successfully. Now, start the Laravel development server by running the following command:

php artisan serve

Next, run the following command to start the queue worker. This step is essential when using queues in Laravel:

php artisan queue:work

Open your browser and navigate to the following URL: http://127.0.0.1:8000/send-mail

You should see an output similar to the one shown below:

Laravel 12 Send Email Using Queue: Step-by-Step Guide
Queue Worker Preview

Conclusion

Sending emails using queues in Laravel 12 is essential for building fast and scalable applications. With queues, your users don’t have to wait for email processing, and your application remains responsive even under heavy load.

By following this step-by-step guide, you can easily implement queued emails and improve both performance and user experience.