In this tutorial, you learn “how to send email using queue” with laravel 7. I will be explain how to use queue jobs in laravel 7 from scratch. we will simple create email send using queue in this article.
Sometime, need to send bulk email for all users then this process take time to send email to all users. If you don’t want to wait to user for send email or other process on loading server side process then you can use queue. because it’s very fast and visitor will happy to see loading time. Laravel 7 queues provide a unified API across a variety of different queue backends, such as Beanstalk, Amazon SQS, Redis, or even a relational database. Queues allow you to defer the processing of a time-consuming task, such as sending an email. Delaying these time-consuming tasks drastically speeds up web requests to your application.
Here, i am going to explain very simple example to create queue 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.
Following are the steps for integration:
Step 1 – Install Laravel 7 Application
1 2 3 | composer create-project --prefer-dist laravel/laravel testQueueJob |
Step 2: Create a Mail Setup
1 2 3 | php artisan make:mail SendEmailTest |
Now copy below code and paste “app/Mail/SendEmailTest.php”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | <?php namespace AppMail; use IlluminateBusQueueable; use IlluminateMailMailable; use IlluminateQueueSerializesModels; use IlluminateContractsQueueShouldQueue; 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.testQueue'); } } |
No need to create a mail view file, create a view file in “resources/views/emails/testQueue.blade.php” and copy below code and paste.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <!DOCTYPE html> <html> <head> <title>How to send mail using queue in Laravel 6? - Webprepration.com</title> </head> <body> <center> <h2 style="padding: 23px;background: #b3deb8a1;border-bottom: 6px green solid;"> <a href="https://webprepration.com">Visit Our Website : Webprepration.com</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. :)</strong> </body> </html> |
Now we need to configure email & queue setting in .env file.
1 2 3 4 5 6 7 8 9 10 | MAIL_DRIVER=smtp MAIL_HOST=smtp.gmail.com MAIL_PORT=587 MAIL_USERNAME=xyz@gmail.com MAIL_PASSWORD=123456 MAIL_ENCRYPTION=tls QUEUE_CONNECTION=database |
Generate queue table migration using below command
1 2 3 4 | php artisan queue:table php artisan migrate |
Step 3: Create Queue Job using a command
1 2 3 | php artisan make:job SendEmailJob |
Now, the send email function will reside in the job file. So that job file looks like this.
Read Also: Laravel 7 Email Verification
app/Jobs/SendEmailJob.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | <?php namespace AppJobs; use IlluminateBusQueueable; use IlluminateQueueSerializesModels; use IlluminateQueueInteractsWithQueue; use IlluminateContractsQueueShouldQueue; use IlluminateFoundationBusDispatchable; use AppMailSendEmailTest; use Mail; class SendEmailJob implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; protected $details; /** * Create a new job instance. * * @return void */ public function __construct($details) { $this->details = $details; } /** * Execute the job. * * @return void */ public function handle() { $email = new SendEmailTest(); Mail::to($this->details['email'])->send($email); } } |
Now create route for test queue job
1 2 3 4 5 6 7 8 | Route::get('testqueue', function(){ $details['email'] = 'abc@gmail.com'; dispatch(new AppJobsSendEmailJob($details)); }); |
Now you can watch your queue process using laravel queue command, so just run below command.
1 2 3 | php artisan queue:listen |
Now run your project and check it.
I’m a full-stack developer. My hobby and profession to write blog and programming tips that helps to others. I am a great admirer of PHP, Laravel, Codeigniter, AngularJS, Vue.js, Javascript, JQuery, WordPress, Plugin Development, Theme Development and Bootstrap from the early stage.