Laravel 10 Send Bulk Mail Using Queue

Websolutionstuff | Mar-13-2023 | Categories : Laravel PHP

In this article, we will see laravel 10 send bulk mail using a queue. Here, we will learn about how to send bulk mail using a queue in laravel 10. 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 much time and also you can not do any operation during this time periods.

So, let's see how to send bulk mail using a queue in laravel 10, how to send bulk mail in laravel 10 using a queue, laravel 10 send an email, and send mail in laravel 10.

Step 1: Install Laravel 10

 In this step, we will install laravel 10 using the following command.

composer create-project --prefer-dist laravel/laravel laravel_10_send_mail

 

Step 2: Update .env File

Now, we will set up the mail configuration in the .env file as below. Here we have used mailtrap.io. So, you can use it as per your requirements.

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 3: Create Route

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

<?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\SendMailController;
  
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
  
Route::get('send/mail', [SendMailController::class, 'sendMail'])->name('send_mail');

 

Step 4: 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 5: Create Controller 

In this step, we will create SendMailController using the following command.

php artisan make:controller SendMailController

app/Http/Controllers/SendMailController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

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

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

 

 

Step 6: Create Job

Now, we will create the SendQueueEmail.php file using the following command.

php artisan make:job SendQueueEmail

app/Jobs/SendQueueEmail.php

<?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['name'] = $value->name;
            $input['email'] = $value->email;

            \Mail::send('mail.mailExample', [], function($message) use($input){
                $message->to($input['email'], $input['name'])
                    ->subject($input['subject']);
            });
        }
    }
}

 

Step  7: Create Mail Blade

In this step, we will create a mailExample.blade.php file. So, add the following code to that file.

resources/views/mail/mailExample.blade.php

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

 

 

Output:

laravel_10_send_bulk_mail_using_queue_output

 


You might also like:

Recommended Post
Featured Post
Laravel 8 Highcharts Example Tutorial
Laravel 8 Highcharts Example T...

Hello guys, In this tutorial we will see laravel 8 highcharts example tutorial. you will learn how to imple...

Read More

Jul-02-2021

How To Create Dark and Light Mode Website using jQuery
How To Create Dark and Light M...

In this article, we will see how to create a dark and light mode website using jquery. As you can see many websites and...

Read More

Nov-21-2020

How to Run Specific Seeder in Laravel 8
How to Run Specific Seeder in...

In this example, we will learn how to run a specific seeder in laravel 8. If you want to run only one seeder in laravel...

Read More

Jan-19-2022

Google Map With Draggable Marker Example
Google Map With Draggable Mark...

In this example I will show you how to add a google map to your website, In many websites you can see they have added ma...

Read More

Jun-11-2020