symfony/mailer
Symfony Mailer helps you send emails via SMTP and other transports with a clean API. Build Email/TemplatedEmail messages, add attachments and headers, and integrate with Twig templates for HTML rendering. Configure transports via DSN and send reliably.
symfony/mailer package is a standalone, dependency-light component that integrates seamlessly with Laravel’s existing email stack (e.g., SwiftMailer via Laravel’s Mail facade). It avoids tight coupling with Symfony’s full framework, making it ideal for Laravel’s ecosystem.Transport::fromDsn() method simplifies configuration, mirroring Laravel’s .env-based approach.EventDispatcher for pre/post-send hooks (e.g., logging, templating), which can be adapted to Laravel’s service container and events system (e.g., Illuminate\Events\Dispatcher).TemplatedEmail and BodyRenderer complements Laravel’s Blade/Twig hybrid approach, though Blade would require a custom adapter.Mail facade already uses SwiftMailer under the hood, and symfony/mailer is designed to be SwiftMailer-compatible. The Email and TemplatedEmail classes map directly to Laravel’s Mailable classes.// Current Laravel (SwiftMailer)
Mail::to('[email protected]')->send(new OrderShipped($order));
// Future (Symfony Mailer)
$email = (new TemplatedEmail())
->to('[email protected]')
->htmlTemplate('emails.orders.shipped')
->context(['order' => $order]);
$mailer->send($email);
.env (e.g., MAIL_MAILER=smtp) can be translated to Symfony’s DSN format (e.g., smtp://user:[email protected]:587), reducing boilerplate.ServiceProvider to register the Mailer instance as a singleton, replacing or extending the existing MailManager.BodyRenderer adapter for Blade (not native Twig). Effort: Medium (1–2 days for a proof-of-concept).EventDispatcher (e.g., dispatching MessageSentEvent to Laravel’s MailSent event).Mailable unit tests) may need updates to use symfony/mailer's Email/TemplatedEmail classes.RoundRobinTransport.Mailable classes to TemplatedEmail? Can a traits-based adapter reduce refactoring?Debugbar or Log channels?Mail::later()) align with Symfony’s AsyncTransport or RoundRobinTransport?Mail facade and Mailable classes. The symfony/mailer package is a drop-in alternative for the email layer.Mailer instance as a singleton in Laravel’s container:
$app->singleton(Mailer::class, fn($app) => new Mailer(
Transport::fromDsn(env('MAILER_DSN')),
null,
$app->make(EventDispatcher::class)
));
EventDispatcher to Laravel’s events:
// In a service provider
$eventDispatcher->addListener(MessageSentEvent::class, fn($event) =>
event(new MailSent($event->getMessage()))
);
BladeBodyRenderer:
use Illuminate\View\Factory as Blade;
class BladeBodyRenderer implements BodyRendererInterface {
public function __construct(private Blade $blade) {}
public function render(string $template, array $context): string {
return $this->blade->make($template, $context)->render();
}
}
Phase 1: Parallel Testing (Low Risk)
symfony/mailer as a dev dependency.symfony/mailer while keeping SwiftMailer as fallback.Phase 2: Core Integration (Medium Risk)
Mail facade with a custom facade wrapping symfony/mailer:
// app/Facades/CustomMail.php
public static function send(MailableContract $mailable) {
$email = (new TemplatedEmail())
->to($mailable->recipients())
->htmlTemplate($mailable->template())
->context($mailable->context());
app(Mailer::class)->send($email);
}
Mailable classes to extend TemplatedEmail or use a trait for backward compatibility.Phase 3: Full Migration (High Risk)
composer.json.Mailable classes to use symfony/mailer's Email/TemplatedEmail.symfony/mailer instead of SwiftMailer.| Feature | Laravel Native | Symfony Mailer | Notes |
|---|---|---|---|
| SMTP/DSN Config | ✅ .env |
✅ DSN | DSN can mirror Laravel’s .env vars. |
| Templating | ✅ Blade | ✅ Twig | Requires custom Blade adapter. |
| Async Queues | ✅ Mail::later() |
✅ AsyncTransport |
Align with Laravel’s queue system. |
| Event Hooks | ✅ MailSent |
✅ MessageSentEvent |
Bridge events via service provider. |
| Testing | ✅ Mailable tests |
✅ Mock Mailer |
Update test classes. |
| Attachments | ✅ | ✅ | Identical API. |
Mailable classes in order of complexity (simple templates first).Email/TemplatedEmail classes encapsulate common email logic (headers, attachments), reducing duplicate code in Mailable classes..env management compared to SwiftMailer’s multi-method setup.symfony/mailer to the dependency tree, increasing attack surface (though MIT-licensed and widely used).BodyRenderer requires ongoing maintenance if Blade syntax evolves.How can I help you explore Laravel packages today?