Constant Contact V3: LIVE SEND - Schedule a newsletter for a named list
See more Constant Contact Examples
LIVE SEND. Scheduling this campaign delivers email to eligible contacts on the selected list at the specified time. Use the reviewer test to check the email first. Select the intended newsletter and list in the same account. The example accepts a primary draft, replaces its entire audience with one active email list, and schedules it. List lookup requires contact_data. Editing and scheduling require campaign_data and account privileges to edit and send.
Enter a future ISO 8601 timestamp with a UTC offset or Z. To cancel before delivery starts, keep the same activity selection, change action to unschedule, and run again. Cancellation removes the schedule and keeps the audience assignment. If the scheduling response is lost, check the activity’s schedule before retrying.
Authorize the application using the same account and credential store.
Constant Contact developer guide
Chilkat Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
var success = false;
// LIVE SEND: Scheduling this activity delivers email to the selected contact list.
// Assumes Chilkat is unlocked. See Global Unlock Sample.
final vault = CkSecrets();
vault.location = 'local_manager';
vault.autoSplit = true;
final entry = CkJsonObject();
entry.updateString('appName', 'ConstantContactV3');
entry.updateString('service', 'OAuth2');
entry.updateString('username', 'my-account');
final tokens = CkJsonObject();
try {
vault.getSecretJson(entry, tokens);
} on ChilkatException {
print('Run the desktop authorization example using this account and store.');
return;
}
if (tokens.jsonTypeOf('access_token') != 1) {
print('The saved authorization has no access token.');
return;
}
final http = CkHttp();
http.requireSslCertVerify = true;
http.authToken = tokens.stringOf('access_token');
final resp = CkHttpResponse();
// Use the selection file from the draft or campaign lookup example for this account.
final selection = CkJsonObject();
success = true;
try {
selection.loadFile('newsletter-selection.json');
} on ChilkatException {
success = false;
}
final activityId = selection.stringOf('campaign_activity_id');
if ((!success) || (activityId == '')) {
print('Select a campaign and save its primary activity ID first.');
return;
}
http.setUrlVar('activity', activityId);
final action = 'schedule';
if (action == 'unschedule') {
success = true;
try {
http.httpNoBody('DELETE', 'https://api.cc.email/v3/emails/activities/{\$activity}/schedules', resp);
} on ChilkatException {
success = false;
}
if ((!success) && (resp.statusCode <= 0)) {
print('Remove schedule: HTTP request failed.');
return;
}
if ((!success) || (resp.statusCode != 204)) {
print('Remove schedule: HTTP ${resp.statusCode} ${resp.bodyStr}');
return;
}
print('Schedule removed for activity $activityId');
return;
}
if (action != 'schedule') {
print('Choose schedule or unschedule.');
return;
}
// Replace the placeholder with the intended future delivery time, e.g. 2030-05-15T09:00:00-05:00.
final deliveryTime = 'ENTER_FUTURE_TIMESTAMP_WITH_OFFSET';
final delivery = CkDateTime();
final now = CkDateTime();
now.setFromCurrentSystemTime();
success = true;
try {
delivery.setFromTimestamp(deliveryTime);
} on ChilkatException {
success = false;
}
if ((!success) || (delivery.diffSeconds(now) <= 0)) {
print('Enter a valid future delivery time with a UTC offset or Z.');
return;
}
// Use the full display name. AddParam encodes spaces and punctuation.
final listName = 'Workshop announcements';
final query = CkHttpRequest();
query.httpVerb = 'GET';
query.addParam('name', listName);
query.addParam('status', 'active');
query.addParam('include_membership_count', 'active');
success = true;
try {
http.httpReq('https://api.cc.email/v3/contact_lists', query, resp);
} on ChilkatException {
success = false;
}
if ((!success) && (resp.statusCode <= 0)) {
print('Look up list: HTTP request failed.');
return;
}
if ((!success) || (resp.statusCode != 200)) {
print('Look up list: HTTP ${resp.statusCode} ${resp.bodyStr}');
return;
}
final matches = CkJsonObject();
try {
matches.load(resp.bodyStr);
} on ChilkatException {
print('Expected a JSON object in the response.');
return;
}
if (matches.sizeOfArray('lists') == 0) {
print('No active email list named $listName');
return;
}
if ((matches.sizeOfArray('lists') != 1) || (!matches.stringOfEquals('lists[0].name', listName, true))) {
print('Expected one exact list name.');
return;
}
final listId = matches.stringOf('lists[0].list_id');
if (listId == '') {
print('Missing list ID.');
return;
}
print('$listName | List ID: $listId | Mailable contacts: ${matches.intOf('lists[0].membership_count')}');
// PUT replaces the activity. Read it first to preserve the subject, sender and other settings.
// Constant Contact retains omitted subresources such as html_content and the physical footer.
success = true;
try {
http.httpNoBody('GET', 'https://api.cc.email/v3/emails/activities/{\$activity}', resp);
} on ChilkatException {
success = false;
}
if ((!success) && (resp.statusCode <= 0)) {
print('Read draft: HTTP request failed.');
return;
}
if ((!success) || (resp.statusCode != 200)) {
print('Read draft: HTTP ${resp.statusCode} ${resp.bodyStr}');
return;
}
final draft = CkJsonObject();
try {
draft.load(resp.bodyStr);
} on ChilkatException {
print('Expected a JSON object in the response.');
return;
}
if ((!draft.stringOfEquals('role', 'primary_email', true)) || (!draft.stringOfEquals('current_status', 'DRAFT', true))) {
print('Select a primary activity in DRAFT status.');
return;
}
print('LIVE SEND: ${draft.stringOf('subject')} | Activity: $activityId | List: $listName | Delivery: $deliveryTime');
draft.updateNewArray('contact_list_ids');
draft.updateString('contact_list_ids[0]', listId);
draft.updateNewArray('segment_ids');
success = true;
try {
http.httpJson('PUT', 'https://api.cc.email/v3/emails/activities/{\$activity}', draft, 'application/json', resp);
} on ChilkatException {
success = false;
}
if ((!success) && (resp.statusCode <= 0)) {
print('Assign audience: HTTP request failed.');
return;
}
if ((!success) || (resp.statusCode != 200)) {
print('Assign audience: HTTP ${resp.statusCode} ${resp.bodyStr}');
return;
}
final updated = CkJsonObject();
try {
updated.load(resp.bodyStr);
} on ChilkatException {
print('Expected a JSON object in the response.');
return;
}
if ((updated.sizeOfArray('contact_list_ids') != 1) || ((!updated.stringOfEquals('contact_list_ids[0]', listId, true)) || (updated.sizeOfArray('segment_ids') > 0))) {
print('The saved audience did not match the requested list. Scheduling stopped.');
return;
}
final schedule = CkJsonObject();
schedule.updateString('scheduled_date', delivery.getAsTimestamp(false));
success = true;
try {
http.httpJson('POST', 'https://api.cc.email/v3/emails/activities/{\$activity}/schedules', schedule, 'application/json', resp);
} on ChilkatException {
success = false;
}
if ((!success) || ((resp.statusCode < 200) || (resp.statusCode >= 300))) {
print('Schedule not confirmed. HTTP ${resp.statusCode}');
print('The audience was saved. Check the schedule before retrying.');
return;
}
print('Live delivery scheduled for ${schedule.stringOf('scheduled_date')} to $listName');
}