Building a campaign step by step
Setting up a campaign through the API takes eight steps, always in the same order. The last one, the send, is done in the interface: the API prepares the campaign and sends the test, but never triggers the send to the recipients.
The sequence of calls
| Step | Method | What to keep |
|---|---|---|
| 1. Check access and credits | account_info | credits, and the IP seen by the server in case of Error auth ip |
| 2. Create the campaign | campaign_create | campaign_id, to keep for all the following steps |
| 3. Load the message | campaign_set_message | tracked_links, the number of tracked links |
| 4. Add the recipients | campaign_add_recipients or inject_segmentlist_2_campaign | inserted and ignored for each batch |
| 5. Wait for the cleaning to finish | campagne_import_status | the final number of recipients |
| 6. Read the campaign back | campaign_get | subject, sender, preheader, number of recipients |
| 7. Send the test | campaign_send_test | credits_used |
| 8. Send or schedule | in the interface |
A few points are worth planning for in your code from the start.
At step 4, pick the right method. Contacts coming from another tool (CRM, business database) go through campaign_add_recipients, in a loop, in batches of 255. Contacts already stored in a platform list go through inject_segmentlist_2_campaign, in a single call.
Plan an automatic retry. While the platform is cleaning a batch’s recipients, adding the next batch is refused with Error a cleaning of the recipients is running on this campaign, try again in a few seconds. This is not a failure: simply send the same batch again a few seconds later.
Do not chain steps 4 and 6 too quickly. The total figure from campaign_add_recipients is counted before the cleaning. The real number of recipients, once unsubscribed, hard-bounced and malformed addresses are removed, is only known at the end of step 5. mails_retires_imports then details the removed addresses.
The test costs one credit per address. Limit it to the people who approve the campaign, 10 addresses at most per call.
A complete PHP script
This script runs steps 1 to 7. It assumes the message is in a newsletter.html file and the contacts are already in a PHP array. Contact keys must be those accepted by campaign_add_recipients: email, nom (last name), prenom (first name), perso1 to perso24.
// Generic REST API call: returns the response decoded as an array
function mdworks($method, $params = array())
{
$params['api_key'] = 'YOUR_API_KEY';
$ch = curl_init('https://www.mdworks.fr/api_http/' . $method);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params, JSON_UNESCAPED_UNICODE));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = json_decode(curl_exec($ch), true);
curl_close($ch);
if (!is_array($response)) {
die("Unreadable response for $method\n");
}
return $response;
}
function stop_on_error($response)
{
if (isset($response['error'])) {
die('Error: ' . $response['message'] . "\n");
}
}
// 1. Check access and credits
$account = mdworks('account_info');
stop_on_error($account);
echo 'Available credits: ' . $account['credits'] . "\n";
// 2. Create the draft campaign
$r = mdworks('campaign_create', array(
'reference' => 'November newsletter',
'subject' => "What's new in November",
'from_name' => 'Example Team',
'from_email' => 'contact@example.com',
'reply_to' => 'contact@example.com',
'preheader' => 'Three new things to discover',
'language' => 'UK'
));
stop_on_error($r);
$campaign_id = $r['campaign_id'];
// 3. Load the HTML message (2 MB at most, images over https)
$r = mdworks('campaign_set_message', array(
'campaign_id' => $campaign_id,
'html' => file_get_contents('newsletter.html')
));
stop_on_error($r);
echo 'Tracked links: ' . $r['tracked_links'] . "\n";
// 4. Add recipients in batches of 255, retrying during the cleaning
$contacts = array(
array('email' => 'mary.smith@example.com', 'nom' => 'Smith', 'prenom' => 'Mary'),
array('email' => 'john.brown@example.com', 'nom' => 'Brown', 'prenom' => 'John')
// ...
);
foreach (array_chunk($contacts, 255) as $batch) {
for ($attempt = 1; $attempt <= 6; $attempt++) {
$r = mdworks('campaign_add_recipients', array(
'campaign_id' => $campaign_id,
'tab_insert' => $batch
));
$cleaning_running = isset($r['error'])
&& strpos($r['message'], 'a cleaning of the recipients is running') !== false;
if (!$cleaning_running) {
break;
}
sleep(15);
}
stop_on_error($r);
echo 'Batch: ' . $r['inserted'] . ' added, ' . $r['ignored'] . " ignored\n";
}
// 5. Wait for the cleaning to finish
do {
sleep(10);
$r = mdworks('campagne_import_status', array('campaign_id' => $campaign_id));
stop_on_error($r);
} while ($r['result'] === 'running');
echo 'Recipients after cleaning: ' . $r['result'] . "\n";
// 6. Read the campaign back
$campaign = mdworks('campaign_get', array('campaign_id' => $campaign_id));
stop_on_error($campaign);
echo 'Subject: ' . $campaign['subject'] . ', sender: ' . $campaign['from_email'] . "\n";
// 7. Send the test to the approval team (1 credit per address)
$r = mdworks('campaign_send_test', array(
'campaign_id' => $campaign_id,
'emails' => array('approval@example.com')
));
stop_on_error($r);
echo 'Test sent to ' . $r['sent_to'] . " address(es)\n";
// 8. The send or scheduling is then done in the interface
Once the test is approved, find the campaign in the interface under the reference chosen at step 2, and send or schedule it. If the test reveals a mistake, fix it with campaign_update or a new campaign_set_message, then send another test: the campaign is still a draft.
Starting over
A test campaign you no longer need is deleted with campaign_delete, as long as it is a draft and no import is running. Deletion takes its recipients with it and cannot be undone.
To find a draft created during a previous run, campaign_list with "status": "draft" lists the account’s drafts, from the most recent to the oldest.