How to send Bulk Email using Laravel Queue feature?

In Laravel Development  Services, Are you want to Send bulk Emails, it's better to use Queues. Most of the developers nowadays using Queues for sending those Bulk emails.

Why this?

It speeds up web requests to your Web application. It will help you run the sending bulk e-mail after a certain time gap so, you can easily backtrack whether the job was done or not. Let’s take look to Code which helps to send email using laravel Queue.

1. First, open your .env file in your project as here we gonna change some configuration for sending mail.

See, the before by default .env setting in MAIL section with fresh laravel installation you get. And here we need to change the setting.





And after changing the setting we get this:


2. Now from your root folder go to Config folder and inside of this open the mail.php as here also we need to change some setting:

Before the change of setting with fresh installation we get this in that mail.php file:


return [

'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', ''),
'port' => env('MAIL_PORT', 587),'from' => ['address' => null,
'name' => null],'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'sendmail' => '/usr/sbin/sendmail -bs',];

And after changing the setting we will get this:


return [

'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', ''),
'port' => env('MAIL_PORT', 587),
'from' => ['address' => '',
'name' =>'YourGmailUserName'],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'sendmail' => '/usr/sbin/sendmail -bs','pretend' => false,];

3. Then we need to focus on the Queue. So first of change the .env file setting for QUEUE_DRIVER=sync to QUEUE_DRIVER=database.

4. Our task is almost done. Now we gonna set the routes.php and make a controller.php to send the email. Let's start with routes

5. In routes.php we need to initialize in which user it will send the user. I have given a demo code you can change with your required code as it's almost the same.

'as' =>'sendMail',]);

6. We have to make a view page as we need to send the email with that page only. So here we gonna make a send.blade.php in resources/views folder which we'll send as email.

<head> Email Demo</head>
<p>Thanks for Registering with User Id</p>

7. Now let's create a RESTful Resource Controller for sending Email. Here we have created a send email controller.

public function send email(Request $request)

Mail::queue('send', ['user' => $user],

function($m) use ($user)
foreach ($user as $user)
$m->to($user->email)->subject('Email Confirmation');

Don't forget to use namespace use Mail; above of your controller.

7. If you want to delay the queued e-mail message, you can use a later method. You need to add a number of seconds by which you want to delay the email as a first argument to the method.

That's it. It will find a list of users with the User:: all() then it will send the emails to the users.

8. We need to create a Queue table in the database using the following command before clicking on the route:

PHP artisan queue: table
PHP artisan migrate

9. Before starting the project we need to run a listener to listen to the Queue Request. But I'm gonna introduce with the new method as listen is high CPU usage. It's better to use a daemon. So Run the following command:

PHP artisan queue:work --daemon --tries=3

10. So when we access the send email route it will send email to a list of users using this Queue and complete the process.

Contact us for the Laravel Development Company.

Related Posts

Technical Solutions

Let’s Check Some Important Eye-Opening Website Facts

It’s End of 2019—so you’ve probably realized by now that your website has an effect on the perceived credibility and quality of your business. But very few people know just how colossal this effect actually Read more…

Technical Solutions

Integrate Zoho CRM For Effective Business Management

Customer Relationship Management (CRM) by Zoho is an approach that helps businesses in maintaining a better relationship with existing customers and brings in more customers. Zoho CRM software is an entire strategy to take up Read more…

Technical Solutions

Steps to Improve Performance of Node Servers

  In the past, Node.js Development was often not an option when building applications that require CPU intensive computation. This is due to its non-blocking, event-driven I/O architecture. With the advent of thread workers in Read more…