Installation:
composer require disjfa/mail-bundle
Add the bundle to config/bundles.php:
return [
// ...
Disjfa\MailBundle\DisjfaMailBundle::class => ['all' => true],
];
Configure Routes:
Add to config/routes/disjfa_mail.yaml:
disjfa_mail:
resource: '@DisjfaMailBundle/Controller/'
type: annotation
prefix: '/admin'
First Use Case:
Create a custom mail class extending MailInterface in src/Mail/:
namespace App\Mail;
use Disjfa\MailBundle\Mail\MailInterface;
use Twig\Environment;
class WelcomeMail implements MailInterface
{
public function getName() { return 'Welcome Email'; }
public function getSubject() { return 'Welcome to our platform!'; }
public function getContent(Environment $twig) {
return $twig->render('emails/welcome.html.twig', ['name' => 'User']);
}
}
Send the Email:
Inject the MailService and call:
$mailService->send(new WelcomeMail(), ['user@example.com']);
Template-Based Emails:
{{ → {{ '{{' }}).templates/emails/welcome.html.twig):
<p>Hello {{ name }}!</p>
Dynamic Variables:
send() method’s second argument:
$mailService->send(new WelcomeMail(), ['name' => 'John']);
{{ variable }}.Translation Support:
Translator into your MailInterface implementation:
public function getSubject(TranslatorInterface $translator) {
return $translator->trans('welcome.subject');
}
Reusable Mail Classes:
src/Mail/ and reuse across controllers/services.Symfony Mailer:
Configure DisjfaMailBundle to use Symfony’s Mailer component via config/packages/disjfa_mail.yaml:
disjfa_mail:
mailer: symfony_mailer
Queue Emails: Use Symfony Messenger to queue emails:
disjfa_mail:
mailer: messenger_mailer
Testing:
Mock MailService in tests:
$mailService = $this->createMock(MailService::class);
$mailService->expects($this->once())->method('send');
Twig Delimiter Escaping:
{{ and }} in templates will break variable parsing.{{ '{{' }} variable {{ '}}' }} instead of {{ variable }}.Circular Dependencies:
MailService into MailInterface implementations (circular dependency).send() arguments.Missing Routes:
disjfa_mail.yaml routes will break admin access to mail settings.No Default Transport:
config/packages/mailer.yaml or use Symfony’s framework/mailer.Check Sent Emails:
Use Symfony’s MailerDebugListener to log emails:
framework:
mailer:
dsn: '%env(MAILER_DSN)%'
delivery_mode: 'debug' # Logs emails to `var/log/dev.log`
Validate Mail Classes:
Ensure all MailInterface methods are implemented. Use PHPStan:
vendor/bin/phpstan analyse src/Mail
Custom Transports:
Extend Disjfa\MailBundle\Transport\TransportInterface to add support for non-Symfony transports (e.g., AWS SES).
Event Listeners:
Listen to mail.send events to log or modify emails:
// config/services.yaml
services:
App\EventListener\MailListener:
tags:
- { name: kernel.event_listener, event: mail.send, method: onMailSend }
Dynamic Templates:
Override Disjfa\MailBundle\Mail\MailInterface to support dynamic template selection:
public function getTemplatePath(): string { return 'emails/' . $this->getName() . '.html.twig'; }
Attachments:
Extend the bundle to support attachments by modifying MailInterface:
public function getAttachments(): array { return []; }
How can I help you explore Laravel packages today?