How to Send Bulk Mail Using Queue in Laravel 8

Websolutionstuff | Feb-10-2021 | Categories : Laravel PHP

In this article, we will see how to send bulk mail using a queue in laravel 8. Laravel queue is used for sending bulk mail with a background process, as we know if we are sending single mail in the laravel application it is working properly without taking more time but if you want to send multiple emails in laravel then it will take too mauch time and also you can not do any operation during this time periods.

So, let's see laravel 6/7/8 sending bulk mail using a queue and how to send bulk mail using a queue in laravel 6/7/8.

So, if you want to send bulk mail then you need to use laravel queue using queue we can send multiple emails in laravel with the background process. let's start and follow the below steps for how to send bulk mail in laravel 6/7/8.

Step 1: Change In .env File

We need to change the configuration for the queue set up in the .env file as below, here we have used mailtrap.io.

MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=your_username
MAIL_PASSWORD=your_passowrd
MAIL_ENCRYPTION=TLS

QUEUE_DRIVER=database

 

 

 Step 2: Create Route

In this step, we will create routes for sending bulk mail using a queue.

use App\Http\Controllers\SendMailController;

Route::get('send/mail', [SendMailController::class, 'send_mail'])->name('send_mail');

 

Step 3: Create Queue Table

Now, we will create a 'jobs' table in the database. So, copy the below command and run it in your terminal.

php artisan queue:table

php artisan migrate​

 

Step 4: Create Controller

In this step, we will create SendMailController and add the below code.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class SendMailController extends Controller
{
    public function send_mail(Request $request)
    {
    	$details = [
    		'subject' => 'Test Notification'
    	];
    	
        $job = (new \App\Jobs\SendQueueEmail($details))
            	->delay(now()->addSeconds(2)); 

        dispatch($job);
        echo "Mail send successfully !!";
    }
}

 

 

Step 5: Create a Job

Now, we need to create the SendQueueEmail.php file in the app\Jobs folder using the below command in your terminal and copy the below code. 

php artisan make:job SendQueueEmail

 

<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\User;
use Mail;

class SendQueueEmail implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    protected $details;
    public $timeout = 7200; // 2 hours

    /**
     * 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();
        $input['subject'] = $this->details['subject'];

        foreach ($data as $key => $value) {
            $input['email'] = $value->email;
            $input['name'] = $value->name;
            \Mail::send('mail.Test_mail', [], function($message) use($input){
                $message->to($input['email'], $input['name'])
                    ->subject($input['subject']);
            });
        }
    }
}

 

Step  6: Create Mail Blade

In this step, we will create a mail blade file in resources/views/mail/Test_mail.blade.php and write and add the below mail content.

Hi <br/>
This is Test Mail.<br />
Thank you !!

And run the below command in your terminal to send manually mail.

php artisan queue:listen

 

how to send bulk mail using queue in laravel 8

 


You might also like:

Recommended Post
Featured Post
How To Toggle Dark and Light Mode using jQuery
How To Toggle Dark and Light M...

In this article, we will see how to toggle between dark and light modes using jquery. As per the current trend of web de...

Read More

Nov-24-2020

How to Install Jenkins on Ubuntu
How to Install Jenkins on Ubun...

In today's fast-paced software development landscape, I've understood the significance of continuous integration...

Read More

Aug-07-2023

Angular 15: Unleashing Modern Web Development
Angular 15: Unleashing Modern...

Angular 15 is the latest iteration of one of the most popular JavaScript frameworks for building dynamic web application...

Read More

Jun-05-2023

How to Install Composer on Ubuntu 22.04
How to Install Composer on Ubu...

Hey there! If you're diving into the world of PHP development on your Ubuntu 22.04 machine, you'll likely come a...

Read More

Jan-10-2024