Sitemap

Learn Queue and jobs in Laravel 12

4 min readJul 25, 2025

--

Press enter or click to view image in full size

📨 Introduction — Why Use Queue & Jobs in Laravel?

Sometimes, we need to do time-consuming tasks like sending bulk emails, processing images, or handling API calls. If we do them directly, the user will have to wait — and that’s not a good experience.

That’s where Laravel Queues & Jobs help us.

Queues allow us to delay or run tasks in the background, so our app stays fast and responsive.

👉 Example use case:
Let’s say we want to send a welcome email to 1,000 users. Instead of sending all emails at once (which takes time), we’ll push each email to a queue, and a worker will process them one by one in the background.

This keeps the UI fast and doesn’t block the server.

In this guide, we’ll show how to use Laravel 12 Jobs & Queues to send bulk emails efficiently.

🛠️ Step 1 — Create New Laravel Project, Set Up Database & Mail

Let’s start by creating a fresh Laravel 12 project.

composer create-project laravel/laravel laravel-queue-example

Now open your .env file and set up your database connection:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name // change your db name
DB_USERNAME=root
DB_PASSWORD=

Next, we’ll use Mailtrap to test emails.
Go to mailtrap.io, create a free account, and copy your SMTP credentials.

Now, update the mail section in .env:

MAIL_MAILER=smtp
MAIL_HOST=sandbox.smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=your_mailtrap_username
MAIL_PASSWORD=your_mailtrap_password
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=example@mail.com
MAIL_FROM_NAME="Laravel App"

👥 Step 2 — Add Dummy Users Using Tinker

Now let’s create some fake users to test sending bulk emails.

Laravel has a built-in tool called Tinker that lets us run PHP code right in the terminal.

Run this command:

php artisan tinker

Then inside Tinker, run:

\App\Models\User::factory()->count(20)->create();

✅ This will insert 20 fake users into your users table using Laravel’s model factory.

Once done, you can exit Tinker by typing:

exit

Now we have sample users ready to send emails to!

✉️ Step 3 — Create Mail Class & Email View

Now let’s create the mail file that will be used to send emails to users.

Run this command:

php artisan make:mail SendEmail

This will create a file at: app/Mail/SendEmail.php

Get Maitrik Thakkar’s stories in your inbox

Join Medium for free to get updates from this writer.

Next, go to your SendEmail.php file and update it like this:

SendEmail.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 SendEmail extends Mailable
{
use Queueable, SerializesModels;

public $data;

/**
* Create a new message instance.
*/
public function __construct($data)
{
$this->data = $data;
}

/**
* Get the message envelope.
*/
public function envelope(): Envelope
{
return new Envelope(
subject: 'Send Email',
);
}

/**
* Get the message content definition.
*/
public function content(): Content
{
return new Content(
view: 'emails.send-email',
);
}

/**
* Get the attachments for the message.
*
* @return array<int, \Illuminate\Mail\Mailables\Attachment>
*/
public function attachments(): array
{
return [];
}
}

Now create a view file that will be the body of the email.

Create a Blade file:
resources/views/emails/send-email.blade.php

And add this simple content:

<!DOCTYPE html>
<html>
<head>
<title>Email from Laravel 12</title>
</head>
<body>
<h2>Hello, {{ $data['name'] }}!</h2>
<p>{{ $data['message'] }}</p>
<p>Thank you!</p>
</body>
</html>

âś… Done! Now we have our mail class and the email content ready.

🧱 Step 4— Create a Job to Handle Email Sending

Let’s move the email logic to a Job so it runs in the background.

Run this command to create a job:

php artisan make:job SendUserEmailJob

It will create a file at: app/Jobs/SendUserEmailJob.php

Now open the file and update it like this:

<?php

namespace App\Jobs;

use App\Mail\SendEmail;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Support\Facades\Mail;

class SendUserEmailJob implements ShouldQueue
{
use Queueable;

public $user;

/**
* Create a new job instance.
*/
public function __construct($user)
{
$this->user = $user;
}

/**
* Execute the job.
*/
public function handle(): void
{
$data = [
'name' => $this->user->name,
'message' => 'This is a test email from Laravel 12.'
];

Mail::to($this->user->email)->send(new SendEmail($data));
}
}

🔗 Step 5— Create a Route and Controller

Now let’s create a controller to trigger the email sending process.

Run this command:

php artisan make:controller EmailController

This will create: app/Http/Controllers/EmailController.php

Inside the controller, add this code:

<?php

namespace App\Http\Controllers;

use App\Jobs\SendUserEmailJob;
use App\Mail\SendEmail;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;

class EmailController extends Controller
{
public function sendEmail()
{
$users = User::limit(25)->get();

foreach ($users as $user) {
dispatch(new SendUserEmailJob($user));
}

return response()->json(['success' => 'Email sent successfully.']);
}
}

Now create a route in routes/web.php:

<?php

use App\Http\Controllers\EmailController;
use Illuminate\Support\Facades\Route;

Route::get('/', function () {
return view('welcome');
});

Route::get('/send-email', [EmailController::class, 'sendEmail']);

âś… Conclusion

And that’s it! 🎉

You’ve just learned how to use Queues and Jobs in Laravel 12 to send bulk emails in the background — without slowing down your app.

Instead of sending emails directly (which takes time), we pushed each email task to a queue, and a worker handled them one by one behind the scenes.

This is super useful for:

  • Sending large batches of emails
  • Uploading files
  • Running heavy tasks without blocking users

I hope it helps you…

--

--