Installation:
composer require accord/mandrill-swiftmailer
Add to config/mail.php under transports:
'mandrill' => [
'driver' => 'mandrill',
'api_key' => env('MANDRILL_API_KEY'),
'async' => env('MANDRILL_ASYNC', false),
],
First Use Case:
Configure Laravel’s mail service in config/app.php:
'mail' => [
'driver' => 'mandrill',
// ... other config
],
Send an email via Laravel’s Mail facade:
Mail::send([], [], function ($message) {
$message->to('user@example.com')
->subject('Test Email')
->setBody('Hello, world!');
});
config/mail.php (Transport configuration)app/Providers/AppServiceProvider.php (Mail service binding, if customized)vendor/accord/mandrill-swiftmailer (Core transport logic)Transport Initialization: Use Laravel’s built-in mail configuration to auto-instantiate the transport:
$transport = new \Accord\MandrillSwiftMailer\MandrillTransport($dispatcher);
$transport->setApiKey(config('mail.mandrill.api_key'));
$transport->setAsync(config('mail.mandrill.async', false));
Integrating with Laravel’s Mail System:
Mail facade for sending emails (as shown above).Mailable class and use MandrillTransport for advanced features:
public function build()
{
$message = $this->view('emails.welcome');
$message->withSwiftMessage(function ($swiftMessage) {
$swiftMessage->getHeaders()
->addTextHeader('X-MC-Autotext', true);
});
return $message;
}
Bulk Sending: Enable async mode in config and batch emails:
Mail::raw('Bulk content', function ($message) use ($recipients) {
foreach ($recipients as $email) {
$message->to($email);
}
$message->getHeaders()
->addTextHeader('X-MC-Autotext', true);
})->setAsync(true);
Event Handling:
Listen to mail.sent events to log or process Mandrill-specific metadata:
Mail::sent(function ($message) {
if ($message->getTransport() instanceof \Accord\MandrillSwiftMailer\MandrillTransport) {
// Access Mandrill-specific data (e.g., message ID)
}
});
$transport->setApiKey(app('mandrill.api_key'));
log or smtp) for resilience:
'transports' => [
'mandrill' => [
'driver' => 'mandrill',
'api_key' => env('MANDRILL_API_KEY'),
'fallback' => 'log', // Fallback to log if Mandrill fails
],
],
Deprecation Warning:
symfony/mailer with the Mandrill bridge if possible.Async Mode Quirks:
setAsync(true)) uses Mandrill’s background sending but does not queue emails locally. Emails are sent immediately to Mandrill’s queue.Header Conflicts:
X-MC-* may conflict with Laravel’s default headers. Validate headers before sending:
if ($message->getHeaders()->has('X-MC-Autotext')) {
// Safe to proceed
}
Rate Limits:
throttle middleware for API calls if integrating directly with Mandrill’s API.Enable Logging: Configure Monolog to log Mandrill transport interactions:
'logging' => [
'default' => 'single',
'channels' => [
'single' => [
'driver' => 'single',
'level' => 'debug',
'handler' => env('LOG_HANDLER', 'stream'),
'path' => storage_path('logs/laravel.log'),
],
],
],
Check logs for MandrillTransport entries.
Validate API Key:
$transport = new \Accord\MandrillSwiftMailer\MandrillTransport($dispatcher);
$transport->setApiKey('invalid_key'); // Force error to test error handling
SwiftMailer Events:
messageSent events to debug failures:
$dispatcher->addListener('messageSent', function ($event) {
if ($event->getTransport() instanceof \Accord\MandrillSwiftMailer\MandrillTransport) {
error_log('Mandrill sent: ' . $event->getMessage()->getId());
}
});
Custom Headers: Extend the transport to add default Mandrill headers:
class CustomMandrillTransport extends \Accord\MandrillSwiftMailer\MandrillTransport
{
public function __construct($dispatcher)
{
parent::__construct($dispatcher);
$this->addDefaultHeaders();
}
protected function addDefaultHeaders()
{
$this->getSwiftMessage()->getHeaders()
->addTextHeader('X-MC-Autotext', true)
->addTextHeader('X-MC-GoogleAnalytics', 'yourdomain.com');
}
}
Webhook Integration: Use Mandrill’s message events to trigger Laravel jobs:
// In your Mandrill webhook endpoint
$event = $request->input('event');
if ($event === 'send') {
EmailSentJob::dispatch($request->input('msg'));
}
Testing: Mock the transport in PHPUnit:
$mockTransport = $this->createMock(\Accord\MandrillSwiftMailer\MandrillTransport::class);
$mockTransport->method('send')->willReturn(true);
$this->app->instance(\Swift_Transport::class, $mockTransport);
Async Mode:
Environment Variables:
MANDRILL_API_KEY is set in .env and never committed to version control.env() helper or a dedicated config file for sensitive data.SwiftMailer Version:
composer.json:
"swiftmailer/swiftmailer": "5.4.*"
How can I help you explore Laravel packages today?