Using an SDK

On this page:

Use one of our SDK packages to manage email lists, subscribers, and campaigns from PHP:

Follow the package’s installation instructions and create an API token with All API endpoints permission. Pass your team’s API URL, including /api, to the PHP SDK.

This example creates an HTML campaign draft. Replace the token, team domain, and email list UUID with your own values. It does not send email.

use Spatie\MailcoachSdk\Mailcoach;

$mailcoach = new Mailcoach(
    'your-api-token',
    'https://<your-team>.mailcoach.app/api',
);

$campaign = $mailcoach->createCampaign([
    'email_list_uuid' => 'a69075c6-ff5c-4b6a-b217-12026cb72e4f',
    'name' => 'My new campaign',
    'subject' => 'Welcome to our newsletter',
    'html' => '<p>Welcome to our newsletter.</p>',
]);

To use template fields or Markdown instead, see creating a campaign with a template.

Send a test

After reviewing the draft, you can optionally send a test to your own address. This sends an email:

$campaign->sendTest('your-address@example.com');

Send to subscribers

Only run this separate step when the campaign is ready. It sends the campaign to its audience:

$campaign->send();

Pagination

Cloud list endpoints normally return up to 1,000 records per page. Call next() to retrieve the next page; it returns null after the last page.

$subscribers = $mailcoach->emailList('a69075c6-ff5c-4b6a-b217-12026cb72e4f')->subscribers();

do {
    foreach ($subscribers as $subscriber) {
        echo $subscriber->email;
    }
} while ($subscribers = $subscribers->next());

See the PHP SDK readme for more examples and the generated API reference for endpoint schemas.