MailCampaigns API client

2.0.0 2023-07-26 09:50 UTC



Packagist PHP Version Support GitHub

MailCampaigns PHP API client

The MailCampaigns PHP Api client provides an easy way to communicate with the MailCampaigns RESTful API (v3).


Install with Composer:

composer require mailcampaigns/api-client


You'll need the Api url, client key and secret which can be obtained by logging in to your MailCampaigns account (on the Custom Connector page).

Note: This section will be updated soon!

use MailCampaigns\ApiClient\ApiClient;
use MailCampaigns\ApiClient\Entity\Customer;

$apiClient = ApiClient::create('<url>', '<client_key>', '<client_secret>');
$customerApi = $apiClient->getCustomerApi();

// Example 1 - Retrieve a customer by id:
$customer = $customerApi->getById(1234);

// Example 2 - Retrieve first three pages of customers:
for ($page = 1; $page <= 3; $page++) {
    $customers = $customerApi->getCollection($page);

    /** @var Customer $customer */
    foreach ($customers as $customer) {
        // Use customer as an entity (object):
        print $customer->getEmail() . PHP_EOL;

        // Output customer as an array.

// Example 3 - Create a new customer:
    (new Customer)

// Example 4 - Update a customer:

// Example 5 - Get customer by customer reference:
$customer = $customerApi->getByCustomerRef('CUST-8539');

// Example 6 - Get customer by email address:
$customer = $customerApi->getByEmail('');

// Example 7 - Delete a customer by id.

Using custom fields:

use MailCampaigns\ApiClient\ApiClient;
use MailCampaigns\ApiClient\Entity\Customer;
use MailCampaigns\ApiClient\Entity\CustomerCustomField;

$apiClient = ApiClient::create('<url>', '<client_key>', '<client_secret>');

$customerCustomFieldApi = $apiClient->getCustomerCustomFieldApi();

// Example 1: Add a new custom field to an existing customer.
$customer = (new Customer)->setCustomerId(6); // Or load an actual customer first.

$customField = (new CustomerCustomField)
    ->setValue('Waarde voor extra veld 2.')

$createdCustomField = $customerCustomFieldApi->create($customField);

// The created custom field will contain the generated id.
print $createdCustomField->getCustomFieldId();

// Example 2: Update an existing custom field.
    ->setUpdatedAt(new DateTime())
    ->setValue('Een aangepaste waarde.');

$updatedCustomField = $customerCustomFieldApi->update($customField);

// Example 4: Delete a custom field by its id.