Laravel 10, By using a queue, the email sending process is offloaded to a background task, allowing your application to respond quickly to user requests. This can significantly improve the performance and responsiveness of your application.
When sending emails synchronously, your application has to wait for the email to be sent before continuing with other tasks. With a queue, emails are sent asynchronously, meaning your application can continue processing other tasks while the email is being sent in the background.
Step 1: Install Laravel App
composer create-project laravel/laravel example-QueueEmail
Step 2: Create Mail Class with Configuration
app/Mail/SendEmailTest.php
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;
class SendEmailTest extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*/
public function __construct()
{
}
/**
* Get the message envelope.
*/
public function envelope(): Envelope
{
return new Envelope(
subject: 'Mail from webthestuff.com',
);
}
/**
* Get the message content definition.
*/
public function content(): Content
{
return new Content(
view: 'emails.test',
);
}
/**
* Get the attachments for the message.
*
* @return array
*/
public function attachments(): array
{
return [];
}
}
resources/views/emails/test.blade.php
<!DOCTYPE html>
<html>
<head>
<title>How to send mail using queue in Laravel 10? - webthestuff.com</title>
</head>
<body>
<center>
<h2 style="padding: 23px;background: #b3deb8a1;border-bottom: 6px green solid;">
<a href="https://webthestuff.com">Visit Our Website : webthestuff.com</a>
</h2>
</center>
<p>Hi, Sir</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<strong>Thank you Sir. :)</strong>
</body>
</html>
.env
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=mygoogle@gmail.com
MAIL_PASSWORD=rrnnucvnqlbsl
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=mygoogle@gmail.com
MAIL_FROM_NAME="${APP_NAME}"
Step 3: Queue Configuration
.env
QUEUE_CONNECTION=database
let's run bellow command for queue database tables:
Generate Migration:
php artisan queue:table
Run Migration:
php artisan migrate
Step 4: Create Queue Job
So let's run bellow command:
php artisan make:job SendEmailJob
app/Jobs/SendEmailJob.php
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
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.
*/
public function __construct($details)
{
$this->details = $details;
}
/**
* Execute the job.
*/
public function handle(): void
{
$email = new SendEmailTest();
Mail::to($this->details['email'])->send($email);
}
}
Step 5: Test Queue Job
routes/web.php
Route::get('email-test', function(){
$details['email'] = 'your_email@gmail.com';
dispatch(new App\Jobs\SendEmailJob($details));
dd('done');
});
Run following command to see queue process,
php artisan queue:work
Run Laravel App:
php artisan serve
Now, Go to web browser, type the given URL and see the output:
http://localhost:8000/email-test