Laravel 9 Send Mail Using Queue Example

In this article, we will see you laravel 9 send mail using a queue example, Many times we can see many processes take more time to load like bulk email sending, payment gateway processes, etc. Whenever you are sending an email for verification 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 server-side process then you can use the queue.

In laravel 9 send mail using a queue example we will set up the mailtrap for sending an email. Laravel queues provide a unified queueing API across a variety of different queue backends, such as Amazon SQSRedis, or even a relational database.

So, let's see send mail using a queue in laravel 9.

How To Send Email Using a Queue In Laravel 9

Step 1: Install Laravel 9 Application For Send Mail Using Queue

Step 2: Create Mail Setup

Step 3: Configuration of Queue

Step 4: Create Queue Job

Step 5: Test Queue Job

Step 6: Run Laravel 9 Application

 

Step 1: Install Laravel 9 Application For Send Mail Using Queue

In this step, we will install the laravel 9 application using the composer command.

composer create-project --prefer-dist laravel/laravel laravel_9_mail_queue_example

 

 

Step 2: Create Mail Setup

Now, we will run the below command in the terminal and create the mail.

php artisan make:mail SendEmailQueueDemo

Now, you will find the new Mail folder in the app directory with the SendEmailQueueDemo.php file. So, add the below code to this file.

app/Mail/SendEmailQueueDemo.php

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class SendEmailQueueDemo 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->subject('How To Send E-mail Using Queue In laravel 9')            
            ->view('email.demo');
    }
}

After that, we need to create an email view using a blade file. So, we will create demo.blade.php following the path.

 

 

resources/views/email/demo.blade.php

<!DOCTYPE html>
<html>
<head>
	<title>How To Send Mail Using Queue In Laravel 9 - Techsolutionstuff</title>
</head>
<body>
   
<center>
<h2>
	<a href="https://techsolutionstuff.com">Visit Our Website : Techsolutionstuff</a>
</h2>
</center>
  
<p>Hello,</p>

<p>This is a test mail. This mail send using queue listen in laravel 9.</p>  

<strong>Thanks & Regards.</strong>

</body>
</html>

Now, we will configure of view file, which we have to set up for email send, So let's configuration in the .env file.

MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=your_username
MAIL_PASSWORD=your_password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"

 

Step 3: Configuration of Queue

Now, we are setting up the configuration on the queue driver. So, we will set the queue driver "database". Also, we will define the driver as redis too. So, here we will define the database driver on the .env file.

QUEUE_CONNECTION=database

After that, we need to generate migration and create tables for queues. So, let's run the below command for queue database tables.

php artisan queue:table

Now, run the migration in your terminal.

php artisan migrate

 

 

Step 4: Create Queue Job

In this step, we will create a new queue job. So, run the below command in your terminal.

php artisan make:job SendEmailQueueJob

After running the above command the SendEmailQueueJob.php file is created.

app/Jobs/SendEmailQueueJob.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\SendEmailQueueDemo;
use Mail;

class SendEmailQueueJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    protected $send_mail;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($send_mail)
    {
        $this->send_mail = $send_mail;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $email = new SendEmailQueueDemo();        
        Mail::to($this->send_mail)->send($email);
    }
}

 

 

Step 5: Test Queue Job

Now, we will check the queue job. So, add the below code to the web.php file and dispatch the queue job.

routes/web.php

Route::get('send/email', function(){
  
	$send_mail = '[email protected]';
  
    dispatch(new App\Jobs\SendEmailQueueJob($send_mail));
  
    dd('send mail successfully !!');
});

Now, clear the config cache using the below command for sending mail with a queue in laravel 9.

php artisan config:clear

 

Step 6: Run Laravel 9 Application

Now, run this laravel 9 queue example with the artisan command.

php artisan serve

 


You might also like:

RECOMMENDED POSTS

FEATURE POSTS