Installation
composer require druidvav/email-bundle
Add to config/bundles.php:
return [
// ...
Druidvav\EmailBundle\DruidvavEmailBundle::class => ['all' => true],
];
Configuration Publish the default config:
php bin/console druidvav:email:install
Edit config/packages/druidvav_email.yaml to match your SMTP/mailer settings.
First Email
Use the EmailService in a controller or service:
use Druidvav\EmailBundle\Service\EmailService;
class UserController extends AbstractController
{
public function sendWelcomeEmail(EmailService $emailService)
{
$emailService->send(
'user@example.com',
'Welcome!',
'This is a test email.'
);
}
}
Templates
Place templates in templates/emails/ (e.g., welcome.html.twig).
Use the render() method for templated emails:
$emailService->send(
'user@example.com',
'Welcome!',
$emailService->render('emails/welcome.html.twig', ['name' => 'John'])
);
Basic Email Sending
$emailService->send(
'recipient@example.com',
'Subject',
'Plaintext or HTML content'
);
Attachments
$emailService->send(
'recipient@example.com',
'Subject',
'Content',
['/path/to/file.pdf']
);
Templated Emails
templates/emails/ (e.g., invoice.html.twig).$emailService->send(
'user@example.com',
'Your Invoice',
$emailService->render('emails/invoice.html.twig', ['total' => 99.99])
);
Async Sending (Queue Integration)
Configure druidvav_email.yaml to use Symfony’s Messenger component:
druidvav_email:
async: true
Then dispatch emails as messages:
$emailService->dispatch(
'recipient@example.com',
'Subject',
'Content'
);
Customizing Defaults
Override defaults in config/packages/druidvav_email.yaml:
druidvav_email:
from_email: 'noreply@example.com'
from_name: 'My App'
charset: 'UTF-8'
Dependency Injection
Inject EmailService into services/controllers for reusable logic:
public function __construct(private EmailService $emailService) {}
Event-Driven Emails
Trigger emails from Symfony events (e.g., KernelEvents::TERMINATE):
$eventDispatcher->addListener(KernelEvents::TERMINATE, function () use ($emailService) {
$emailService->send('admin@example.com', 'System Alert', 'Server restarted.');
});
Testing
Use the EmailService in tests with a mock mailer:
$emailService = $this->createMock(EmailService::class);
$emailService->expects($this->once())->method('send');
Local Development
Use Symfony’s null transport for testing:
framework:
mailer:
dsn: 'null://default'
Template Paths
templates/emails/ (case-sensitive on some systems).render() (e.g., 'emails/welcome.html.twig').Async Mode Quirks
async: true, emails may not send immediately (depends on Messenger transport).messenger.transport.async DSN is configured in framework.yaml.Attachment Limits
var/log/dev.log).Configuration Overrides
framework.mailer. Explicitly set from_email to avoid conflicts.Logs Enable Symfony’s mailer logging:
monolog:
handlers:
main:
level: debug
channels: ['!event']
Common Errors
druidvav_email.yaml.Content-Type header is set to text/html in your template.Custom Mailer
Extend the EmailService to add logic:
class CustomEmailService extends EmailService
{
public function sendWithTracking($to, $subject, $content)
{
$this->trackEmail($to); // Custom logic
parent::send($to, $subject, $content);
}
}
Dynamic Templates
Use Twig’s include to modularize templates:
{# templates/emails/base.html.twig #}
<html>
<body>
{{ include('emails/header.html.twig') }}
{{ content }}
</body>
</html>
Environment-Specific Config Use Symfony’s parameter bags to switch configs:
# config/packages/dev/druidvav_email.yaml
druidvav_email:
from_email: 'dev@example.com'
How can I help you explore Laravel packages today?