Laravel 9 Send mail using queue example; In this tutorial I would like to share with you how to send email using queue job in laravel 9 app. This tutorial will explain 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.
Email sent from the Laravel application represent a mailable class; every mailable class is kept inside the app/Mail folder. The mail queue is a directory that stores data and controls files for mail messages that the sendmail command delivers.
The php artisan make:mail your_class_name
command forms the Mailable class, this also creates the blade view email template, which manages the mailer view. So get ready to enhance the impetus of sending simple mail using queue in Laravel application.
How to Send Mail using Queue in Laravel
Follow the following steps to send mail using queue in laravel apps:
- Step 1: Install Laravel App
- Step 2: Configuration SMTP & Database
- Step 3: Create Mailable Class
- Step 4: Make Routes
- Step 5: Create Blade View File
- Step 6: Configuration Mail Queue
- Step 7: Build Queue Job For Sending Mail
- Step 8: Start Your Application
Install Laravel App
First create new laravel application adding the following command in terminal.
composer create-project --prefer-dist laravel/laravel laravel-mail-queue
Configuration SMTP & Database
In this step, we need to add the database credentials and update the 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
Create Mailable Class
Now we need to make the mail class using the following command:
php artisan make:mail NotifyMail
Now open the recent generated file and put the below code on it;
<?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('emails.test-email');
}
}
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.test-email');
Make Routes
In this step, open /web.php, so navigate to routes directory. And then open routes/web.php file add the following routes for send email;
Route::get('send-email', function(){
$details['email'] = 'your_email@gmail.com';
dispatch(new App\Jobs\SendEmailJob($details));
dd('done');
});
Create Blade Files
Now create an test-mail.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>
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
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);
}
}
Start Your Application
php artisan serve
Then open browser and fire the following URL on it:
http://127.0.0.1:8000/send-email
We have completed seding email in laravel using queue tutorial, i believe you would love this tutorial and it will help you.