Laravel 8 Send Mail using Queue Tutorial

Laravel 8 Send Mail using Queue Tutorial

Laravel 8 Send mail using queue example; This tutorial will completely guide you on 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 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.

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. So, This laravel 8 send email using queue example tutoiral will create simple and easy queue job with database driver for test email sending.

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

How to Send Mail 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 

Note that:- If you are sending a mail using Gmail you have to allow non-secure apps to access Gmail you can do this by going to your Gmail settings here.

Once less secure apps are enabled; now you can use your 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 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, 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 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

How to send mail using queue in Laravel 8 example; In this tutorial, 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

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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