bonnier/laravel-email-provider
Laravel package to fetch email template translations from a Translation/Email Manager. Provides BonnierMail::get($key, $replace, $locale) and an artisan command (bonnier:translation:get) to sync translations, configured via EMAIL_MANAGER_URL and service ID.
Installation:
composer require bonnier/laravel-email-provider
Publish the config file:
php artisan vendor:publish --provider="Bonnier\EmailProvider\EmailProviderServiceProvider" --tag="config"
Configuration:
Edit config/email-provider.php to define your email templates, translations, and API endpoints for the Email Manager.
First Use Case: Fetch and render a translated email template:
use Bonnier\EmailProvider\EmailProvider;
$emailProvider = app(EmailProvider::class);
$emailContent = $emailProvider->get('welcome_email', ['name' => 'John']);
Fetch Translated Content:
$content = $emailProvider->get('template_key', ['variables' => 'values']);
template_key with your configured key in config/email-provider.php.['name' => 'User']).Integration with Laravel Mail:
Mail::send([], [], function ($message) use ($emailContent) {
$message->subject('Welcome!');
$message->setBody($emailContent);
});
Caching: Enable caching in the config to reduce API calls:
'cache' => [
'enabled' => true,
'ttl' => 60, // Cache for 60 minutes
],
EmailProvider facade or service container to fetch templates dynamically.dev, prod) with different API endpoints.Deprecated Package:
API Dependency:
Translation Overrides:
config/email-provider.php includes a fallback_locale to avoid missing translations.Enable Logging: Add debug logs to track API calls:
'logging' => true,
Check storage/logs/laravel.log for errors or failed requests.
Mock API Responses: For testing, override the HTTP client or use a mock API service to simulate responses.
Custom HTTP Client:
Bind a custom GuzzleHttp\Client to the container for advanced request handling:
$this->app->bind(\GuzzleHttp\Client::class, function () {
return new \GuzzleHttp\Client([
'timeout' => 10,
'headers' => ['Authorization' => 'Bearer token'],
]);
});
Template Preprocessing:
Extend the Bonnier\EmailProvider\Contracts\TemplateRenderer interface to add custom logic (e.g., sanitization, placeholders).
Event Listeners:
Listen for email-provider.fetched events to log or modify fetched content:
Event::listen('email-provider.fetched', function ($templateKey, $content) {
// Custom logic here
});
How can I help you explore Laravel packages today?