In this article, we'll see how to send email using a queue in laravel 11. Here, we'll learn the concept of the queue in laravel 11 and send a mail with the help of a queue in laravel. Laravel allows you to easily create queued jobs that may be processed in the background.
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.
To install a Laravel 11 application, execute the following composer command:
composer create-project laravel/laravel laravel-11-example
In the .env file, 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
Let's add mail configuration into the .env file including MAIL_HOST, MAIL_PORT, MAIL_USERNAME, and MAIL_PASSWORD. Here, we're using Gmail for sending mail, but you can also use Mailtrap for testing purposes.
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=test@gmail.com
MAIL_PASSWORD=password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=example@gmail.com
MAIL_FROM_NAME="${APP_NAME}"
To create a mail class named TestMail
for sending emails, run the following command:
php artisan make:mail TestMail
app/Mail/TestMail.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 TestMail 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: 'Test Mail',
);
}
/**
* Get the message content definition.
*/
public function content(): Content
{
return new Content(
view: 'emails.testMail'
);
}
/**
* Get the attachments for the message.
*
* @return array
*/
public function attachments(): array
{
return [];
}
}
In this step, we'll create a TestController
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 TestController
app/Http/Controllers/TestController.php
<?PHP
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Mail;
use App\Mail\TestMail;
class TestController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index()
{
$mailData = [
'title' => 'Mail from Techsolutionstuff',
'body' => 'This is testing email using Queue.'
];
Mail::to('test@gmail.com')->queue(new TestMail($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\TestController;
Route::get('send-mail', [TestController::class, 'index']);
Next, create a file named testMail.blade.php and add HTML content for sending emails.
resources/views/emails/testMail.blade.php
<!DOCTYPE html>
<html>
<head>
<title>How to Send Email using Queue in Laravel 11 - Techsolutionstuff</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: