How To Use Laravel 10 Queue To Send Emails Tutorial

Reading Time: 7 minutes
334 Views

Laravel’s queue system provides a powerful mechanism for handling time-consuming tasks, such as sending emails, in a background process, ensuring a smoother and more responsive user experience. Leveraging Laravel 10’s queue system to send emails allows developers to offload email sending tasks to background jobs, optimizing application performance.

In this tutorial, we’ll see the comprehensive process of utilizing Laravel 10’s queue system to send emails. This functionality empowers developers to queue email sending tasks, enabling faster responses for users while the emails are processed asynchronously in the background.

Read More: Build WordPress User Login Using Laravel Corcel Tutorial

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

Step #1: Setup Mail Class with Configuration

Create a Mail class using Laravel Mail facade,

php artisan make:mail SendEmailTest

It will create a new folder “Mail” in app directory with SendEmailTest.php file.

Open app/Mail/SendEmailTest.php file and write this complete code into it,

PHP

Next,

We have to create a blade file for email template,

Read More: Laravel with Gupshup API Send WhatsApp Message

Email Template Settings

Create a blade view file test.blade.php inside resources/views/emails folder.

Open file and write this complete code into it,

PHP

Step #2: How To Configure SMTP in Laravel?

To configure SMTP details, open up the file .env from application root.

We will pass mail driver as gmail server, mail host, mail port, mail username, mail password.

PHP

Step #3: Queue Configuration

Configure queue driver,

Open .env file and update this key value,

QUEUE_CONNECTION=database

After that you need to generate migration and create tables for queue,

php artisan queue:table

Run Migrations,

php artisan migrate

Read More: How To Fix Laravel “Vite Manifest Not Found” Error Tutorial

Step #4: Setup Queue Job

Open project terminal and run this command.

php artisan make:job SendEmailJob

It will create app/Jobs/SendEmailJob.php queue job file. Open file and add this complete code into it,

PHP

Step #5: Add Route

Open web.php file from /routes folder. Add this route and code into it.

PHP

Step #6: Test Queue Job

Next, you have to run the given command to see queue process.

php artisan queue:work

Don’t close this terminal tab. It will show your running job status.

Application Testing

Run this command into project terminal to start development server,

php artisan serve

URL: http://127.0.0.1:8000/email-test

Output

done

Job Queue Status

That’s it.

We hope this article helped you to learn about How To Use Laravel 10 Queue To Send Emails Tutorial in a very detailed way.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.