Laravel 9 Mail Send using Queue Example

10-Apr-2023

.

Admin

Laravel 9 Mail Send using Queue Example

Hi Guys,

This article will provide example of laravel 9 mail send using a queue. you will learn mail send using queue in laravel 9 application. I will give an easy and simple example of how to send mail using queue in laravel 9. In this tutorial, we will look at an example of laravel 9 mail send using a queue.

This tutorial will give you mail send using queue in laravel 9. We will discuss laravel 9 mail send using a queue. let's discuss about laravel 9 mail send using a queue.

Here I will give you a full example for send mail using the queue in laravel 9 application. So let's see below step by step:

Step 1: Download Laravel


Let us begin the tutorial by installing a new laravel application. if you have already created the project, then skip the following step.

composer create-project laravel/laravel example-app

Step 2: Database Configuration

In the second step, we will make database Configuration for the example database name, username, password, etc. So let's open the .env file and fill in all details like as below:

.env

DB_CONNECTION=mysql

DB_HOST=127.0.0.1

DB_PORT=3306

DB_DATABASE=here your database name(blog)

DB_USERNAME=here database username(root)

DB_PASSWORD=here database password(root)

Step 3: Add Mail

In this step, you will create an email for testing using laravel Mail facade. So let's open the terminal and run the below command:

php artisan make:mail TestEmailSend

Now you will have new folder as Mail in app directory with TestEmailSend.php file. So let's put bellow code in this file:

app/Mail/TestEmailSend.php

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;

use Illuminate\Contracts\Queue\ShouldQueue;

use Illuminate\Mail\Mailable;

use Illuminate\Queue\SerializesModels;

class TestEmailSend 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.testMail');

}

}

Step 4: Add Blade File

Now we will create email view file using blade file first you can create "emails" folder in view directory and in this folder we will create simple view file and put bellow code:

resources/views/emails/test.blade.php

<!DOCTYPE html>

<html>

<head>

<title>Laravel 9 Mail Send using Queue Example - Nicesnippets.com</title>

</head>

<body>

<center>

<h2 style="padding: 23px;background: #b3deb8a1;border-bottom: 6px green solid;">

<a href="https://Nicesnippets.com">Visit Our Website : Nicesnippets.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>

Step 5: Mail Configuration

after configuration of view file, we have to setup for email send, So let' set configuration in .env file:

.env

MAIL_DRIVER=smtp

MAIL_HOST=smtp.gmail.com

MAIL_PORT=587

MAIL_USERNAME=xyz@gmail.com

MAIL_PASSWORD=123456

MAIL_ENCRYPTION=tls

Step 6: Add Configuration of Queue

In this step, we will make configuration on queue driver so first of all we will set queue drive "database" in .env file:

.env

QUEUE_CONNECTION=database

After that we need to generate migration and create tables for queue. So let's run bellow command for queue database tables:

Generate Migration:

php artisan queue:table

Run Migration:

php artisan migrate

Step 7: Add Queue Job

now we will create queue job bey following command, this command will create queue job file with Queueable. So let's run bellow command:

php artisan make:job SendEmailJob

now you have the SendEmailJob.php file in the "Jobs" directory. So let's see that file and put the below code on that file.

app/Jobs/SendEmailJob.php

<?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\TestEmailSend;

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 TestEmailSend();

Mail::to($this->details['email'])->send($email);

}

}

Step 8: Add Route & Test Queue job

Now the time is used and the test created queue job, so let's simple create a route with the following code for a testing created queue.

routes/web.php

<?php

/*

|--------------------------------------------------------------------------

| Web Routes

|--------------------------------------------------------------------------

|

| Here is where you can register web routes for your application. These

| routes are loaded by the RouteServiceProvider within a group which

| contains the "web" middleware group. Now create something great!

|

*/

Route::get('email-test', function(){

$details['email'] = 'your_email@gmail.com';

dispatch(new \App\Jobs\SendEmailJob($details));

dd('Send Email Successfully');

});

Ok route is defined, you can watch your queue process using laravel queue command, so let's run bellow command:

php artisan queue:listen

You will see layout as like bellow if queue is works:

You can also clear config cache using bellow command:

php artisan config:clear

Run Laravel App:

All steps have been done, now you have to type the given command and hit enter to run the laravel app:

php artisan serve

Now, you have to open web browser, type the given URL and view the app output:

http://localhost:8000/email-test

It will help you...

#Laravel 9