Laravel 10 Send Email/Mail using Queue Tutorial

If you want to send mail from your Laravel web application. And your emails are in thousands and millions. Then you cannot send so many emails at once. For this, you have to use Laravel queue job. You can easily email thousands and millions of people using Laravel’s queue job.

In this tutorial, you will learn how to send emails or mails to thousands and millions of people using a queue job in Laravel 10 app with smtp drivers like Mailgun, Postmark, Amazon SES, office365, Gmail, and Sendmail.

A mail queue is a directory that stores data and control files for mail messages that the sendmail command delivers. If so, the mail messages must be stored temporarily. If a remote host does not respond to a request for a mail connection, the mail system queues the message and tries again later.

Laravel 10 Send Email/Mail using Queue Tutorial

By using the following steps, You can easily email thousands and millions of people using Laravel’s queue job:

  • Step 1 – Create New Laravel 10 Project
  • Step 2 – Setup Database with Laravel App
  • Step 3 – Creating a Mailable Class
  • Step 4 – Make Email Send Routes
  • Step 5 – Create Directory And Mail Blade View
  • Step 6 – Configure Mail Queue Job
  • Step 7 – Create a Queue Job For Sending Mail
  • Step 8 – Start Development Server

Step 1 – Create New Laravel 10 Project

Firstly, Open your terminal or command prompt.

Then execute the following command into it to install or download Laravel 10 application:

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

Step 2 – Setup Database with Laravel App

Once you have installed laravel 10 applications into your system.

Then you need to configure SMTP and database details in the .env file. So, visit your Laravel app root directory and open the .env file and configure the details; as follows:

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 emails.

Step 3 – Creating a Mailable Class

In this step, you need to create a mailable class for sending emails in Laravel apps.

So, execute the following command on the terminal or command prompt to create NotifyMail mailable class:

php artisan make:mail NotifyMail

After that, visit app/mail directory and open the NotifyMail.php file. Then add the following code to 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('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 the build class of the above-created NotifyMail class.

    return $this->view('view.name');
    to
    return $this->view('emails.demoMail');

Step 4 – Make Email Send Routes

In this step, open /web.php, so navigate to the routes directory.

And then add the following routes for sending emails:

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 the resources/views directory. and create a demoMail.blade.php blade view file inside the resources/views/emails directory.

Then update the following code into it:

<!DOCTYPE html>
<html>
<head>
 <title>Laravel 10 Send Email Example</title>
</head>
<body>

 <h1>This is test mail from Tutsmake.com</h1>
 <p>Laravel 10 send email example</p>

</body>
</html> 

Step 6 – Configure Mail Queue Job

In this step, configure the mail queue job driver. So open .env file and define the database queue driver on the “.env” file like the following:

QUEUE_CONNECTION=database

Then open the terminal or command prompt and execute the following command for queue database tables:

php artisan queue:table

Next, execute migrate table command on the terminal to create queue job-related tables into database:

php artisan migrate

Step 7 – Create a Queue Job For Sending Mail

In this step. create a queue job using the following command:

php artisan make:job SendEmailJob

Then open the SendEmailJob.php file, which is located in “app/Jobs” directory.

Then add 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 – Start Development Server

In the last step, Execute the following command on the terminal or command prompt 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 10 example; In this tutorial, you have learned how to create a mailable class in Laravel 10. And using this class on how to send emails in Laravel 10.

Recommended Laravel Posts

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

AuthorDevendra Dode

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 *