php 110 lines · 4 tabs

Dispatch a Queued Welcome Email via Laravel Event Listeners on Registration

Shared by codesnips Aug 2026
4 tabs
<?php

namespace App\Http\Controllers\Auth;

use App\Events\UserRegistered;
use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

class RegisteredController extends Controller
{
    public function store(Request $request)
    {
        $data = $request->validate([
            'name' => ['required', 'string', 'max:255'],
            'email' => ['required', 'email', 'unique:users,email'],
            'password' => ['required', 'string', 'min:8', 'confirmed'],
        ]);

        $user = User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
        ]);

        event(new UserRegistered($user));

        return response()->json([
            'message' => 'Registration successful.',
            'user' => $user->only('id', 'name', 'email'),
        ], 201);
    }
}
4 files · php Explain with highlit

This snippet shows the idiomatic Laravel way to send a welcome email after a user registers without blocking the HTTP request: fire a domain event and let a queued listener do the slow work. The pattern decouples the fact that a user registered from the side effects of registration, so the controller stays thin and new reactions (analytics, Slack pings, provisioning) can be added later without touching registration logic.

In RegisteredController, the request is validated and the User is created in a single create call. The controller does not touch the mailer at all — it simply calls event(new UserRegistered($user)) and returns. This keeps the response fast because no SMTP round-trip happens inline, and it means the registration flow has exactly one responsibility.

UserRegistered event is a plain data-carrying class. It uses the Dispatchable and SerializesModels traits: SerializesModels is what makes the event safe to push onto a queue, because it stores only the model's identifier and re-fetches a fresh User when the job runs, avoiding stale or bloated serialized state. The event exposes a public $user so listeners can read it.

SendWelcomeEmail listener is where the interesting part lives. By implementing ShouldQueue, the listener is automatically pushed onto the queue instead of running synchronously — the framework serializes the event and hands it to a worker. The $connection, $queue, and $tries properties tune where and how reliably it runs, and backoff() spaces out retries so a flaky mail provider gets breathing room. The handle() method builds and sends a WelcomeMail mailable to the user. failed() runs only after all retries are exhausted, giving a single place to log the permanent failure.

EventServiceProvider wires the event to its listener through the $listen map, which is how Laravel knows which listeners to invoke for a given event; multiple listeners can be attached to the same event here.

The key trade-off is eventual consistency: the email is sent slightly after the response, and a worker must be running (php artisan queue:work) for delivery to happen. The payoff is a snappy request, automatic retries with backoff, and a registration path that is trivial to extend.


Related snips

Share this code

Here's the card — post it anywhere.

Dispatch a Queued Welcome Email via Laravel Event Listeners on Registration — share card
Link copied