spatie/laravel-mailcoach-unlayer
Optional add-on for Mailcoach that integrates the free Unlayer drag-and-drop email editor. Create and edit beautiful newsletters with a visual builder directly inside Mailcoach.
Installation Require the package via Composer in a Laravel project with Mailcoach installed:
composer require spatie/laravel-mailcoach-unlayer
Publish the package configuration (if needed):
php artisan vendor:publish --provider="Spatie\MailcoachUnlayer\MailcoachUnlayerServiceProvider"
Configuration
Ensure your config/mailcoach.php includes the Unlayer editor as an option:
'editor' => [
'default' => 'unlayer',
'options' => [
'unlayer' => [
'enabled' => true,
'api_key' => env('UNLAYER_API_KEY'), // Required
],
],
],
Add your Unlayer API key to .env:
UNLAYER_API_KEY=your_api_key_here
First Use Case
Template Creation
$template = Mailcoach::templates()->create([
'name' => 'Welcome Email',
'editor' => 'unlayer',
'content' => $unlayerJsonContent, // Generated by Unlayer
]);
Campaign Integration
$campaign = Mailcoach::campaigns()->create([
'name' => 'Promo Campaign',
'template_id' => $template->id,
]);
Dynamic Content
{{ user.name }}) alongside Unlayer’s dynamic blocks.API-Driven Workflows Fetch Unlayer’s pre-designed templates via its API and import them into Mailcoach:
$unlayerTemplate = Unlayer::getTemplate($templateId);
$mailcoachTemplate = Mailcoach::templates()->create([
'content' => $unlayerTemplate['json'],
]);
Fallback Editors
Configure a fallback editor (e.g., mailcoach or tinymce) in config/mailcoach.php if Unlayer fails:
'editor' => [
'default' => 'unlayer',
'fallback' => 'mailcoach',
],
Custom Styling
Extend Unlayer’s default styles by overriding its CSS in Mailcoach’s assets or via the unlayer config:
'unlayer' => [
'custom_css' => file_get_contents(public_path('css/unlayer-overrides.css')),
],
API Key Management
UNLAYER_API_KEY in .env causes silent failures.if (empty(config('mailcoach.editor.options.unlayer.api_key'))) {
throw new \RuntimeException('Unlayer API key not configured.');
}
Content Size Limits
content field).content field for metadata and store the JSON in a separate table.Caching Quirks
php artisan cache:clear
php artisan view:clear
Dynamic Content Conflicts
{{ block }}) may conflict with Mailcoach’s personalization syntax.{{{ user.name }}}) or escape placeholders in Unlayer’s JSON.Editor Not Loading Check browser console for 403/404 errors on Unlayer’s API endpoints. Verify:
Template Rendering Issues
Use Mailcoach’s preview feature to test Unlayer templates:
$preview = Mailcoach::templates()->find($templateId)->preview(['user' => $user]);
Inspect the rendered HTML for missing styles or broken placeholders.
Custom Unlayer Config Override Unlayer’s default settings (e.g., toolbar options) via the config:
'unlayer' => [
'toolbar' => [
'options' => ['bold', 'italic', 'undo', 'redo'],
],
],
Webhook Integration Trigger actions in Laravel when Unlayer templates are updated via its API:
// In a route or controller
$hook = app(\Spatie\MailcoachUnlayer\Listeners\UnlayerWebhookListener::class);
$hook->handle($request);
Template Validation Add custom validation to Unlayer-generated content before saving:
use Spatie\MailcoachUnlayer\Validators\UnlayerContentValidator;
$validator = new UnlayerContentValidator();
if (!$validator->validate($template->content)) {
throw new \InvalidArgumentException('Invalid Unlayer template structure.');
}
Local Development
For offline testing, use Unlayer’s local development mode and proxy its API through Laravel’s routes/api.php:
Route::get('/unlayer-api/{path}', function ($path) {
return response()->file(public_path("unlayer-local/{$path}"));
});
How can I help you explore Laravel packages today?