How to Send Emails with Queues in Laravel. how to use queue in laravel

Himmat Kumar Oct 16, 2024, 6:02 PM
Laravel
Views 150
Blog Thumbnail

A Comprehensive Approach to Sending 1000 Emails in Laravel Queues

This tutorial will guide you through the process of sending 1000 emails with the help of queues in Laravel 10. This includes creating a users table, inserting 1000 email addresses, and executing email-sending tasks asynchronously using Laravel queues.

Create mailtrap account free and use Click To view Blog

Step 1: Create a New Laravel Project

composer create-project --prefer-dist laravel/laravel queue-email-demo
        

Step 2: Set Up Database and Mail Configuration

In the .env file, configure your database and mail settings:


DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_db
DB_USERNAME=root
DB_PASSWORD=

MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=your_mailtrap_username
MAIL_PASSWORD=your_mailtrap_password
MAIL_ENCRYPTION=null

        

Step 3: Make a Migration for the Users Table

php artisan make:migration create_users_table
        

Update the migration file to create the users table:


Schema::create('users', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->string('email')->unique();
    $table->timestamps();
});

        

Step 4: Add 1000 Random Users

Insert the following code in DatabaseSeeder.php to seed users:


use App\Models\User;
use Faker\Factory as Faker;

public function run() {
    $faker = Faker::create();
    for ($i = 0; $i < 1000; $i++) {
        User::create([
            'name' => $faker->name,
            'email' => $faker->email,
        ]);
    }
}

        
php artisan db:seed
        

Step 5: Create the Mail Class

php artisan make:mail UserMail
        

Modify the UserMail class:


use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class UserMail extends Mailable {
    use Queueable, SerializesModels;
    public $user;

    public function __construct($user) {
        $this->user = $user;
    }

    public function build() {
        return $this->view('emails.user')->with([
            'userName' => $this->user->name,
        ]);
    }
}

        

Step 6: Create a Job to Queue the Email

php artisan make:job SendEmailJob
        

Update the SendEmailJob class:


use App\Mail\UserMail;
use Illuminate\Bus\Queueable;
use Illuminate\Support\Facades\Mail;

class SendEmailJob implements ShouldQueue {
    use Queueable;

    public $user;

    public function __construct($user) {
        $this->user = $user;
    }

    public function handle() {
        Mail::to($this->user->email)->send(new UserMail($this->user));
    }
}

        

Step 7: Dispatch the Jobs


use App\Jobs\SendEmailJob;
use App\Models\User;

public function sendEmails() {
    $users = User::all();
    foreach ($users as $user) {
        SendEmailJob::dispatch($user);
    }
    return response()->json(['message' => 'Emails are being sent!']);
}

        

Step 8: Execute the Queue Worker

php artisan queue:work
        

Step 9: Create a View for the Email


<!DOCTYPE html>
<html>
<head>
    <title>Email Notification</title>
</head>
<body>
    <h1>Hello, {{ $userName }}</h1>
    <p>This is a test email sent via Laravel Queues!</p>
</body>
</html>

        

Step 10: Test the Process

Route::get('/send-emails', 'App\Http\Controllers\EmailController@sendEmails');
        

Visit https://localhost:8000/send-emails to start sending emails via the queue.

Conclusion

With Laravel queues, you can efficiently send 1000 emails asynchronously, ensuring a seamless user experience. Implement queues in your projects to handle heavy tasks in the background!

Comments

Please login to leave a comment.

No comments yet.

Related Posts

laravel-post-route-and-post-request-post-request-using-postman
201 viewsLaravel
Himmat Kumar Dec 14, 2024, 8:32 AM

laravel post route and post request , post request usin...

get-route-in-laravel
204 viewsLaravel
Himmat Kumar Dec 13, 2024, 11:07 AM

GET Route in Laravel

laravel-framework-overview
233 viewsLaravel
Himmat Kumar Jul 29, 2024, 11:15 AM

Laravel Framework Overview: Features, Benefits, and Res...

laravel-cookies-guide
1096 viewsLaravel
Himmat Regar Jun 1, 2025, 2:23 PM

Laravel Cookies: Secure, Encrypted & Easy (Guide 2025)

laravel-service-container-guide
447 viewsLaravel
Himmat Regar May 31, 2025, 7:23 PM

Mastering Laravel Service Container: Dependency Injecti...

What-is-laravel-controller
310 viewsLaravel
Himmat Kumar Dec 24, 2024, 12:22 AM

What is Laravel - Controllers

laravel-configuration
228 viewsLaravel
Himmat Kumar Dec 4, 2024, 11:58 AM

Laravel Configuration

laravel-application-structure
185 viewsLaravel
Himmat Kumar Dec 3, 2024, 8:33 AM

Laravel Application File Structure

eloquent-relationships-guide
1089 viewsLaravel
Himmat Regar May 31, 2025, 7:32 PM

Mastering Eloquent Relationships in Laravel (2025) — Co...

laravel-setup-tutorial
290 viewsLaravel
Himmat Kumar Jul 30, 2024, 12:03 PM

Master Laravel: Basic System Requirement,Installation a...