In this tutorial you will learn about the Laravel 8 Send Mail using Queue Tutorial and its application with practical example.
In this tutorial Laravel 8 Send Mail using Queue Tutorial I will show you how to send mail using queue in laravel 8. In this tutorial you will learn to send mails using queue job In laravel application. In Laravel 8 you can use mailtrap, smtp, Mailgun, Postmark, Amazon SES, and sendmail for sending emails.
When we want to send mails in bulk it takes enough time to send all mails. Sending mails in bulk result time consuming process and may not guarantee all mails being sent. In laravel we can use queue job for sending mail in bulk. In this example we will create a queue job with database driver for sending emails.
Laravel 8 Send Mail using Queue Tutorial
In this step by step tutorial I will demonstrate you how to send mail using queue job In laravel application. Please follow instructions given below:
- Step 1 – Install Laravel 8 App
- Step 2 – Configuration SMTP & Database
- Step 3 – Create Mailable Class
- Step 4 – Add Email Send Route
- Step 5 – Create Directory And Mail Blade View
- Step 6 – Configuration Mail Queue
- Step 7 – Build Queue Job For Sending Mail
- Step 8 – Run Development Server
Step 1 – Install Laravel 8 App
First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command
1 |
composer create-project --prefer-dist laravel/laravel blog |
Step 2 – Configuration SMTP & Database
In this step we will be setting smtp detail for mail configuration in Laravel, We will be using following Gmail SMTP details such as username, password inside the .env file.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=db name DB_USERNAME=db user name DB_PASSWORD=db password MAIL_DRIVER=smtp MAIL_HOST=smtp.gmail.com MAIL_PORT=587 MAIL_USERNAME=Add your user name here MAIL_PASSWORD=Add your password here MAIL_ENCRYPTION=tls |
Step 3 – Create Mailable Class
The mailable class in Laravel used for sending emails. Now, lets create a new laravel mailable class using following artisan command:
1 |
php artisan make:mail NotifyMail |
Once the above command executed, it will create a new laravel mailable class NotifyMail.php in app/Mail/ directory. Open the NotifyMail.php file and put the following code in it.
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 |
<?php namespace App\Mail; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Mail\Mailable; use Illuminate\Queue\SerializesModels; class NotifyMail 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('view.name'); } } |
Now add email template name in build class of the above created notifymail class.
1 2 3 |
return $this->view('view.name'); to return $this->view('emails.demoMail'); |
In next step, Create email template named demoMail.blade.php inside resources/views/emails directory. That’s why have added view name email.
Step 4 – Add Send Email Route
After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 2 3 4 5 6 7 8 |
Route::get('email-test', function(){ $details['email'] = 'your_email@gmail.com'; dispatch(new App\Jobs\SendEmailJob($details)); dd('done'); }); |
Step 5 – Create Directory And Mail Blade View
In this step, create directory name emails inside resources/views directory. Then create an demoMail.blade.php blade view file inside resources/views/emails directory. And update the following code into it:
1 2 3 4 5 6 7 8 9 10 11 12 |
<!DOCTYPE html> <html> <head> <title>Laravel 8 Send Email Example</title> </head> <body> <h1>This is test mail</h1> <p>Laravel 8 send email example</p> </body> </html> |
Step 6 – Configuration Mail Queue
1 |
QUEUE_CONNECTION=database |
Then open the terminal and run following command for queue database tables:
1 |
php artisan queue:table |
Next, migrate tables into database:
1 |
php artisan migrate |
Step 7 – Build Queue Job For Sending Mail
Now we will create a queue job to send emails using following artisan command:
1 |
php artisan make:job SendEmailJob |
Then open SendEmailJob.php file which is placed on “app/Jobs” directory. And update the following mail queue code into it:
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 |
<?php namespace App\Jobs; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use App\Mail\SendEmailTest; 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); } } |
Step 8 – Run Development Server
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
1 |
http://127.0.0.1:8000/email-test |