bengor-user/mandrill-bridge-bundle
Install the Bundle
composer require bengor-user/mandrill-bridge-bundle
Ensure MandrillBridgeBundle is enabled in config/bundles.php:
return [
// ...
BenGorUser\MandrillBridgeBundle\MandrillBridgeBundle::class => ['all' => true],
];
Configure Mandrill API Key
Add your Mandrill API key to config/packages/mandrill_bridge.yaml (or create it):
mandrill_bridge:
api_key: '%env(MANDRILL_API_KEY)%'
First Use Case: Sending a Welcome Email
Trigger the email in your user registration workflow (e.g., after UserBundle's onRegistrationSuccess event):
use BenGorUser\MandrillBridgeBundle\Event\SendEmailEvent;
// In your registration listener/service
$event = new SendEmailEvent(
$user, // User entity
'welcome_email', // Template name (must exist in Mandrill)
['name' => $user->getFullName()] // Template variables
);
$this->dispatcher->dispatch($event);
Event-Driven Email Dispatch
SendEmailEvent for all Mandrill-triggered emails (e.g., password resets, notifications).UserBundle's UserEvents to dispatch emails post-actions:
// src/EventListener/UserListener.php
public function onRegistrationSuccess(UserEvents::onRegistrationSuccess, User $user) {
$event = new SendEmailEvent($user, 'welcome_email', ['name' => $user->getName()]);
$this->dispatcher->dispatch($event);
}
Template Management
MandrillTemplates::WELCOME).Dynamic Recipients
$event = new SendEmailEvent($user, 'template_name', [], $user->getEmail());
Async Processing (Optional)
# config/packages/messenger.yaml
framework:
messenger:
transports:
mandrill: '%env(MANDRILL_MESSENGER_TRANSPORT_DSN)%'
UserBundle Compatibility
BenGorUser\UserBundle is installed (composer require bengor-user/user-bundle).UserBundle's event listeners to inject SendEmailEvent where needed.Custom Templates
MandrillClient to support custom template logic:
// src/Service/MandrillClient.php
public function sendCustomTemplate(User $user, string $template, array $vars) {
$this->sendTemplate($template, $vars, $user->getEmail());
}
Testing
MandrillClient in PHPUnit:
$this->client = $this->createMock(MandrillClient::class);
$this->client->expects($this->once())
->method('sendTemplate')
->with('welcome_email', ['name' => 'John'], 'john@example.com');
Deprecated Dependencies
UserBundle (last updated 2017). Verify compatibility with your User entity structure.getFullName()).Mandrill Template Validation
// src/Service/MandrillClient.php
public function templateExists(string $name): bool {
$templates = $this->client->templates->list();
return in_array($name, wp_list_pluck($templates, 'name'));
}
API Rate Limits
Environment Variables
MANDRILL_API_KEY in .env. Add a fallback in config/packages/mandrill_bridge.yaml:
mandrill_bridge:
api_key: '%env(MANDRILL_API_KEY)%{default:null}'
Enable API Logging
Add to config/packages/mandrill_bridge.yaml:
mandrill_bridge:
debug: '%kernel.debug%'
Logs will appear in var/log/dev.log.
Common Errors
| Error | Cause | Solution |
|---|---|---|
Invalid API key |
Wrong MANDRILL_API_KEY |
Verify .env and Mandrill dashboard. |
Template not found |
Template name mismatch | Check Mandrill’s "Templates" tab. |
Recipient invalid |
Malformed email | Validate emails before dispatching. |
Custom Email Events
Extend SendEmailEvent for domain-specific data:
class CustomEmailEvent extends SendEmailEvent {
public function __construct(
User $user,
string $template,
array $vars,
?string $recipient = null,
array $customData = []
) {
parent::__construct($user, $template, $vars, $recipient);
$this->customData = $customData;
}
}
Override Mandrill Client
Replace the default MandrillClient service:
# config/services.yaml
services:
BenGorUser\MandrillBridgeBundle\Service\MandrillClient:
class: App\Service\CustomMandrillClient
arguments: ['@mandrill.client']
Add Global Email Headers Inject headers via a compiler pass:
// src/DependencyInjection/Compiler/MandrillHeadersPass.php
public function process(ContainerBuilder $container) {
$definition = $container->findDefinition('mandrill_bridge.client');
$definition->addMethodCall('setDefaultHeaders', [['X-My-Header' => 'value']]);
}
How can I help you explore Laravel packages today?