In this tutorial, we'll explore how to efficiently send bulk emails using a queue in Laravel 10. Leveraging Laravel's queue system allows us to process multiple email dispatches in the background, optimizing performance and allowing for uninterrupted application operation.
In this guide, we'll see how to send bulk mail using a queue in laravel 10. Also, you can use laravel 8 and laravel 9.
So, let's see how to send bulk mail using a queue in laravel 10, larave10 sending bulk mail using a queue, how to send multiple emails in laravel 8/9/10, and send emails using a queue in laravel 9/10.
Begin by installing Laravel 10 with the following command:
composer create-project --prefer-dist laravel/laravel laravel_10_send_bulk_emails
Update the .env
file with your mail configuration details. Here, we'll use Mailtrap as an example:
MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=your_username
MAIL_PASSWORD=your_password
MAIL_ENCRYPTION=tls
QUEUE_DRIVER=database
Define routes for sending bulk mail using the queue in the routes/web.php
file:
use App\Http\Controllers\SendMailController;
Route::get('send/mail', [SendMailController::class, 'sendBulkMail'])->name('send_mail');
Generate a jobs table in the database by running the migration command:
php artisan queue:table
php artisan migrate
Generate a SendMailController
using the Artisan command:
php artisan make:controller SendMailController
Then, add the following code to the SendMailController.php
file:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Jobs\SendBulkMailJob;
use App\Models\User;
class SendMailController extends Controller
{
public function sendBulkMail(Request $request)
{
$details = [
'subject' => 'Test Notification'
];
$job = (new SendBulkMailJob($details))->delay(now()->addSeconds(2));
dispatch($job);
return "Mail sent successfully!";
}
}
Generate a job named SendQueueEmail
using Artisan:
php artisan make:job SendBulkMailJob
Then, update the SendBulkMailJob.php
file in the app/Jobs
directory with the following code:
<?php
namespace App\Jobs;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Mail;
class SendBulkMailJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $details;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($details)
{
$this->details = $details;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$data = User::all();
foreach ($data as $user) {
Mail::send('mail.mailExample', [], function ($message) use ($user) {
$message->to($user->email, $user->name)
->subject($this->details['subject']);
});
}
}
}
Create a view file named mailExample.blade.php
in the resources/views/mail
directory with the content:
Hi {{ $user->name }},<br>
This is a test mail.<br>
Thank you!
Start the queue listener by running the following command:
php artisan queue:listen
You might also like:
- Read Also: How to Generate PDF File using DomPDF in Laravel 10
- Read Also: How to Send Email with PDF Attachment in Laravel 10
- Read Also: Laravel 10 Password and Confirm Password Validation
- Read Also: How To Create Dynamic Line Chart In Laravel 10