PHP code example of spatie / mailcoach-sdk-php

1. Go to this page and download the library: Download spatie/mailcoach-sdk-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

spatie / mailcoach-sdk-php example snippets


$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('[email protected]');

// sending a campaign
$campaign->send();

// 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())

use Spatie\MailcoachSdk\Mailcoach;

$mailcoach = new Mailcoach('<your-api-key>', '<your-mailcoach-api-endpoint>')

$subscribers = $mailcoach->subscribers('<email-list-uuid');

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

$emailLists = $mailcoach->emailLists();

$emailList = $mailcoach->emailList('<uuid-of-email-list>');

$mailcoach->createEmailList(['name' => 'My new email list']);

$emailList->name;
$emailList->uuid;
// ...

$emailList->name = 'Updated name';
$emailList->save();

$emailList->delete();

$subscribers = $mailcoach
   ->emailList('<uuid-of-email-list>')
   ->subscribers();

$subscribers = $mailcoach
   ->emailList('<uuid-of-email-list>')
   ->subscribers(['email' => 'gmail.com']);

$subscribers = $mailcoach->subscribers('<uuid-of-email-list>', $optionalFilters);

// returns instance of Spatie\MailcoachSdk\Resources\Subscriber
// or null if the subscriber does not exist.

$subscriber = $emaillist->subscriber('[email protected]');

$subscriber = $mailcoach->subscriber('<subscriber-uuid>');

$subscriber = $mailcoach->createSubscriber('<email-list-uuid>', [
    'email' => '[email protected]',
]);

$subscriber->firstName;
$subscriber->email;
// ...

$subscriber->firstName = 'Updated name';
$subscriber->save();

$subscriber->confirm();
$subscriber->unsubscribe();
$subscriber->resubscribe();
$subscriber->delete();
$subscriber->addTags(['abandoned-cart', 'product-updates']);
$subscriber->removeTags(['abandoned-cart', 'product-updates']);

$campaigns = $mailcoach->campaigns();

$campaign = $mailcoach->campaign('<campaign-uuid>');

$campaign = $this->createCampaign([
   'name' => 'My new campaign',
   'subject' => 'Here is some fantastic content for you',
   'email_list_uuid' => '<email-list-uuid>',
   
   // optionally, you can specify the uuid of a template
   'template_uuid' => '<template-uuid>',
   
   // if that template has field, you can pass the values
   // in the `fields` array. If you use the markdown editor,
   // we'll automatically handle any passed markdown
   'fields' => [
        'title' => 'Content for the title place holder',
        'content' => '# My title',
    ],    
]);

$campaign->name;
$campaign->subject;
// ...

$campaign->name = 'Campaign';
$campaign->save();

// sending a test to a single person
$campaign->sendTest('[email protected]');

// sending a test to multiple persons
$campaign->sendTest(['[email protected]', '[email protected]']);

$campaign->send();

$campaign->delete();

composer test
bash
composer