How to Send Mail using Queue in Laravel?

Admin   PHP   35  2020-08-12 07:37:12

In this tutorial, I would like to share with you how to create queue job for email send in Laravel 5. We can use database, redis, amazon sqs and beanstalkd for queue driver. I create simple example of queue job for mail send that will help more for less load time.

Laravel Queue will help to save time of loading. If you are work on your big application like CRM, E-Commerce etc website then you require to make job for each task. It would be great if you use queue, because it's very fast and visitor will happy to see loading time.

Here, i am going to share very simple example to create query with database driver for test email sending. You can definitely understand how to work queue and how it's easy. If you haven't used before then don't worry, here if from starch and very simple. This is for startup developer on queue task.

So, let's follow bellow few step and get full example of how to create queue in Laravel.

Preview:

Step 1 : Create Mail

We are going from scratch and in first step, we will create email for testing using Laravel Mail facade. So let's simple run bellow command.

php artisan make:mail SendEmailTest

Now you will have new folder "Mail" in app directory with SendEmailTest.php file. So let's simply copy bellow code and past on that file.

app/Mail/SendEmailTest.php

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;

use Illuminate\Mail\Mailable;

use Illuminate\Queue\SerializesModels;

use Illuminate\Contracts\Queue\ShouldQueue;

class SendEmailTest extends Mailable

{

use Queueable, SerializesModels;

/**

* Create a new message instance.

*

* @return void

*/

public function __construct()

{

}

/**

* Build the message.

*

* @return $this

*/

public function build()

{

return $this->view('emails.test');

}

}

Ok, now we require to create email view using blade file. So we will create simple view file and copy bellow code om following path.

resources/views/emails/test.blade.php

<center>

<h2 style="padding: 23px;background: #b3deb8a1;border-bottom: 6px green solid;">

<a href="">Visit Our Website : </a>

</h2>

</center>

<p>Hi, Sir</p>

<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod

tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,

quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo

consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse

cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non

proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>

<strong>Thank you Sir. :)details = $details;

}

/**

* Execute the job.

*

* @return void

*/

public function handle()

{

$email = new SendEmailTestMail();

Mail::to($this->details['email'])->send($email);

}

}

Step 4 : Use Queue Job

Now time is use and test created queue job, so let's simple create route with following code for testing created queue.

routes/web.php

Route::get('email-test', function(){

$details['email'] = '[email protected]';

dispatch(new App\Jobs\SendEmailTest($details));

dd('done');

});

Ok route is defined, you can watch your queue process using laravel queue command, so let's run bellow command:

php artisan queue:listen

Now run your project and bellow link:

http://localhost:8000/email-test

I hope it can help you....