daily/hipaway-mandrill-bundle
Installation:
composer require hipaway-travel/mandrill-bundle:dev-master
Enable the bundle in config/bundles.php:
Hip\MandrillBundle\HipMandrillBundle::class => ['all' => true],
Configuration:
Add Mandrill API key to config/packages/hip_mandrill.yaml:
hip_mandrill:
api_key: '%env(MANDRILL_API_KEY)%'
Ensure MANDRILL_API_KEY is in your .env file.
First Use Case:
Create a message class (e.g., src/Message/WelcomeEmail.php):
namespace App\Message;
use Hip\MandrillBundle\Message\MandrillMessage;
class WelcomeEmail extends MandrillMessage
{
public function __construct()
{
$this->setSubject('Welcome to Our App!');
$this->setFromEmail('noreply@example.com');
$this->setFromName('Our App');
$this->setTo(['user@example.com' => 'User Name']);
$this->setHtml('<h1>Hello!</h1><p>Welcome aboard.</p>');
}
}
Dispatch it in a controller:
use Hip\MandrillBundle\MandrillManager;
public function sendWelcomeEmail(MandrillManager $mandrill)
{
$message = new WelcomeEmail();
$mandrill->send($message);
}
Message Creation:
Extend Hip\MandrillBundle\Message\MandrillMessage and override methods like:
setSubject(), setFromEmail(), setTo(), setHtml(), setText().setHeaders() for custom headers (e.g., Reply-To).addAttachment().Dynamic Messages: Use dependency injection to pass dynamic data:
$message = new WelcomeEmail($user->getEmail(), $user->getName());
Then inject via constructor in WelcomeEmail:
public function __construct(string $email, string $name)
{
$this->setTo([$email => $name]);
$this->setHtml($this->renderHtml($name));
}
Templates: Use Mandrill templates by setting:
$this->setTemplateName('welcome-template');
$this->setTemplateContent([
'name' => 'User Name',
'app_name' => 'Our App',
]);
Async Sending: Queue messages with Symfony Messenger (if integrated):
$message = new WelcomeEmail();
$dispatcher->dispatch(new SendMandrillMessage($message));
use Hip\MandrillBundle\Event\MessageSentEvent;
public function onMessageSent(MessageSentEvent $event)
{
if (!$event->isSuccess()) {
$this->logger->error('Mandrill error: ' . $event->getError());
}
}
config/packages/hip_mandrill.yaml:
hip_mandrill:
debug: '%kernel.debug%'
MandrillManager in tests:
$this->mockBuilder->getMockBuilder(MandrillManager::class)
->disableOriginalConstructor()
->getMock();
API Key Exposure:
MANDRILL_API_KEY in config files. Always use .env.Rate Limits:
429 Too Many Requests errors gracefully:
try {
$mandrill->send($message);
} catch (MandrillException $e) {
if ($e->getCode() === 429) {
sleep(30); // Retry after 30 seconds
$mandrill->send($message);
}
}
Template Caching:
Character Encoding:
setHtml() and setText() use UTF-8 encoding to avoid corruption in emails.config/packages/monolog.yaml:
handlers:
hip_mandrill:
type: stream
path: "%kernel.logs_dir%/hip_mandrill.log"
level: debug
MandrillManager to log raw responses:
public function send(MandrillMessage $message)
{
$response = $this->client->messages->send($message->toArray(), true);
$this->logger->debug('Mandrill raw response:', ['response' => $response]);
return $response;
}
Custom Message Classes:
Override Hip\MandrillBundle\Message\MandrillMessage to add domain-specific methods:
class PasswordResetEmail extends MandrillMessage
{
public function setResetLink(string $link): self
{
$this->setHtml($this->renderHtml($link));
return $this;
}
}
Event Listeners:
Subscribe to hip_mandrill.message_sent event to post-process messages:
public static function getSubscribedEvents()
{
return [
'hip_mandrill.message_sent' => 'onMessageSent',
];
}
Custom Transport:
Replace the default Guzzle client by binding a custom MandrillClient service:
# config/services.yaml
Hip\MandrillBundle\MandrillManager:
arguments:
$client: '@custom_mandrill.client'
Batch Sending: Use Mandrill’s batch API for bulk emails:
$batch = new Hip\MandrillBundle\Message\MandrillBatch();
$batch->addMessage($message1);
$batch->addMessage($message2);
$mandrill->sendBatch($batch);
How can I help you explore Laravel packages today?