• +91 9806724185,+91 9685733903
  • +91 9806724185

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.

MAIL_DRIVER=smtp

MAIL_HOST=mailtrap.io

MAIL_PORT=2525MAIL_USERNAME=null

MAIL_PASSWORD=null

MAIL_ENCRYPTION=null

And after changing the setting we get this:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=youremail@gmail.com
MAIL_PASSWORD=youremailpassword
MAIL_ENCRYPTION=tls

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:

<?PHP

return [

'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'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:

<?PHP

return [

'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.gmail.com'),
'port' => env('MAIL_PORT', 587),
'from' => ['address' => 'youremail@gmail.com',
'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.

Route::get('/sendMail',
['uses'=>'UserController@sendEmail',
'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.
send.blade.php:

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

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)

{
$user=User::all();
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

Laravel Framework: Easily Connect Your Database

Laravel framework makes it very easy or flexible to manage your database connections through app/config/database.php and through .env. Given, Below Code is how you have to do it using the app/config/database.php file. When you first Read more…

Technical Solutions

Assist your business with Zoho Integration Services in 2019

If your handling many small businesses at a time then, taking your apps to the cloud, you may have come across Zoho Services. Some say that Zoho is just a Google Apps knockoff, it's not. Read more…

Technical Solutions

Google Maps v/s Bing Maps: Comparing two Giants Corporate

Everyone wants to reach his destination, Its Only possible through the Mapping system. Home to the Meeting, Mapping system coverage is everywhere. Recently google maps discontinue his free API Support and Companies have no idea Read more…