cooperspeele/twig-mailer-bundle
Installation
composer require cooperspeele/twig-mailer-bundle
Add to config/bundles.php (Symfony):
Cooperspeele\TwigMailerBundle\TwigMailerBundle::class => ['all' => true],
Configure Twig Mailer
Update config/packages/twig_mailer.yaml (Symfony) or manually configure in Laravel via config/twig_mailer.php:
twig_mailer:
mailer: 'mailer' // Laravel's default mailer
from_address: 'noreply@example.com'
from_name: 'Your App'
First Use Case
Create a Twig template (resources/views/emails/welcome.twig):
<html>
<body>
<h1>Welcome, {{ name }}!</h1>
</body>
</html>
Send an email in Laravel:
use Cooperspeele\TwigMailerBundle\Mailer\TwigMailer;
$mailer = app(TwigMailer::class);
$mailer->send('emails.welcome', ['name' => 'John'], 'Welcome Email', [
'to' => 'user@example.com'
]);
Template-Based Emails
resources/views/emails/ with .twig extension.{{ user.name }}).Reusable Components
header.twig, footer.twig) in resources/views/emails/_partials/.{% include '_partials/header.twig' %}
Dynamic Recipients
{% for user in users %}
<p>{{ user.email }}: {{ user.message }}</p>
{% endfor %}
$mailer->send('emails/newsletter', ['users' => $users], 'Newsletter', [
'to' => ['user1@example.com', 'user2@example.com']
]);
Attachments
Mailable or directly:
$mailer->send('emails/invoice', ['invoice' => $invoice], 'Invoice', [
'to' => 'client@example.com',
'attachments' => ['/path/to/invoice.pdf' => 'invoice.pdf']
]);
Queueing Emails
$mailer->queue('emails/welcome', ['name' => 'John'], 'Welcome Email', [
'to' => 'user@example.com'
]);
Laravel Notifications: Extend Mailable to use TwigMailer:
public function build()
{
return $this->markdown('emails.welcome')
->with(['name' => $this->user->name]);
}
(Note: Requires minor adjustments to use Twig instead of Blade.)
Testing:
Use Laravel’s MailFake or mock TwigMailer in tests:
$mailer = Mockery::mock(TwigMailer::class);
$mailer->shouldReceive('send')->once();
Twig vs. Blade Confusion
@foreach won’t work).{% %}) and functions ({{ }}).Mailer Configuration Overrides
mailer key in twig_mailer.yaml is misconfigured, emails may fail silently.config/mail.php for valid transport settings (e.g., mailgun, ses).Template Paths
resources/views/emails/ or a custom path configured in the bundle.twig_mailer.template_path in config if using a non-standard path.Caching Issues
php artisan view:clear
Recipient Format
to parameter expects an array or string (e.g., ['user@example.com'] or 'user@example.com').Enable Twig Debugging:
Add to config/twig_mailer.php:
twig_mailer:
debug: true
Errors will show detailed Twig stack traces.
Log Sent Emails: Use Laravel’s mail log:
Mail::pretend(true); // Log emails instead of sending
Custom Twig Extensions
Add extensions in config/twig_mailer.php:
twig_mailer:
twig:
extensions:
- App\Twig\CustomExtension
Override Mailer Class
Bind a custom mailer in AppServiceProvider:
$this->app->bind(TwigMailer::class, function ($app) {
return new CustomTwigMailer($app['mailer'], $app['twig']);
});
Event Listeners
Listen for TwigMailerEvents (if the bundle emits them) or wrap the mailer in a decorator for pre/post-send logic.
php artisan twig:compile to cache templates (useful for production).$mailer->later(now()->addMinutes(5), 'emails/newsletter', [...], 'Newsletter', ['to' => $recipients]);
How can I help you explore Laravel packages today?