Laravel 8 Send Mail using Queue Tutorial

Laravel 8 Send Mail using Queue Tutorial

Laravel 8 Send mail using queue example. In this tutorial, we would love to show you how to send mail using queue job in laravel 8 app. And you will learn step by step how to send email in laravel 8 using queue job.

Sometimes, the Mail send process takes some time. And you don’t want to wait to send an email or another process on loading the server-side process. So, you can use the queue job for sending mail in laravel 8 app.

In this example tutoiral, create simple and easy queue job with database driver for test email sending.

Note that, you can use several SMTP drivers details (Mailgun, Postmark, Amazon SES, and sendmail) in .env file for sending email in laravel 8.

Send Email using Queue in Laravel 8

  • 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

In this step, use the following command to install or download laravel 8 application:

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

Step 2 – Configuration SMTP & Database

In this step, you 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 your user name here
MAIL_PASSWORD=Add your password here
MAIL_ENCRYPTION=tls 

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 you will create an email template. That you 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, we will create email template named demoMail.blade.php inside resources/views/emails directory. That’s why we 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 8 Send Email Example</title>
</head>
<body>

 <h1>This is test mail from Tutsmake.com</h1>
 <p>Laravel 8 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 your server locally:

php artisan serve

Then open browser and fire the following URL on it:

http://127.0.0.1:8000/email-test

Conclusion

Laravel 8 send an email example, you have learned how to create a mailable class in laravel 8. And using this class on how to send emails in laravel 8.

Recommended Laravel Posts

If you have any questions or thoughts to share, use the comment form below to reach us.

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Javascript, JQuery, Laravel, Livewire, Codeigniter, Vue JS, Angular JS, React Js, WordPress, and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *