Installation Add the package via Composer (though note this is a Symfony bridge, not a standalone Laravel package):
composer require symfony/swiftmailer-bridge
Note: In Laravel, SwiftMailer is typically used via the swiftmailer/swiftmailer package (or its successor, symfony/mailer). This bridge is primarily for Symfony apps, but Laravel can still leverage it for integration with Symfony components like HttpClient or Process.
First Use Case: Sending Emails with Symfony’s Mailer If you’re migrating from SwiftMailer to Symfony’s Mailer (recommended), replace:
// Old SwiftMailer (deprecated)
$mailer = new \Swift_Mailer($transport);
With Symfony’s Mailer (which internally uses SwiftMailer):
// Laravel's built-in Mail facade (already uses Symfony Mailer under the hood)
use Illuminate\Support\Facades\Mail;
Mail::raw('Email content', function ($message) {
$message->to('user@example.com')
->subject('Test Email');
});
Where to Look First
HttpClient), check the SwiftmailerBridge source (though it’s archived).HttpClient for Email Attachments:
Use the bridge to fetch email attachments dynamically via Symfony’s HttpClient:
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
$mailer = new Mailer(new Transport(), new HttpClient());
$email = (new Email())
->from('sender@example.com')
->to('recipient@example.com')
->subject('Attachment Example')
->html('<p>Check the attachment!</p>')
->attachFromUrl(
$mailer->getTransport()->getUrl(),
'report.pdf',
'application/pdf'
);
$mailer->send($email);
Laravel Note: Use Laravel’s Http facade instead for simplicity:
use Illuminate\Support\Facades\Http;
$attachment = Http::get($url)->body();
Process for Async Email Handling:
Offload email sending to a background process (e.g., using Symfony’s Process):
use Symfony\Component\Process\Process;
$process = new Process(['php', 'artisan', 'queue:work', '--once']);
$process->start();
Custom Mailables with Symfony Features:
Extend Laravel’s Mailable to use Symfony’s Email class for advanced features:
use Symfony\Component\Mime\Email;
use Illuminate\Mail\Mailable;
class CustomMailable extends Mailable
{
public function build()
{
$email = (new Email())
->from('sender@example.com')
->to($this->recipient)
->subject('Custom Email')
->html($this->template);
return $this->withSwiftMessage(
$this->swiftMailer->createMessage()
->setFrom($email->getFrom())
->setTo($email->getTo())
->setSubject($email->getSubject())
->setBody($email->getHtmlBody())
);
}
}
Testing with Symfony’s Mailer:
Use Symfony’s TestMailer for isolated email testing:
use Symfony\Component\Mailer\TestMailer;
$testMailer = new TestMailer();
$mailer = new Mailer(new Transport(), $testMailer);
$mailer->send($email);
$emails = $testMailer->getMessages();
$this->assertCount(1, $emails);
config/mail.php (Laravel) and reuse them in Symfony’s Dsn:
// Laravel config/mail.php
'mailers' => [
'smtp' => [
'transport' => 'smtp',
'host' => env('MAIL_HOST'),
'port' => env('MAIL_PORT'),
],
],
// Symfony Dsn (if needed)
$transport = new Transport(
(new Dsn('smtp://user:pass@example.com:1025'))
);
Archived Package:
swiftmailer/swiftmailer or symfony/mailer). Avoid reinventing the wheel—use Laravel’s built-in Mail facade or Symfony’s Mailer directly.Deprecated SwiftMailer:
composer require symfony/mailer
Laravel’s Mail facade already supports this.Bridge Overhead:
Mail facade for simplicity.Mailer for advanced features (e.g., Email class, TestMailer).Log Emails:
Use Laravel’s Mail::pretend() or Symfony’s TestMailer to inspect emails without sending:
// Laravel
Mail::pretend();
Mail::to('user@example.com')->send(new CustomMailable());
// Symfony
$testMailer = new TestMailer();
$mailer->send($email);
dd($testMailer->getMessages());
Transport Issues:
storage/logs/laravel.log or Symfony’s Monolog handler..env:
MAIL_MAILER=smtp
MAIL_HOST=smtp.example.com
MAIL_PORT=587
MAIL_USERNAME=...
MAIL_PASSWORD=...
MAIL_ENCRYPTION=tls
Custom Swift Events:
Laravel’s Mail facade supports events (sending, sent, failed). Extend them for logging or analytics:
Mail::sending(function ($event) {
Log::debug('Sending email to: ' . $event->message->getTo()[0]['address']);
});
Symfony’s Email Class:
Use Symfony’s Email class for complex emails (e.g., embedded images, custom headers):
$email = (new Email())
->from('newsletter@example.com')
->to('user@example.com')
->subject('Weekly News')
->html($this->renderView('emails/newsletter', ['data' => $data]))
->embedFromPath($this->logoPath, 'logo_id');
Queueing Emails:
Laravel’s Mail facade queues emails by default. For Symfony’s Mailer, use:
$mailer->send($email); // Synchronous
// OR use a queue (e.g., Symfony Messenger or Laravel Queues)
$message = new SendEmailMessage($email);
$bus->dispatch($message);
How can I help you explore Laravel packages today?