Installation:
composer require symfony/swiftmailer-bundle
Add the bundle to config/bundles.php (Symfony) or config/app.php (Laravel via bridge):
return [
// ...
Symfony\Swiftmailer\SwiftmailerBundle\SwiftmailerBundle::class => ['all' => true],
];
Configuration:
Edit .env for mail settings (Laravel-style):
MAIL_MAILER=smtp
MAIL_HOST=smtp.example.com
MAIL_PORT=587
MAIL_USERNAME=your_username
MAIL_PASSWORD=your_password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=from@example.com
MAIL_FROM_NAME="Your App"
First Email:
Inject Swift_Mailer into a controller/service:
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\Mailer\MailerInterface;
public function sendWelcomeEmail(MailerInterface $mailer)
{
$email = (new TemplatedEmail())
->from('from@example.com')
->to('user@example.com')
->subject('Welcome!')
->htmlTemplate('emails/welcome.html.twig');
$mailer->send($email);
}
Templates:
Place Twig templates in resources/views/emails/ (Laravel) or templates/emails/ (Symfony).
Dynamic Emails: Pass data to templates:
$email->htmlTemplate('emails/invoice.html.twig')
->context(['invoice' => $invoiceData]);
Attachments:
$email->attachFromPath('/path/to/file.pdf');
// Or inline:
$email->embed($mailer->getSymfonyMailer()->getTransport()->getUrl(), 'cid:unique_id');
Queueing Emails (Laravel):
Use Mail::to()->send() with Laravel’s queue system:
Mail::to('user@example.com')->send(new WelcomeEmail($user));
Testing:
Use Symfony’s TestMailer or Laravel’s MailFake:
$mailer = $this->getMailer();
$sent = $mailer->getSentMessages();
symfony/mailer + symfony/swiftmailer for modern SwiftMailer features.Swift_Events_SendEvent for logging/auditing:
$mailer->getTransport()->getEventDispatcher()->addListener(
'sent',
function ($event) { /* Log email */ }
);
X-Mailer header:
$email->getHeaders()->addTextHeader('X-Mailer', 'YourApp/1.0');
Deprecation Warnings:
SwiftmailerBundle is archived; prefer symfony/mailer (Symfony 5+) or Laravel’s native mailers.Swift_* classes; use Symfony\Component\Mailer\* equivalents.Configuration Overrides:
.env may conflict with Symfony’s config/packages/swiftmailer.yaml. Prioritize one source.TLS/SSL Issues:
php bin/console debug:mailer
MAIL_ENCRYPTION=null for plaintext (e.g., local testing).Template Caching:
php artisan view:clear
# config/packages/swiftmailer.yaml
swiftmailer:
logging: true
$mailer->getTransport()->getEventDispatcher()->addListener(
'sent',
function ($event) { dd($event->getMessage()); }
);
Custom Transports:
Override Swift_Transport for APIs (e.g., SendGrid):
$transport = new Swift_SmtpTransport('smtp.sendgrid.net', 587, [
'username' => 'apikey',
'password' => 'your_key',
'source_ip' => 'your.ip',
]);
Message Modifiers:
Use Swift_Events_SendEvent to alter messages pre-send:
$dispatcher->addListener('sent', function ($event) {
$event->getMessage()->getHeaders()->addTextHeader('X-Custom', 'Value');
});
Laravel-Specific:
Mail::raw() for non-Twig emails:
Mail::raw('Plain text', function ($message) {
$message->to('user@example.com')->subject('Plain Email');
});
How can I help you explore Laravel packages today?