adcog-cpi/email-bundle
EBEmailBundle is a Symfony bundle to define and send templated emails via config. Set multiple senders, per-email subjects and Twig text/HTML templates, pass template variables, include inline images and PDF attachments, and apply global vars, images, attachments, and recipients.
Installation:
composer require adcog-cpi/email-bundle
Add to config/bundles.php:
return [
// ...
Adcog\EBEmailBundle\EBEmailBundle::class => ['all' => true],
];
Basic Configuration (config/packages/eb_email.yaml):
eb_email:
senders:
- { name: "Support Team", email: "support@example.com" }
emails: []
Define an Email Template (templates/App/Email/_contact.txt.twig):
Hello {{ name }},
Thanks for contacting us!
Register the Email (config/packages/eb_email.yaml):
eb_email:
emails:
contact:
subject: 'New contact from {{ name }}!'
text_template: 'App/Email/_contact.txt.twig'
Send the Email (Controller/Service):
$this->get('eb_email')->send('contact', 'user@example.com', ['name' => 'John']);
Send a welcome email with dynamic content:
# config/packages/eb_email.yaml
eb_email:
emails:
welcome:
subject: 'Welcome, {{ user.name }}!'
html_template: 'App/Email/welcome.html.twig'
text_template: 'App/Email/welcome.txt.twig'
// In a controller/service
$user = $this->getUser(); // Assume User has `name` and `email` properties
$this->get('eb_email')->send('welcome', $user);
$this->get('eb_email')->send('contact', 'user@example.com', ['data' => 'value']);
$this->get('eb_email')->send('newsletter', ['a@example.com', 'b@example.com']);
$user = $this->getUser(); // Must implement `getEmail()` or `getUsername()`
$this->get('eb_email')->send('password_reset', $user, ['token' => 'xyz']);
templates/base_email.txt.twig):
{# Base content #}
{{ block('content') }}
templates/App/Email/_contact.txt.twig):
{% extends 'base_email.txt.twig' %}
{% block content %}
Hello {{ name }},
{% endblock %}
Extend the bundle or use a wrapper:
// Custom service (not natively supported)
$email = $this->get('eb_email')->create('contact', 'user@example.com');
$email->addAttachment('/path/to/file.pdf');
$this->get('mailer')->send($email);
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use App\Events\UserRegisteredEvent;
class EmailSubscriber implements EventSubscriberInterface {
public static function getSubscribedEvents() {
return [UserRegisteredEvent::class => 'sendWelcomeEmail'];
}
public function sendWelcomeEmail(UserRegisteredEvent $event) {
$this->get('eb_email')->send('welcome', $event->getUser());
}
}
eb_email:
emails:
contact:
subject:
en: 'New contact from {{ name }}!'
fr: 'Nouveau contact de {{ name }}!'
text_template: 'App/Email/_contact.txt.twig'
$this->get('eb_email')->send('contact', 'user@example.com', ['name' => 'John'], 'fr');
$container->set('eb_email', $this->createMock(EmailService::class));
$mock->expects($this->once())->method('send')->with('contact', 'user@example.com');
Template Paths:
App/Email/_contact.txt.twig) or ensure twig.paths is configured.TemplateNotFoundException if the path is relative or incorrect.Recipient Resolution:
getEmail() or getUsername().InvalidArgumentException if the recipient is neither a string nor an object with getEmail().Twig Context:
send() are not automatically merged with the template context.$this->get('eb_email')->send('contact', 'user@example.com', ['name' => 'John']);
Sender Configuration:
HTML/Text Templates:
text_template is provided, the email will be sent as plaintext.text_template and html_template are provided, the bundle sends a multipart/alternative email.Enable Twig Debugging:
# config/packages/twig.yaml
twig:
debug: true
strict_variables: true
Log Email Content:
$this->get('eb_email')->send('contact', 'user@example.com', ['name' => 'John'], null, true); // Debug mode
Check for Typos:
eb_email.emails.<name> keys in YAML (case-sensitive).Verify Mailer Transport:
framework.mailer is configured (e.g., transport: '%env(MAILER_DSN)%').Custom Email Service:
# config/services.yaml
services:
App\Service\CustomEmailService:
decorates: 'eb_email'
arguments: ['@eb_email.inner']
Dynamic Template Selection:
$template = $this->getTemplateResolver()->resolve($emailName, $locale);
Queue Support:
send() call in a job for async processing:
use Symfony\Component\Messenger\MessageBusInterface;
$bus->dispatch(new SendEmailMessage('contact', 'user@example.com', ['name' => 'John']));
Event Dispatching:
$dispatcher->dispatch(new EmailSentEvent($emailName, $recipients, $context));
Fallback Templates:
if (!$this->templateLoader->getSource($template)) {
$template = 'App/Email/_fallback.txt.twig';
}
How can I help you explore Laravel packages today?