Using an SDK
When using PHP, the easiest way to get started with using the API would be to use one of our software development kit (SDK) packages:
Using these packages will help you can manage email lists, subscribers, and campaigns.
Here are a few examples:
$mailcoach = new \Spatie\MailcoachSdk\Mailcoach('<api-key>', '<mailcoach-api-endpoint>')
// creating a campaign
$campaign = $mailcoach->createCampaign([
'email_list_uuid' => 'use-a-real-email-list-uuid-here',
'name' => 'My new campaign'
'fields' => [
'title' => 'The title on top of the newsletter',
'content' => '# Welcome to my newsletter'
],
]);
// sending a test of the campaign to the given email address
$campaign->sendTest('john@example.com');
// sending a campaign
$campaign->send();
By default, Mailcoach’s endpoints will be paginated with a limit of 1000. The package makes it easy to work with paginated resources. Just call ->next()
to get the next page.
// listing all subscribers of a list
$subscribers = $mailcoach->emailList('use-a-real-email-list-uuid-here')->subscribers();
do {
foreach($subscribers as $subscriber) {
echo $subscriber->email;
}
} while($subscribers = $subscribers->next())
You will find more examples in the readme of the SDK at GitHub.