In this tutorial, we will implement a laravel 8 send mail in the queue. Here you will learn the laravel 8 queue mail send. Alright, let’s dive into the steps.
Sometimes, you see some process take time to load like email send, payment gateway, etc. When you send emails for verification or send invoices then it loads time to send mail because it is services. If you don't want to wait for the user to send an email or other processes on loading the server-side process then you can use the queue. Because it's very fast and visitors will happy to see the loading time.
Here, I am going to share a very simple example to create a queue with a 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.
first of all, we need to get a fresh Laravel 8 version application using the bellow command, So open your terminal OR command prompt and run bellow command:
composer create-project --prefer-dist laravel/laravel blog
We are going from scratch and in the first step, we will create an email for testing using the Laravel Mail facade. So let's simple run bellow command.
Now you will have a new folder "Mail" in-app directory with SendEmailTest.php file. So let's simply copy bellow code and past on that file.
Ok, now we require to create an email view using the blade file. So we will create a simple view file and copy bellow code om following path.
After configuration of the view file, we have to set up for email send, So let' set configuration in the .env file:
Now in the next step, we will make configuration on the queue driver so first of all, we will set the queue driver "database". You can set as you want also we will define driver as redis too. So here define database driver on the ".env" file:
After that, we need to generate migration and create tables for the queue. So let's run bellow command for queue database tables:
now we will create a queue job, bey, following command, this command will create a queue job file with Queueable. So let's run bellow command:
now you have SendEmailJob.php file in the "Jobs" directory. So let's see that file and put the bellow code on that file.
Now time is use and test the created queue job, so let's simply create a route with the following code for the testing created queue.
The okay route is defined, you can watch your queue process using the laravel queue command, so let's run bellow command:
You can also clear the config cache using bellow command:
Now you can run the project using bellow command:
Now run your project and bellow link: