Laravel 9 Send mail using queue example; In this tutorial, we will learn how to send mail using queue job in laravel 9 app. And, will learn step by step how to send email in laravel 9 using queue job with smtp drivers like Mailgun, Postmark, Amazon SES, office365, gmail and sendmail.
The mail queue is a directory that stores data and controls files for mail messages that the sendmail command delivers. If this is so, mail messages must be stored temporarily. If a remote host does not answer a request for a mail connection, the mail system queues the message and tries again later.
Note that, we can use several SMTP drivers details like Mailgun, Postmark, Amazon SES, office365, gmail and sendmail in .env file for sending email in laravel 9.
How to Send Mail using Queue in Laravel 9
Follow the following steps to send mail using queue in laravel 9 apps:
- Step 1 – Install Laravel 9 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 9 App
In this step, use the following command to install or download laravel application:
composer create-project --prefer-dist laravel/laravel blog
Step 2 – Configuration SMTP & Database
In this step, we need to configure smtp details in .env file like following:
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 user name here MAIL_PASSWORD=Add password here MAIL_ENCRYPTION=tls
Note that:- If we are sending a mail using Gmail we have to allow non-secure apps to access Gmail we can do this by going to Gmail settings here.
Once less secure apps are enabled; now we can use Gmail for sending the emails.
Step 3 – Create Mailable Class
In this step, use the below given command to create NotifyMail mailable class:
php artisan make:mail NotifyMail
<?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'); } }
By whatever name we will create an email template. That want to send. Do not forget to add an email template name in build class of the above created notifymail class.
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
In this step, open /web.php, so navigate to routes directory. And then add the following routes for send email:
Route::get('email-test', function(){ $details['email'] = '[email protected]'; 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:
<!DOCTYPE html> <html> <head> <title>Laravel 9 Send Email Example</title> </head> <body> <h1>This is test mail from Tutsmake.com</h1> <p>Laravel 9 send email example</p> </body> </html>
Step 6 – Configuration Mail Queue
In this step, configuration on queue driver. So open .env file and define database queue driver on “.env” file like following:
QUEUE_CONNECTION=database
Then open the terminal and run following command for queue database tables:
php artisan queue:table
Next, migrate tables into database:
php artisan migrate
Step 7 – Build Queue Job For Sending Mail
In this step. create queue job using the following command:
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:
<?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
In this step, use this PHP artisan serve command to start server locally:
php artisan serve
Then open browser and fire the following URL on it:
http://127.0.0.1:8000/email-test
Conclusion
How to send mail using queue in Laravel 9; In this tutorial, we have learned how to create a mailable class in laravel 9. And using this class on how to send emails in laravel 9.
Recommended Laravel Tutorials
If you have any questions or thoughts to share, use the comment form below to reach us.