Laravel 11 Send Email Queue Tutorial

In this tutorial, we will show you how to send email using queue worker in laravel 11 application. Queue sends emails in the background and is useful for sending large numbers of emails.

Here are steps to send email using queue in laravel:

Step 1 – Configure Mail Server

Open the .env file in the root directory of your Laravel application, And configure mail server details in it; Something like this:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=Add your user name here
MAIL_PASSWORD=Add your password here
MAIL_ENCRYPTION=tls

To allow non-secure apps to access Gmail to send emails, you can go to your Gmail settings here and click Enable less secure apps.

Step 2 – Setup Queue Configuration

Open the .env file in the root directory of your Laravel application, And setup queue email configuration:

QUEUE_CONNECTION=database

Create queue table in database using the following command:

php artisan make:queue-table

Run migration command:

php artisan migrate

Step 3 – Create Mail Class

Run the following command to create mail class, which is used to send mails

php artisan make:mail HelloMail

Once you have created notifyMail class, Then visit the app/mail directory and open notifyMail.php file and add the following code into it:

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;

class HelloMail extends Mailable
{
use Queueable, SerializesModels;

/**
* Create a new message instance.
*/
public function __construct(private string $title, private string $body)
{
}

/**
* Get the message envelope.
*/
public function envelope(): Envelope
{
return new Envelope(
subject: 'Welcome on tutsmake.com',
);
}

/**
* Get the message content definition.
*/
public function content(): Content
{
return new Content(
view: 'emails.helloMail',
with: [
'title' => $this->title,
'body' => $this->body,
],
);
}

/**
* Get the attachments for the message.
*
* @return array<int, \Illuminate\Mail\Mailables\Attachment>
*/
public function attachments(): array
{
return [];
}
}

Step 4 – Define Route

Define a route in the web.php file to handle the request to send mail; Something like this:

use App\Http\Controllers\HelloController;

Route::get('hello-email', [HelloController::class, 'sendHelloMail']);

Step 5 – Create a Controller File

Run the following command to make a controller file to handle mail sending process into it:

php artisan make:controller HelloController

Open a HelloController.php controller file from app/http/controller folder and update the following code into it:

<?php

namespace App\Http\Controllers;

use App\Mail\HelloMail;
use Illuminate\Support\Facades\Mail;

class HelloController extends Controller
{
public function sendHelloMail()
{
$title = 'Hello Mail from Tutsmake.com';
$body = 'This is the first email to send in laravel 11 application from tutsmake.com';

Mail::to('[email protected]')->queue(new HelloMail($title, $body));

return "Email has been sent successfully!";
}
}

Step 6 – Create an Email View

Create a helloMail.blade.php file inside resources/views/emails folder and add the following HTML code into it:

<!DOCTYPE html>
<html>
<head>
<title>{{ $title }}</title>
</head>
<body>
<p>{{ $body }}</p>
</body>
</html>

Step 7 – Test Application

Run php artisan serve command to start application server to testing:

php artisan serve

Open browser with URL http://127.0.0.1:8000/hello-email to send first hello mail from laravel 11 application.

To see queue email process, use the following command:

php artisan queue:work

Conclusion

Congratulations! In this example, you taught how to send the mail in using queue in Laravel 11 application using Mailable class with gmail smtp.

Recommended Guides

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *