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
How to Create Slider using jQuery
How to Create Slider using jQu...

In this post we will see how to create slider using jquery, here we will use owl carousel for create slider using b...

Read More

Aug-04-2021

How To Get Hourly Data In MySQL
How To Get Hourly Data In MySQ...

In this tutorial, we will see how to get hourly data in mysql. Many times we need to get hourly data or we are required...

Read More

Feb-04-2022

How to Search Records using Laravel 10 Livewire
How to Search Records using La...

Hey developers! Today, I'm excited to walk you through an incredibly powerful feature in Laravel 10: searching...

Read More

Dec-27-2023

How to Upload Image to Storage Folder in Laravel 10
How to Upload Image to Storage...

As I delve into Laravel 10, a true powerhouse in the realm of PHP frameworks, I've found it to be a game-changer for...

Read More

Sep-29-2023