spatie/craft-mailcoach
Mailcoach adapter plugin for Craft CMS 5+. Install via the Plugin Store or Composer to add Mailcoach integration to your Craft project. Requires PHP 8.2 or later.
Installation:
composer require spatie/craft-mailcoach
./craft plugin/install mailcoach
Verify installation via ./craft plugin/list (should show mailcoach as active).
First Use Case:
newsletter) and define a Mailcoach Template (e.g., welcome.html).mailcoach mailer in your code:
use Spatie\CraftMailcoach\Facades\Mailcoach;
Mailcoach::send('newsletter', ['user@example.com'], ['name' => 'John']);
Key Files to Review:
config/mailcoach.php (default config, override in config/mailcoach.php).vendor/spatie/craft-mailcoach/src/ (core logic, e.g., MailcoachService.php for advanced use).Sending Emails:
Mailcoach::send('campaign-slug', ['recipient@example.com'], ['data' => 'value']);
$recipients = ['a@example.com', 'b@example.com'];
Mailcoach::send('campaign-slug', $recipients, ['key' => 'shared-value']);
Mailcoach::send('campaign-slug', ['email@example.com'], [], [
'attachments' => [Storage::disk('public')->path('file.pdf')]
]);
Dynamic Data Injection:
{{ data.name }}).Mailcoach::send('campaign-slug', ['email@example.com'], [
'name' => 'Alice',
'custom_field' => 'value'
]);
Event Listeners:
Mailcoach.Sent events to log or process sent emails:
Event::on(
\Spatie\CraftMailcoach\Events\MailcoachSent::class,
\Spatie\CraftMailcoach\Events\MailcoachSent::class,
function ($event) {
// $event->mailcoachMessage contains sent data
}
);
Integration with Craft CMS:
User model to fetch emails dynamically:
$users = User::all();
foreach ($users as $user) {
Mailcoach::send('welcome', [$user->email], ['name' => $user->firstName]);
}
Event::on(
Entry::class,
Entry::EVENT_AFTER_SAVE,
function ($event) {
if ($event->entry->type->handle === 'contact-form') {
Mailcoach::send('thank-you', [$event->entry->contactEmail], []);
}
}
);
Testing:
Mailcoach::fake() for unit tests:
Mailcoach::fake();
Mailcoach::send('test', ['email@example.com']);
Mailcoach::assertSent('test', 1);
Template Paths:
storage/craft-mailcoach/templates/ (not templates/).config/mailcoach.php:
'templatePath' => storage_path('app/mailcoach-templates'),
Recipient Validation:
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
Mailcoach::send('campaign', [$email]);
}
Caching Issues:
./craft clear-caches
config/mailcoach.php for development:
'cacheTemplates' => env('APP_ENV') !== 'local',
PHP 8.2+ Requirements:
Class 'Spatie\CraftMailcoach\...' not found if PHP < 8.2.composer.json for version constraints.Rate Limiting:
try {
Mailcoach::send('campaign', [$email]);
} catch (\Spatie\CraftMailcoach\Exceptions\MailcoachException $e) {
// Retry logic here
}
Enable Logging:
Add to config/mailcoach.php:
'logSentMessages' => true,
Check logs in storage/logs/craft-mailcoach.log.
Inspect Sent Messages:
Use the Mailcoach.Sent event to debug:
Event::on(
\Spatie\CraftMailcoach\Events\MailcoachSent::class,
function ($event) {
Craft::info('Sent to: ' . $event->mailcoachMessage->recipients[0]);
}
);
Template Debugging:
dump() in templates to inspect variables:
{{ dump(data) }}
config/general.php:
'devMode' => true,
Custom Mailcoach Messages:
Extend the MailcoachMessage class to add metadata:
namespace App\Services;
use Spatie\CraftMailcoach\MailcoachMessage;
class CustomMailcoachMessage extends MailcoachMessage
{
public function __construct(string $campaign, array $recipients, array $data = [], array $options = [])
{
parent::__construct($campaign, $recipients, $data, $options);
$this->customField = 'value';
}
}
Use via:
Mailcoach::send(new CustomMailcoachMessage('campaign', ['email@example.com']));
Override Mailcoach Service:
Bind a custom service in config/app.php:
'services' => [
\Spatie\CraftMailcoach\Facades\Mailcoach::class => \App\Services\CustomMailcoachService::class,
],
Add Custom Fields to Templates:
Extend the template context by modifying the MailcoachService:
namespace App\Services;
use Spatie\CraftMailcoach\MailcoachService;
class CustomMailcoachService extends MailcoachService
{
protected function getTemplateData(): array
{
$data = parent::getTemplateData();
$data['custom'] = 'value';
return $data;
}
}
Queue Mailcoach Jobs: Use Laravel’s queue system to defer sends:
Mailcoach::queue('campaign', ['email@example.com']);
Configure the queue in config/mailcoach.php:
'queue' => 'mailcoach',
How can I help you explore Laravel packages today?