PHP code example of millionsend / millionsend-php

1. Go to this page and download the library: Download millionsend/millionsend-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/ */

    

millionsend / millionsend-php example snippets


use MillionSend\MillionSend;
use MillionSend\Exceptions\ErrorException;

$ms = MillionSend::client('ms_123', 'https://mail.acme.dev');

try {
    $email = $ms->emails->send([
        'from' => 'Acme <[email protected]>',
        'to' => '[email protected]',
        'subject' => 'Hello from MillionSend',
        'html' => '<strong>It works!</strong>',
    ]);

    echo "sent {$email['id']}\n";
} catch (ErrorException $e) {
    echo "{$e->getErrorName()}: {$e->getErrorMessage()}\n";
}

MillionSend::client(
    apiKey: 'ms_123',                 // falls back to env MILLIONSEND_API_KEY; missing → throws
    baseUrl: 'https://mail.acme.dev', // falls back to env MILLIONSEND_BASE_URL, then http://localhost:3001
    options: [
        'client' => $guzzle,          // inject a GuzzleHttp\ClientInterface (proxies, tests)
        'userAgent' => 'acme-app/2.1', // suffix appended after the SDK's own token
        'timeout' => 30.0,            // total request timeout, seconds
        'connectTimeout' => 10.0,     // connection timeout, seconds
    ],
);

try {
    $email = $ms->emails->get($id);
} catch (ErrorException $e) {
    if ($e->getErrorName() === 'not_found') { /* … */ }
    // $e->getStatusCode(); // int, or null for transport failures
}

$ms->emails->send($payload, ['idempotencyKey' => $key]);   // POST /emails
$ms->emails->get($id);                                      // GET /emails/:id
$ms->emails->cancel($id);                                   // POST /emails/:id/cancel (scheduled only)
$ms->batch->send([$payloadA, $payloadB], ['idempotencyKey' => $key]); // up to 100

$ms->emails->send([
    'from' => 'Acme <[email protected]>',
    'to' => ['[email protected]', '[email protected]'],
    'subject' => 'Launch',
    'html' => '<p>Hi</p>',
    'replyTo' => '[email protected]',
    'tags' => [['name' => 'category', 'value' => 'launch']],
]);

$ms->contacts->create([
    'email' => '[email protected]',
    'firstName' => 'Ada',
    'properties' => ['plan' => 'pro'],
]);
$ms->contacts->get(['email' => '[email protected]']);  // by id or email (email wins)
$ms->contacts->get($contactId);                   // a bare string id works too
$ms->contacts->update(['id' => $contactId, 'unsubscribed' => true, 'firstName' => null]); // null clears
$ms->contacts->remove(['email' => '[email protected]']);
$ms->contacts->list(['limit' => 50]);

// Topic subscriptions (granular unsubscribe)
$ms->contacts->topics->update([
    'email' => '[email protected]',
    'topics' => [['id' => $topicId, 'subscription' => 'opt_out']],
]);
// $ms->contacts->updateTopics([...]) is an equivalent alias.

$ms->topics->create(['name' => 'Product updates', 'defaultSubscription' => 'opt_in']);
$ms->topics->get($id);
$ms->topics->list();     // bare { data } — topics are unpaginated
$ms->topics->remove($id);

$broadcast = $ms->broadcasts->create([
    'from' => 'Acme <[email protected]>',
    'subject' => 'Launch',
    'html' => '<p>Hi {{{FIRST_NAME|there}}}</p>',
    'segmentId' => $segmentId, // optional
]);
$ms->broadcasts->list();
$ms->broadcasts->get($id);
$ms->broadcasts->update($id, ['subject' => 'Launch 🚀']);          // draft only
$ms->broadcasts->send($id, ['scheduledAt' => '2026-09-01T09:00:00Z']); // omit to send now
$ms->broadcasts->cancel($id);                                       // scheduled only
$ms->broadcasts->remove($id);                                       // draft only

$ms->segments->create([
    'name' => 'Pro plan',
    'filter' => [
        'match' => 'all',
        'conditions' => [['field' => 'property:plan', 'op' => 'equals', 'value' => 'pro']],
    ],
]);
$ms->segments->get($id);   // 
bash
composer