bengor-user/mandrill-bridge
MandrillBridge is an adapter that makes BenGorUser’s User library compatible with Mandrill, providing a bridge for integrating Mandrill-based email delivery into your user workflow. Requires PHP 5.5+ and installs via Composer.
Install the Package
composer require bengor-user/mandrill-bridge
Ensure your Laravel project uses bengor-user/user (the parent library) as a dependency.
Configure Mandrill
Add Mandrill credentials to your .env:
MANDRILL_SECRET=your_mandrill_api_key
Basic Usage Inject the bridge into your service or controller:
use BenGorUser\MandrillBridge\MandrillBridge;
use BenGorUser\User\User;
$user = new User(); // Your User model/instance
$bridge = new MandrillBridge($user, new \Mandrill());
$bridge->sendWelcomeEmail(); // Example method (if implemented)
Key Classes
MandrillBridge: Core adapter class.User (from bengor-user/user): Your user model, extended for Mandrill compatibility.Extend User Model
Add Mandrill-compatible methods to your User model (e.g., sendEmail()):
use BenGorUser\MandrillBridge\MandrillBridge;
class User extends Authenticatable
{
public function sendEmail(MandrillBridge $bridge, string $template, array $mergeVars = [])
{
$bridge->setTemplate($template)
->setMergeVars($mergeVars)
->sendTo($this->email);
}
}
Service Layer Integration Create a service to abstract email logic:
class EmailService
{
public function __construct(private MandrillBridge $bridge) {}
public function sendWelcome(User $user)
{
$this->bridge->setTemplate('welcome')
->setMergeVars(['name' => $user->name])
->sendTo($user->email);
}
}
Event-Driven Emails
Trigger emails via Laravel events (e.g., Registered):
use Illuminate\Auth\Events\Registered;
use BenGorUser\MandrillBridge\MandrillBridge;
Registered::listen(function ($user) {
$bridge = app(MandrillBridge::class);
$bridge->setTemplate('welcome')
->sendTo($user->email);
});
Dynamic Templates Use user attributes to dynamically set templates:
$bridge->setTemplate($user->isPremium() ? 'premium_welcome' : 'welcome')
->sendTo($user->email);
Deprecated Package
phpunit or phpspec (as per the package’s test suite).Missing Documentation
bengor-user/user docs for core functionality.setTemplate()) follow Mandrill’s API conventions.Hardcoded Dependencies
bengor-user/user v0.8+. Update or fork if using a newer version.No Laravel Service Provider
MandrillBridge in AppServiceProvider:
$this->app->bind(MandrillBridge::class, function ($app) {
return new MandrillBridge(
auth()->user(),
new \Mandrill($app['config']['services.mandrill.secret'])
);
});
Enable Mandrill Debugging
Add to .env:
MANDRILL_DEBUG=true
Logs will appear in storage/logs/laravel.log.
Validate User Data
Ensure User model has email and required fields (e.g., name for merge vars):
$this->assertNotEmpty($user->email, 'User email is required for MandrillBridge.');
Test Locally Use Mandrill’s sandbox mode (free tier) to avoid real sends:
$mandrill = new \Mandrill('your_sandbox_key');
Custom Merge Vars
Extend MandrillBridge to add user-specific logic:
class CustomMandrillBridge extends MandrillBridge
{
public function setUserMergeVars(User $user)
{
$this->mergeVars = array_merge($this->mergeVars, [
'user_id' => $user->id,
'signup_date' => $user->created_at->format('Y-m-d'),
]);
}
}
Template Caching Cache compiled Mandrill templates to reduce API calls:
$template = cache()->remember("mandrill_template_{$templateName}", now()->addHours(1), function () use ($bridge) {
return $bridge->getTemplateContent($templateName);
});
Fallback to Laravel Mail
Gracefully fall back to Laravel’s Mail facade if Mandrill fails:
try {
$bridge->sendTo($user->email);
} catch (\Exception $e) {
Mail::to($user->email)->send(new WelcomeMail($user));
}
How can I help you explore Laravel packages today?