How to Send Bulk Mail in Laravel 8 Using Queue

In this post we will learn how to send bulk mail in laravel 8 using queue. Aslo we learn Laravel 8 send Bulk Mail in Background using Queue, Laravel 8 Send Email with Queue Tutorial.

The Laravel line is used to send bulk mail in laravel 8 through the background process, as we know if we send single mail to the laravel app it works well without taking much time but if you want to send bulk mail in laravel 8 it will take too long and you can’t to do any work at this time.

Therefore, if you want to send more email you will need to use the laravel line using the line we can send more emails to laravel through the background process. let’s get started and follow the steps below on how to send more emails via laravel.

Read Also :  How to get Random Value from Array in laravel

Send Bulk Mail in Laravel 8 Using Queue

Step 1 : Edit In .env File

We need to edit configuration foe queue set up in .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 send bulk mail using queue.

use App\Http\Controllers\SendMailController;

Route::get('send/mail', [SendMailController::class, 'send_mail'])->name('send_mail');
Step 3 : Create Queue Table
php artisan queue:table

php artisan migrate​

Step 4 : Create Controller 

Create SendMailController and add 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 Job

Create the SendQueueEmail.php file in the app\Jobs folder using below command in your terminal and copy 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

Create mail blade file in resources/views/mail/Test_mail.blade.php and write add  below mail content.

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

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

php artisan queue:listen