In this tutorial, we will learn how to send emails using queues in Laravel 10. Queues allow you to defer the processing of a time-consuming task, such as sending an email, until a later time. This allows you to speed up web requests to your application.
Prerequisites
Before starting this tutorial, you should have a basic understanding of the following:
• PHP
• Laravel
• Queues
Step 1 – Install Laravel
First, we need to install the latest version of Laravel. To do this, open a terminal window and run the following command:
composer create-project –prefer-dist laravel/laravel laravel-10-email-queue
This will create a new Laravel project in the laravel-10-email-queue directory.
Step 2 – Configure Queue Driver
Next, we need to configure the queue driver. Open the .env file and set the QUEUE_DRIVER variable to database. This will tell Laravel to use the database as the queue driver.
QUEUE_DRIVER=database
Step 3 – Create Migration
Next, we need to create a migration to create the jobs table. To do this, run the following command:
php artisan make:migration create_jobs_table
This will create a new migration file in the database/migrations directory. Open the file and add the following code:
public function up()
{
Schema::create(‘jobs’, function (Blueprint $table) {
$table->bigIncrements(‘id’);
$table->string(‘queue’)->index();
$table->longText(‘payload’);
$table->unsignedTinyInteger(‘attempts’);
$table->unsignedInteger(‘reserved_at’)->nullable();
$table->unsignedInteger(‘available_at’);
$table->unsignedInteger(‘created_at’);
});
}
public function down()
{
Schema::dropIfExists(‘jobs’);
}
This will create a jobs table with the necessary columns.
Step 4 – Run Migration
Next, we need to run the migration. To do this, run the following command:
php artisan migrate
This will create the jobs table in the database.
Step 5 – Create Job
Next, we need to create a job to send the email. To do this, run the following command:
php artisan make:job SendEmailJob
This will create a new job class in the app/Jobs directory. Open the file and add the following code:
public function handle()
{
Mail::to(‘[email protected]’)->send(new EmailNotification());
}
This will send an email to the specified address using the EmailNotification class.
Step 6 – Dispatch Job
Next, we need to dispatch the job. To do this, open the routes/web.php file and add the following code:
Route::get(‘/send-email’, function () {
dispatch(new App\Jobs\SendEmailJob());
return ‘Email Sent!’;
});
This will dispatch the job when the /send-email route is accessed.
Step 7 – Run Queue Worker
Finally, we need to run the queue worker. To do this, open a terminal window and run the following command:
php artisan queue:work
This will start the queue worker and process any jobs that are added to the queue.
Conclusion
In this tutorial, we have learned how to send emails using queues in Laravel 10. Queues allow you to defer the processing of a time-consuming task, such as sending an email, until a later time. This allows you to speed up web requests to your application.
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.