$ lexprog.com

// notes from an old coder -- php, databases, and the occasional rant

[May 28, 2024] Laravel

Laravel Mail: Sending Emails Like a Pro

Laravel Mail: Sending Emails Like a Pro

────────────────────────────────────────────────────────

Laravel Mail: Sending Emails Like a Pro

Tip: Use Markdown Mailables

php artisan make:mail OrderShipped --markdown=emails.orders.shipped

Built-in components: @component('mail::message'), @component('mail::button').

Gotcha: Mailables Queue Automatically with ShouldQueue

The build() method runs in the queue worker, not the request. Don't use request-specific data.

Tip: Preview Mailables in Browser

Route::get('/mailable', function () {
    return new OrderShipped(Order::find(1));
});

No need to send actual emails during development.

Gotcha: to() vs cc() vs bcc()

BCC recipients don't see each other. CC recipients see all CC addresses.

Tip: Raw String Emails

Mail::raw('Hello world', function ($message) {
    $message->to('user@example.com')->subject('Test');
});

No mailable class needed for simple text emails.

Gotcha: Mail Fake in Tests

Mail::fake();
Mail::assertSent(OrderShipped::class);

Mail::fake() prevents actual sending. Great for tests.

Tip: Use route:cache Carefully

php artisan route:cache is fast, but it doesn't work with closure-based routes. Every time you cache routes, Laravel serializes them. If you have Route::redirect() or closure callbacks, the cache breaks. Stick to controller-based routes in production.

Tip: Model APP_KEY Rotation

Rotating APP_KEY invalidates all encrypted data — cookies, encrypted DB columns, and password reset tokens. If you must rotate (e.g., after a leak), plan a migration that re-encrypts existing data with the new key.

Gotcha: Local Scope Leaks

Global scopes defined in booted() apply to ALL queries on that model — including relationships. An innocent User::all() in admin panel might exclude soft-deleted users if a global scope is active.

Senior Insight

Email in Laravel is deceptively complex. The issue I've debugged most often is MIME encoding breaking with special characters in subject lines — Laravel handles UTF-8 well, but some email clients don't. Always send test emails to multiple clients (Gmail, Outlook, Apple Mail) before your launch. Also, never send transactional emails synchronously in the request cycle; queue them, and set up a dedicated queue worker for mail to avoid head-of-line blocking.

Source: Laravel News (https://laravel-news.com/), Freek.dev (https://freek.dev/tags/laravel), Spatie Blog (https://spatie.be/blog)

────────────────────────────────────────────────────────
<-- back to posts