As a Laravel developer, one of the common requirements is sending emails from your application. However, processing email sending synchronously can impact the performance and responsiveness of your application, especially when dealing with large volumes of emails.
That's where Laravel's powerful feature called queues comes to the rescue. In this article, I will guide you through the process of leveraging queues in Laravel 10 to send emails asynchronously, resulting in improved performance and an enhanced user experience.
In laravel 10 send mail using a queue example we will set up the mailtrap for sending an email. Laravel queues provide a unified queueing API across a variety of different queue backends, such as Amazon SQS, Redis, or even a relational database.
Table of content:
- Install Laravel 10
- Creating a Mailable Class
- Configuration of Queue
- Create Queue Job
- Dispatching Mail Jobs to the Queue
- Processing the Queue Jobs
- Run Laravel 10 Application
- Conclusion
In this step, we will install the laravel 10 application using the composer command.
composer create-project --prefer-dist laravel/laravel laravel_10_send_mail
Generate a Mailable class using the make:mail
Artisan command.
php artisan make:mail SendEmailQueueDemo
Now, you will find the new Mail folder in the app directory with the SendEmailQueueDemo.php file. So, add the below code to this file.
app/Mail/SendEmailQueueDemo.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 SendEmailQueueDemo extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*/
public function __construct()
{
}
/**
* Get the message envelope.
*/
public function envelope(): Envelope
{
return new Envelope(
subject: 'Laravel 10 Send Mail Using Queue',
);
}
/**
* Get the message content definition.
*/
public function content(): Content
{
return new Content(
view: 'email.demo',
);
}
/**
* Get the attachments for the message.
*
* @return array
*/
public function attachments(): array
{
return [];
}
}
After that, we need to create an email view using a blade file. So, we will create demo.blade.php following the path.
resources/views/email/demo.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Laravel 10 Send Mail Using Queue - Techsolutionstuff</title>
</head>
<body>
<center>
<h2>
<a href="https://vidvatek.com/">Visit Our Website : Vidvatek</a>
</h2>
</center>
<p>Hello,</p>
<p>This mail send using queue listen in laravel 10.</p>
<strong>Thanks & Regards.</strong>
</body>
</html>
Now, we will configure of view file, which we have to set up for email send, So let's configuration in the .env file.
MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=your_username
MAIL_PASSWORD=your_password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"
Open the .env
file in the root directory of your Laravel project. Set the QUEUE_CONNECTION
variable to the desired queue driver. For example.
QUEUE_CONNECTION=database
Laravel supports various queue drivers like database, Redis, Beanstalkd, etc. Choose the appropriate one based on your requirements.
After that, we need to generate migration and create tables for queues. So, let's run the below command for queue database tables.
php artisan queue:table
Now, run the migration in your terminal.
php artisan migrate
In this step, we will create a new queue job. So, run the below command in your terminal.
php artisan make:job SendEmailQueueJob
After running the above command the SendEmailQueueJob.php file is created.
app/Jobs/SendEmailQueueJob.php
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Mail\SendEmailQueueJob;
use Mail;
class SendEmailQueueJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $send_mail;
/**
* Create a new job instance.
*/
public function __construct($send_mail)
{
$this->send_mail = $send_mail;
}
/**
* Execute the job.
*/
public function handle(): void
{
$email = new SendEmailQueueDemo();
Mail::to($this->send_mail)->send($email);
}
}
Now, we will check the queue job. So, add the below code to the web.php file and dispatch the queue job.
routes/web.php
Route::get('send/email', function(){
$send_mail = '[email protected]';
dispatch(new App\Jobs\SendEmailQueueJob($send_mail));
dd('send mail successfully !!');
});
Now, clear the config cache using the below command for sending mail with a queue in laravel 10.
php artisan config:clear
Now, run this laravel 10 send mail using the queue example with the artisan command.
php artisan serve
Run the queue worker using the queue:work
Artisan command.
php artisan queue:listen
OR
php artisan queue:work
The queue worker will start processing the queued email jobs in the background.
By following these steps and incorporating the provided code examples, you can successfully implement mail queues in your Laravel 10 application. Emails will be sent asynchronously, improving the performance and responsiveness of your application.
You might also like:
- Read Also: Top 10 Tricks and Tips of Laravel
- Read Also: Laravel 10 Import and Export CSV and Excel Files
- Read Also: Building Complete CRUD Application in Laravel 10
- Read Also: AJAX CRUD Operations In Laravel 10: Step-by-Step Guide