Send Email using Queue in Laravel 12

In this tutorial, I’ll show you how to send emails using queues in Laravel 12. Queuing emails helps your app respond faster by handling email sending in the background. It’s easy to set up and super useful for better performance.

I’ll walk you through each step with simple code examples.

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

Send Email using Queue in Laravel 12

 

Step 1: Configure Mail in env

Make sure your .env file has the correct mail settings:

MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=your_mailtrap_username
MAIL_PASSWORD=your_mailtrap_password
MAIL_ENCRYPTION=null
[email protected]
MAIL_FROM_NAME="YourAppName"

 

Step 2: Set Up Queue Driver

In env, set your queue driver. For testing, we’ll use database.

QUEUE_CONNECTION=database

Then run the following commands:

php artisan queue:table
php artisan migrate

 

Step 3: Create Mailable Class

Run this command:

php artisan make:mail QueueEmail

 

Step 4: Edit the Mailable Class

Open app/Mail/QueueEmail.php and update it like this:

<?php

namespace App\Mail;

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

class QueueEmail extends Mailable implements ShouldQueue
{
    use Queueable, SerializesModels;

    public $data;

    public function __construct($data)
    {
        $this->data = $data;
    }

    public function envelope(): Envelope
    {
        return new Envelope(
            subject: 'Test Email via Queue',
        );
    }
  
    /**
     * Get the message content definition.
     */
    public function content(): Content
    {
        return new Content(
            view: 'emails.queue_email'
        );
    }
  
    /**
     * Get the attachments for the message.
     *
     * @return array
     */
    public function attachments(): array
    {
        return [];
    }
}

 

Step 5: Create the Email Blade View

Create this file: resources/views/emails/queue_email.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Email</title>
</head>
<body>
    <h2>Hello, {{ $data['name'] }}</h2>
    <p>This is a test email sent using queue in Laravel 12.</p>
    <p>Thank you,<br>{{ config('app.name') }}</p>
</body>
</html>

 

Step 6: Send the Email Using Queue

Use this in a controller or route to trigger the email:

use Illuminate\Support\Facades\Mail;
use App\Http\Controllers\MailController;

Route::get('send-queued-email', [MailController::class, 'index']);

app/Http/Controllers/MailController.php

<?PHP
    
namespace App\Http\Controllers;
    
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use App\Mail\QueueEmail;
    
class MailController extends Controller
{
    public function index()
    {
        $data = [
          'name' => 'John Doe',
        ];

        Mail::to('[email protected]')->queue(new QueueEmail($data));

        return 'Queued email has been sent!';
    }
}

 

Step 7: Run the Queue Worker

To process the queued email, run this in your terminal:

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