spatie/mailcoach-cli
Command-line tool for Mailcoach. Log in with your instance URL and API token, then run autogenerated commands for every Mailcoach API endpoint (lists, campaigns, etc.). Includes clear-cache/logout and an AI agent skill for Boost/skills.sh.
Installation:
composer global require spatie/mailcoach-cli
Ensure $HOME/.composer/vendor/bin is in your PATH or use ./vendor/bin/mailcoach if installed locally.
First Authentication:
mailcoach login
https://your-instance.mailcoach.app).~/.config/spatie/mailcoach-cli/config.json.First Use Case: List all email lists to verify connectivity:
mailcoach list-email-lists
Output will show a JSON array of lists (e.g., {"data":[{"id":1,"name":"Newsletter"}]}).
Campaign Management:
mailcoach create-campaign --name="Spring Sale" --subject="20% Off!"
mailcoach schedule-campaign --campaign-id=1 --scheduled-at="2026-05-15T10:00:00Z"
mailcoach send-test-email --campaign-id=1 --email="test@example.com"
Automation with Laravel:
artisan commands (e.g., php artisan mailcoach:sync-campaigns).// app/Console/Commands/SyncSubscribers.php
public function handle() {
$subscribers = shell_exec('mailcoach list-subscribers --email-list-id=1');
// Parse JSON and update local DB...
}
CI/CD Integration:
# .github/workflows/deploy.yml
steps:
- run: mailcoach clear-cache && mailcoach list-campaigns
Agent-Assisted Workflows:
/mailcoach list-campaigns --status=scheduled
php artisan boost:run "mailcoach create-campaign --name='Weekly Digest'"
Laravel Service Provider: Bind the CLI to a service container for programmatic access:
// app/Providers/AppServiceProvider.php
public function register() {
$this->app->singleton('mailcoach', function () {
return new \Spatie\MailcoachCli\MailcoachCli();
});
}
Use in controllers:
$campaigns = $this->app['mailcoach']->run('list-campaigns');
Environment Variables:
Override credentials via .env:
MAILCOACH_URL=https://your-instance.mailcoach.app
MAILCOACH_TOKEN=your_api_token_here
Then run:
mailcoach --env=.env list-email-lists
API Spec Updates: Refresh the cached OpenAPI spec manually:
mailcoach clear-cache
Or automate via a cron job:
0 3 * * * mailcoach clear-cache >> /dev/null 2>&1
Token Permissions:
list-* commands first.mailcoach --verbose list-email-lists
Look for 403 Forbidden errors in the output.Rate Limiting:
$cacheKey = "mailcoach_campaigns_{$campaignId}";
return Cache::remember($cacheKey, now()->addMinutes(5), function () use ($campaignId) {
return shell_exec("mailcoach show-campaign --campaign-id={$campaignId}");
});
ID Mismatches:
--id flags (e.g., --campaign-id=1) instead of names or slugs, as these are immutable.Local Development:
.env file for local testing to avoid hardcoding credentials:
mailcoach --env=.env.local list-campaigns
Verbose Mode:
mailcoach --verbose create-campaign --name="Test"
Reveals raw API requests/responses (e.g., headers, payloads).
Dry Runs: Simulate API calls without executing:
mailcoach --dry-run schedule-campaign --campaign-id=1
Logging:
Enable debug logging in ~/.config/spatie/mailcoach-cli/config.json:
{
"debug": true,
"log_file": "/tmp/mailcoach-cli.log"
}
Custom Commands: Extend the CLI by creating a plugin. Example:
# Create a new command file
mkdir -p ~/.config/spatie/mailcoach-cli/commands
touch ~/.config/spatie/mailcoach-cli/commands/export-subscribers.php
Add PHP code to define new commands (see Spatie CLI docs).
Webhook Triggers:
Use the CLI in Laravel events (e.g., sent email events) to update Mailcoach:
// app/Events/EmailSent.php
public function handle() {
shell_exec("mailcoach update-subscriber --email-list-id=1 --email={$this->email}");
}
GitHub Actions: Automate deployments with Mailcoach:
- name: Deploy Campaign
run: |
mailcoach create-campaign --name="${{ github.event.inputs.name }}"
mailcoach schedule-campaign --campaign-id=$(mailcoach list-campaigns | jq -r '.data[-1].id') --scheduled-at="${{ github.event.inputs.date }}"
Cache Directory:
The CLI caches API specs in ~/.cache/spatie/mailcoach-cli. Clear it if commands fail unexpectedly:
rm -rf ~/.cache/spatie/mailcoach-cli
URL Formatting:
Ensure the instance URL ends with / (e.g., https://example.com/). Trailing slashes are required for API routes.
Token Rotation: Rotate tokens via:
mailcoach logout && mailcoach login
The CLI does not support token refresh; manual re-authentication is required.
How can I help you explore Laravel packages today?