Mail: Laravel 11 Send Email using Queue

Websolutionstuff | Apr-12-2024 | Categories : Laravel PHP

In this guide, we'll see how to send email using a queue in laravel 11. Here we'll see the concept of queue with mail in the laravel 11. Laravel provides a clean, simple email by Symfony Mailer. Laravel and Symfony Mailer provide drivers for sending email via SMTP, Mailgun, Postmark, and Amazon SES.

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. In this file, you will find connection configurations for each of the queue drivers

Table of Contents:

Step 1: Install Laravel 11 Application

Step 2: Queue Configuration

Step 3: Make Mail Configuration

Step 4: Create Mail Class

Step 5: Create Controller

Step 6: Create Routes

Step 7: Create Blade View

Step 8: Run the Laravel 11 Application

 

Step 1: Install Laravel 11 Application

In this step, we'll install a Laravel 11 application, and execute the following composer command:

composer create-project laravel/laravel laravel-11-send-queue-email

 

Step 2: Queue Configuration

Now, open the .env file, and 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

Then configure mail into the .env file including MAIL_HOST, MAIL_PORT, MAIL_USERNAME, and MAIL_PASSWORD. Here, we're using Mailtrap for sending mail for testing purposes.

MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=your_username
MAIL_PASSWORD=your_password
MAIL_ENCRYPTION=tls
[email protected]
MAIL_FROM_NAME="${APP_NAME}"

 

Step 4: Create Mail Class

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

php artisan make:mail sendTestMail

app/Mail/sendTestMail.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 sendTestMail 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: 'Testing Email using Queue',
        );
    }
  
    /**
     * Get the message content definition.
     */
    public function content(): Content
    {
        return new Content(
            view: 'emails.sendTestMail'
        );
    }
  
    /**
     * Get the attachments for the message.
     *
     * @return array
     */
    public function attachments(): array
    {
        return [];
    }
}

 

Step 5: Create Controller

In this step, we'll create a TestEmailController 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 TestEmailController

app/Http/Controllers/TestEmailController.php

<?PHP
    
namespace App\Http\Controllers;
    
use Illuminate\Http\Request;
use Mail;
use App\Mail\sendTestMail;
    
class TestEmailController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $mailData = [
            'title' => 'Mail from Websolutionstuff',
            'body' => 'This is testing email using Queue.'
        ];
           
        Mail::to('[email protected]')->queue(new sendTestMail($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\TestEmailController;
    
Route::get('send-test-mail', [TestEmailController::class, 'index']);
 
Step 7: Create Blade View

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

resources/views/emails/sendTestMail.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Mail: Laravel 11 Send Email using Queue - Websolutionstuff</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:

Recommended Post
Featured Post
Laravel 9 Import Export CSV/EXCEL File Example
Laravel 9 Import Export CSV/EX...

In this tutorial, I will give you laravel 9 import export csv and excel file example. We will simply create import...

Read More

Feb-19-2022

Integrating ChatGPT with Node and Vue
Integrating ChatGPT with Node...

In a world where technology and human interaction blend seamlessly, artificial intelligence (AI) holds incredible potent...

Read More

Jul-17-2023

How to Create Form Request Validation in Laravel 10
How to Create Form Request Val...

Hey there! Today, I want to talk to you about a super useful feature in Laravel 10 called form request validation. If yo...

Read More

Feb-23-2024

Carbon diffForHumans Laravel Example
Carbon diffForHumans Laravel E...

In this article, we will see carbon diffForHumans in laravel. Carbon diffForHumans function in carbon provides the...

Read More

Dec-14-2020