spatie/mailcoach-mailer
Symfony Mailer transport for sending transactional email via Mailcoach. Install with composer, then configure Mailcoach credentials to deliver messages through Mailcoach’s transactional mail feature. Laravel users: see spatie/laravel-mailcoach-mailer.
Installation:
composer require spatie/mailcoach-mailer
For Laravel, use the dedicated package: spatie/laravel-mailcoach-mailer.
Configuration:
Add the transport to your Symfony Mailer configuration (or Laravel's config/mail.php if using the Laravel wrapper):
// config/mail.php (Laravel)
'mailers' => [
'mailcoach' => [
'transport' => 'mailcoach',
'host' => env('MAILCOACH_HOST', 'http://localhost:8000'),
'token' => env('MAILCOACH_TOKEN'),
],
],
First Use Case: Send a test email via Mailcoach:
use Symfony\Component\Mailer\MailerInterface;
$mailer = new Mailer([
new MailcoachTransport('http://localhost:8000', 'your-api-token'),
]);
$email = (new Email())
->to('recipient@example.com')
->subject('Test Email')
->html('<h1>Hello from Mailcoach!</h1>');
$mailer->send($email);
config/mail.php: Mailer configuration (Laravel).MailcoachTransport class: Core transport logic.Transactional Emails: Use Mailcoach for templated emails (e.g., password resets, notifications) while leveraging Mailcoach’s segmentation and analytics.
$mailer->send(
(new Email())
->to('user@example.com')
->subject('Your Invoice')
->html(view('emails.invoice', ['total' => $total]))
);
Dynamic Recipients: Batch-send emails to segmented lists (e.g., newsletters) using Mailcoach’s API:
$mailer->send(
(new Email())
->to(['user1@example.com', 'user2@example.com']) // or use Mailcoach’s list API
->subject('Weekly Digest')
->html(view('emails.digest'))
);
Template Integration: Pre-design emails in Mailcoach’s UI, then reference them by ID:
$mailer->send(
(new Email())
->to('user@example.com')
->subject('Welcome')
->html('<mailcoach-template id="123" />') // Renders Mailcoach template
);
Laravel Notifications:
Extend Mailable to use Mailcoach:
class WelcomeMail extends Mailable {
public function build() {
return $this->subject('Welcome')
->withMailcoachTemplate(123)
->view('emails.welcome');
}
}
Queueing: Use Laravel queues to defer Mailcoach sends:
Mail::to('user@example.com')->mailcoach()->queue(new WelcomeMail());
Event Listeners:
Trigger Mailcoach emails on model events (e.g., created):
User::created(function ($user) {
Mail::to($user->email)->mailcoach()->send(new WelcomeMail());
});
API Integration: Fetch Mailcoach campaigns dynamically:
$client = new MailcoachClient('http://mailcoach:token@localhost:8000');
$campaign = $client->campaigns()->find(1);
Authentication:
MAILCOACH_TOKEN is correct and has permissions for the API endpoint.Template Rendering:
<mailcoach-template id="..." />) must be defined in the Mailcoach UI first.$email->html(view('fallback.email'));
Rate Limiting:
php artisan queue:work --sleep=3 --tries=3
CORS Issues:
Debugging:
$mailer = new Mailer([new MailcoachTransport(..., true)]);
Environment-Specific Config:
Use Laravel’s .env to switch between local/staging/prod Mailcoach instances:
MAILCOACH_HOST=mailcoach.staging.app
MAILCOACH_TOKEN=staging_token_here
Testing:
config(['mail.mailers.mailcoach.options.test_mode' => true]);
$this->partialMock(MailcoachTransport::class, [], function ($mock) {
$mock->method('send')->willReturn(true);
});
Performance:
Extensions:
Email object:
$email->getHeaders()->addTextHeader('X-Custom-Header', 'value');
Fallback Transport: Combine Mailcoach with another transport (e.g., SMTP) for redundancy:
$mailer = new Mailer([
new MailcoachTransport(...),
new SmtpTransport(...),
]);
How can I help you explore Laravel packages today?