Sending Emails Using Queues in Laravel

This guide demonstrates how to send emails using queues in Laravel to improve application performance by processing email sending in the background. Queues help offload time-consuming tasks, ensuring a faster user experience. We’ll use the database queue driver for simplicity, but you can adapt this to other drivers like Redis or Amazon SQS.

Prerequisites

  • A Laravel project set up (composer create-project laravel/laravel example-app)
  • A mail service (e.g., Mailtrap, Gmail SMTP) for testing email sending
  • A database (e.g., MySQL, SQLite) for the queue driver
  • Basic knowledge of Laravel and PHP

Step 1: Set Up the Queue Driver

Laravel supports multiple queue drivers (database, Redis, SQS, etc.). For this example, we’ll use the database driver.

1. Configure the Queue Connection: Update your .env file to set the queue connection to database:

Queue Connection 

2. Create the Queue Tables: Run the following Artisan command to generate the migration for the jobs table:

Artisan command 
Then, migrate the database to create the jobs table:

migrate the database to create the jobs table 

Step 2: Configure Email Settings

Set up your mail driver in the .env file. For this example, we’ll use Mailtrap for testing. Replace the credentials with your own (e.g., from Mailtrap or Gmail).

Configure Email Settings 

Note: If using Gmail, enable “App Passwords” or “Less Secure Apps” (if available) and set MAIL_HOST=smtp.gmail.com, MAIL_PORT=587

Step 3: Create a Mailable Class

Create a mailable class to define the email structure. 

Run the following Artisan command:

 

This generates a DemoMail class in app/Mail/DemoMail.php. Update it as follows:

DemoMail 

The ShouldQueue interface ensures the email is queued.

Step 4: Create an Email Template

Create a Blade template for the email content at resources/views/emails/demo.blade.php:

 

Step 5: Handle email-sending Logic

Let’s simply create a route with the following code. 

 

Step 6: Run the Queue Worker

run the queue worker 
Note: In a production environment, use a process manager like Supervisor to keep the queue worker running. Configure Supervisor with a configuration like:
 
Configure Supervisor 

Replace /path/to/your/laravel/app and your_server_user with your project path and server user.

Step 7: Test the Email Sending

1. Start the Laravel development server:

Laravel development server 

2. In another terminal, run the queue worker:

 

3. Visit http://127.0.0.1:8000/email-test in your browser. You should see the message “Email has been queued for sending!”.

4. You will see layout as like bellow if queue is works:

queue is works 

5. Check your Mailtrap inbox (or other email service) to confirm the email was received.

Why Use Queues?

  • Improved Performance: Queues process emails in the background, reducing response time for users.
  • Reliability: Failed jobs can be retried automatically (configure — tries in queue:work).
  • Scalability: Handle large volumes of emails efficiently.

If you found this helpful, feel free to share or drop a comment. Happy coding with Laravel! 🧱✨