Installation:
composer require 2lenet/mailer-bundle
Ensure your project meets the requirements: PHP ≥ 7.1 and Symfony 4.
Enable the Bundle:
Add to config/bundles.php:
return [
// ...
TwoLE\MailerBundle\MailerBundle::class => ['all' => true],
];
First Use Case:
Inject MailerManager into a controller/service and send a basic email:
use TwoLE\MailerBundle\Mailer\MailerManager;
public function sendTestEmail(MailerManager $mailerManager)
{
$recipients = [
'recipient@example.com' => ['nom' => 'John Doe'],
];
$mail = $mailerManager->create('TEST', $recipients);
$mailerManager->send($mail);
}
Check Configuration:
Verify config/packages/two_le_mailer.yaml exists (or create it). Defaults are minimal; extend as needed.
Mail Creation:
Use MailerManager::create() with a template name (e.g., 'TEST') and an array of recipients with metadata (e.g., ['nom' => 'Name']).
Templates should be defined in templates/mail/ (e.g., TEST.html.twig).
Sending Emails:
$mail = $mailerManager->create('TEMPLATE_NAME', $recipients);
$mailerManager->send($mail); // Synchronous send
For async, use a message queue (e.g., Symfony Messenger) or wrap in a job.
Dynamic Recipients: Loop through collections to generate emails dynamically:
foreach ($users as $user) {
$mail = $mailerManager->create('WELCOME', [$user->email => ['nom' => $user->name]]);
$mailerManager->send($mail);
}
Template Inheritance:
Extend base templates (e.g., base.html.twig) for consistent layouts. Example:
{# templates/mail/TEST.html.twig #}
{% extends 'mail/base.html.twig' %}
{% block body %}
Hello {{ recipient.nom }}, welcome!
{% endblock %}
two_le_mailer.yaml to use Symfony’s MailerInterface:
two_le_mailer:
mailer: 'symfony.mailer.mailer'
config/packages/swiftmailer.yaml.2lenet/easyadmin-plus-bundle and configure menu.yaml to expose mail templates/recipients.Template Paths:
Templates must reside in templates/mail/ or a custom path defined in config. Example error:
Template "TEST.html.twig" not found in namespace "mail".
Fix: Verify the path in two_le_mailer.yaml:
two_le_mailer:
templates_dir: '%kernel.project_dir%/templates/custom_mail/'
Recipient Format:
The destinataires array must use email as the key and an associative array as the value:
// ❌ Wrong
$recipients = ['John Doe' => 'john@example.com'];
// ✅ Correct
$recipients = ['john@example.com' => ['nom' => 'John Doe']];
Async Sending: The bundle does not natively support async. Use Symfony Messenger or a queue system:
$this->messageBus->dispatch(new SendMailMessage($mail));
EasyAdminPlus Dependency:
The admin UI features (editing templates/recipients) require easyadmin-plus-bundle. Without it, these routes will 404.
config/packages/dev/monolog.yaml:
handlers:
mailer:
type: stream
path: '%kernel.logs_dir%/%kernel.environment%.mailer.log'
level: debug
channels: ['mailer']
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
public function __construct(ParameterBagInterface $params) {
dump($params->get('two_le_mailer'));
}
Custom Mailer:
Implement TwoLE\MailerBundle\Mailer\MailerInterface for custom transport (e.g., SES, SendGrid):
class CustomMailer implements MailerInterface {
public function send(Mail $mail) { /* ... */ }
}
Register as a service and set in config:
two_le_mailer:
mailer: 'app.custom_mailer'
Template Overrides:
Override default templates by placing files in templates/TwoLEMailerBundle/mail/.
Recipient Validation:
Extend TwoLE\MailerBundle\Validator\RecipientValidator to add custom validation logic (e.g., domain checks).
Events:
Listen for mailer.send events to intercept/modify emails:
$eventDispatcher->addListener(MailerEvents::SEND, function (MailerEvent $event) {
$event->getMail()->setSubject('[MODIFIED] ' . $event->getMail()->getSubject());
});
How can I help you explore Laravel packages today?