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 SQS, Redis, 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
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
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
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}"
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 [];
}
}
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.");
}
}
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']);
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>
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:
In this article, we will explore the integration of OpenAI into Laravel versions 8, 9, and 10. Our focus will be on unde...
Feb-06-2023
In this article, we will see how to get the current user location in laravel 9. Many times we are required to...
Mar-23-2022
Today I will show you how to add a bootstrap popup modal on button click. many times we need to set confirmation on the...
May-27-2020
Hello, laravel web developers! In this article, we'll see how to validate forms in laravel 11 Livewire. In lara...
Jun-12-2024