laravel-mail
When to use
Use this skill when building email functionality:
- Mailable classes with HTML/Blade or Markdown templates
- Queued email sending
- Attachments and inline images
- Mail testing and previewing
For simple notification emails (one-off messages), see laravel-notifications.
Use Mailables when you need full control over the email template.
Procedure: Create a Mailable
- Inspect existing mailables — Review
app/Mail/ for naming, base class, queueing convention, and the templates in resources/views/emails/ for the project's markdown style.
- Generate class —
php artisan make:mail InvoiceMail --markdown=emails.invoice.
- Configure — Set subject, from, attachments, queuing (
ShouldQueue).
- Create template — Markdown template in
resources/views/emails/.
- Verify — Send test email, confirm rendering and delivery.
Example
php artisan make:mail InvoiceMail --markdown=emails.invoice
declare(strict_types=1);
namespace App\Mail;
use App\Models\Invoice;
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 InvoiceMail extends Mailable implements ShouldQueue
{
use Queueable;
use SerializesModels;
public function __construct(
private readonly Invoice $invoice,
) {}
public function envelope(): Envelope
{
return new Envelope(
subject: 'Invoice #' . $this->invoice->getNumber(),
replyTo: ['billing@example.com'],
);
}
public function content(): Content
{
return new Content(
markdown: 'emails.invoice',
with: [
'invoice' => $this->invoice,
'url' => route('invoices.show', $this->invoice->getId()),
],
);
}
/** @return array<int, \Illuminate\Mail\Mailables\Attachment> */
public function attachments(): array
{
return [
Attachment::fromPath('/path/to/invoice.pdf')
->as('invoice-' . $this->invoice->getNumber() . '.pdf')
->withMime('application/pdf'),
];
}
}
Markdown templates
{{-- resources/views/emails/invoice.blade.php --}}
<x-mail::message>
# Invoice {{ $invoice->getNumber() }}
Thank you for your order. Here is your invoice summary:
<x-mail::table>
| Item | Amount |
|:-----|-------:|
@foreach ($invoice->getItems() as $item)
| {{ $item->getName() }} | {{ $item->getFormattedAmount() }} |
@endforeach
| **Total** | **{{ $invoice->getFormattedTotal() }}** |
</x-mail::table>
<x-mail::button :url="$url">
View Invoice
</x-mail::button>
Thanks,<br>
{{ config('app.name') }}
</x-mail::message>
Sending mail
// Send immediately
Mail::to($user)->send(new InvoiceMail($invoice));
// Queue for background sending (preferred)
Mail::to($user)->queue(new InvoiceMail($invoice));
// Send later
Mail::to($user)->later(now()->addMinutes(10), new InvoiceMail($invoice));
// Multiple recipients
Mail::to($users)
->cc($manager)
->bcc('archive@example.com')
->send(new InvoiceMail($invoice));
Testing
// Assert mail was sent
Mail::fake();
// ... trigger action ...
Mail::assertSent(InvoiceMail::class, function (InvoiceMail $mail) use ($user) {
return $mail->hasTo($user->getEmail());
});
Mail::assertNotSent(InvoiceMail::class);
Mail::assertNothingSent();
Mail::assertQueued(InvoiceMail::class);
Previewing in browser
// routes/web.php (local only)
Route::get('/mail-preview', function () {
$invoice = Invoice::factory()->create();
return new InvoiceMail($invoice);
});
Surviving the mail client
An email is not a web page. Mail clients strip, rewrite, and ignore CSS that
every browser honors, so a template that renders correctly in
/mail-preview tells you nothing about the inbox. Markdown templates (above)
give you a tested baseline for free — this section is what to hold to when a
design forces you off them.
Four requirements, in order of what breaks first:
- Table-based layout, not flexbox or grid. Use nested
<table> elements
with role="presentation", a fixed outer width of 600px, and cellpadding
/ cellspacing / border set to 0. display: flex and
display: grid are unsupported or partially supported in the Windows
Outlook family and collapse to a single stacked column.
- Inline styles, not a
<style> block. Write style="…" on the element.
A <head><style> block is stripped outright by some webmail clients, and
class selectors then match nothing. Use a CSS inliner at build time if the
template is authored with classes — never ship the classes unresolved. Keep
a <style> block only for what cannot be inlined (media queries), and treat
everything in it as optional.
- No web fonts, no background images, no external JS. Declare a font stack
ending in a system fallback; a remote font silently degrades. Background
images require the
v:fill VML fallback in Outlook, so put the color on
bgcolor and treat the image as decoration.
- Explicit width and alt text on every image, and no image-only content.
Images are blocked by default in several clients, so an email whose call to
action is an image is an email with no call to action.
The client list worth testing, and what breaks in each:
| Client |
What breaks |
| Outlook 2016-2019 / Windows (Word engine) |
flex, grid, float, max-width, border-radius, background images, padding on <div>; the strictest target — if it renders, most others do |
| Outlook.com / Outlook 365 web |
strips <style> blocks in some views; rewrites class attributes; ignores margin on several elements |
| Gmail web |
clips the message past ~102 KB of HTML with a "view entire message" link — anything below the clip, including the unsubscribe link, is not seen; strips <style> when the message is clipped |
| Gmail app (iOS / Android) |
no support for embedded <style> on non-Gmail accounts; media queries ignored there, so the mobile layout must be the fluid default |
| Apple Mail / iOS Mail |
the most permissive; auto-scales small text and auto-links dates and addresses unless suppressed — a false green if it is the only client you check |
| Dark mode (Apple Mail, Outlook, Gmail) |
colors are force-inverted; a logo on a hardcoded white background becomes a white box on dark, and #000 text on a transparent background becomes invisible |
Verify against a real client, not a preview route. /mail-preview renders in
a browser and proves none of the above. Send to real accounts, or use a
rendering service, before the template ships.
Core rules
- Always queue emails — implement
ShouldQueue to avoid blocking requests.
- Use Markdown templates for consistent styling across email clients.
- Use Envelope + Content pattern (Laravel 11+) — not the old
build() method.
- Test with
Mail::fake() — verify recipients, content, and queuing.
- Keep Mailables focused — one Mailable per email type.
Output format
- Mailable class with envelope, content, and attachments
- Blade/Markdown email template
- Queued mail dispatch integration
Auto-trigger keywords
- Mailable
- email template
- send mail
- Mail::to
- markdown email
- mail attachment
Gotcha
- Always queue emails (
ShouldQueue) — synchronous sending blocks the request.
- The model forgets that mail templates are Blade files — they need to be published/created.
- Don't test email content with
Mail::fake() alone — it doesn't render the template. Use Mail::assertSent() with closure.
Do NOT
- Do NOT send emails synchronously in request lifecycle — always queue.
- Do NOT use
build() method — use envelope(), content(), attachments().
- Do NOT hardcode email addresses — use config or environment variables.
- Do NOT put HTML in Mailable classes — use Blade templates.
1---2name: laravel-mail3description: Use when building Laravel emails — Mailables, Markdown templates, queued sending, attachments, previews — even when the user says 'send this as an email' without naming Mailables.4---56# laravel-mail78## When to use910Use this skill when building email functionality:11- Mailable classes with HTML/Blade or Markdown templates12- Queued email sending13- Attachments and inline images14- Mail testing and previewing1516For **simple notification emails** (one-off messages), see [laravel-notifications](../laravel-notifications/SKILL.md).17Use Mailables when you need full control over the email template.1819## Procedure: Create a Mailable20211. **Inspect existing mailables** — Review `app/Mail/` for naming, base class, queueing convention, and the templates in `resources/views/emails/` for the project's markdown style.222. **Generate class** — `php artisan make:mail InvoiceMail --markdown=emails.invoice`.233. **Configure** — Set subject, from, attachments, queuing (`ShouldQueue`).244. **Create template** — Markdown template in `resources/views/emails/`.255. **Verify** — Send test email, confirm rendering and delivery.2627### Example2829```bash30php artisan make:mail InvoiceMail --markdown=emails.invoice31```3233```php34declare(strict_types=1);3536namespace App\Mail;3738use App\Models\Invoice;39use Illuminate\Bus\Queueable;40use Illuminate\Contracts\Queue\ShouldQueue;41use Illuminate\Mail\Mailable;42use Illuminate\Mail\Mailables\Content;43use Illuminate\Mail\Mailables\Envelope;44use Illuminate\Queue\SerializesModels;4546class InvoiceMail extends Mailable implements ShouldQueue47{48 use Queueable;49 use SerializesModels;5051 public function __construct(52 private readonly Invoice $invoice,53 ) {}5455 public function envelope(): Envelope56 {57 return new Envelope(58 subject: 'Invoice #' . $this->invoice->getNumber(),59 replyTo: ['billing@example.com'],60 );61 }6263 public function content(): Content64 {65 return new Content(66 markdown: 'emails.invoice',67 with: [68 'invoice' => $this->invoice,69 'url' => route('invoices.show', $this->invoice->getId()),70 ],71 );72 }7374 /** @return array<int, \Illuminate\Mail\Mailables\Attachment> */75 public function attachments(): array76 {77 return [78 Attachment::fromPath('/path/to/invoice.pdf')79 ->as('invoice-' . $this->invoice->getNumber() . '.pdf')80 ->withMime('application/pdf'),81 ];82 }83}84```8586## Markdown templates8788```blade89{{-- resources/views/emails/invoice.blade.php --}}90<x-mail::message>91# Invoice {{ $invoice->getNumber() }}9293Thank you for your order. Here is your invoice summary:9495<x-mail::table>96| Item | Amount |97|:-----|-------:|98@foreach ($invoice->getItems() as $item)99| {{ $item->getName() }} | {{ $item->getFormattedAmount() }} |100@endforeach101| **Total** | **{{ $invoice->getFormattedTotal() }}** |102</x-mail::table>103104<x-mail::button :url="$url">105View Invoice106</x-mail::button>107108Thanks,<br>109{{ config('app.name') }}110</x-mail::message>111```112113## Sending mail114115```php116// Send immediately117Mail::to($user)->send(new InvoiceMail($invoice));118119// Queue for background sending (preferred)120Mail::to($user)->queue(new InvoiceMail($invoice));121122// Send later123Mail::to($user)->later(now()->addMinutes(10), new InvoiceMail($invoice));124125// Multiple recipients126Mail::to($users)127 ->cc($manager)128 ->bcc('archive@example.com')129 ->send(new InvoiceMail($invoice));130```131132## Testing133134```php135// Assert mail was sent136Mail::fake();137138// ... trigger action ...139140Mail::assertSent(InvoiceMail::class, function (InvoiceMail $mail) use ($user) {141 return $mail->hasTo($user->getEmail());142});143144Mail::assertNotSent(InvoiceMail::class);145Mail::assertNothingSent();146Mail::assertQueued(InvoiceMail::class);147```148149## Previewing in browser150151```php152// routes/web.php (local only)153Route::get('/mail-preview', function () {154 $invoice = Invoice::factory()->create();155 return new InvoiceMail($invoice);156});157```158159## Surviving the mail client160161An email is not a web page. Mail clients strip, rewrite, and ignore CSS that162every browser honors, so a template that renders correctly in163`/mail-preview` tells you nothing about the inbox. Markdown templates (above)164give you a tested baseline for free — this section is what to hold to when a165design forces you off them.166167**Four requirements, in order of what breaks first:**1681691. **Table-based layout, not flexbox or grid.** Use nested `<table>` elements170 with `role="presentation"`, a fixed outer width of 600px, and `cellpadding`171 / `cellspacing` / `border` set to `0`. `display: flex` and172 `display: grid` are unsupported or partially supported in the Windows173 Outlook family and collapse to a single stacked column.1742. **Inline styles, not a `<style>` block.** Write `style="…"` on the element.175 A `<head><style>` block is stripped outright by some webmail clients, and176 class selectors then match nothing. Use a CSS inliner at build time if the177 template is authored with classes — never ship the classes unresolved. Keep178 a `<style>` block only for what cannot be inlined (media queries), and treat179 everything in it as optional.1803. **No web fonts, no background images, no external JS.** Declare a font stack181 ending in a system fallback; a remote font silently degrades. Background182 images require the `v:fill` VML fallback in Outlook, so put the color on183 `bgcolor` and treat the image as decoration.1844. **Explicit width and alt text on every image, and no image-only content.**185 Images are blocked by default in several clients, so an email whose call to186 action is an image is an email with no call to action.187188**The client list worth testing, and what breaks in each:**189190| Client | What breaks |191|---|---|192| **Outlook 2016-2019 / Windows (Word engine)** | `flex`, `grid`, `float`, `max-width`, `border-radius`, background images, `padding` on `<div>`; the strictest target — if it renders, most others do |193| **Outlook.com / Outlook 365 web** | strips `<style>` blocks in some views; rewrites `class` attributes; ignores `margin` on several elements |194| **Gmail web** | clips the message past ~102 KB of HTML with a "view entire message" link — anything below the clip, including the unsubscribe link, is not seen; strips `<style>` when the message is clipped |195| **Gmail app (iOS / Android)** | no support for embedded `<style>` on non-Gmail accounts; media queries ignored there, so the mobile layout must be the fluid default |196| **Apple Mail / iOS Mail** | the most permissive; auto-scales small text and auto-links dates and addresses unless suppressed — a false green if it is the only client you check |197| **Dark mode (Apple Mail, Outlook, Gmail)** | colors are force-inverted; a logo on a hardcoded white background becomes a white box on dark, and `#000` text on a transparent background becomes invisible |198199**Verify against a real client, not a preview route.** `/mail-preview` renders in200a browser and proves none of the above. Send to real accounts, or use a201rendering service, before the template ships.202203## Core rules204205- **Always queue emails** — implement `ShouldQueue` to avoid blocking requests.206- **Use Markdown templates** for consistent styling across email clients.207- **Use Envelope + Content** pattern (Laravel 11+) — not the old `build()` method.208- **Test with `Mail::fake()`** — verify recipients, content, and queuing.209- **Keep Mailables focused** — one Mailable per email type.210211## Output format2122131. Mailable class with envelope, content, and attachments2142. Blade/Markdown email template2153. Queued mail dispatch integration216217## Auto-trigger keywords218219- Mailable220- email template221- send mail222- Mail::to223- markdown email224- mail attachment225226## Gotcha227228- Always queue emails (`ShouldQueue`) — synchronous sending blocks the request.229- The model forgets that mail templates are Blade files — they need to be published/created.230- Don't test email content with `Mail::fake()` alone — it doesn't render the template. Use `Mail::assertSent()` with closure.231232## Do NOT233234- Do NOT send emails synchronously in request lifecycle — always queue.235- Do NOT use `build()` method — use `envelope()`, `content()`, `attachments()`.236- Do NOT hardcode email addresses — use config or environment variables.237- Do NOT put HTML in Mailable classes — use Blade templates.