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 8 Form Validation Example
Laravel 8 Form Validation Exam...

In this article, we will see the laravel 8 form validation example. form validation in laravel is a very common fun...

Read More

Oct-10-2020

How to Create Multi Language Website in Laravel
How to Create Multi Language W...

In this article, we will see how to create a multi-language website in laravel. In this example, you can understand...

Read More

Nov-09-2020

Crop Image Before Upload Using Croppie Plugin
Crop Image Before Upload Using...

In this article, we will see how to crop images before uploading using the croppie plugin. any times we have requir...

Read More

Aug-15-2020

How To Get Current Month Records In MySQL
How To Get Current Month Recor...

In this tutorial, we will see how to get current month records in MySQL. For data analysis and reporting, we need more d...

Read More

Feb-08-2022