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 Objective-C Downloads
#import <CkoSecrets.h>
#import <CkoJsonObject.h>
#import <CkoHttp.h>
#import <CkoHttpResponse.h>
#import <NSString.h>
#import <CkoDateTime.h>
#import <CkoHttpRequest.h>
BOOL success = NO;
// LIVE SEND: Scheduling this activity delivers email to the selected contact list.
// Assumes Chilkat is unlocked. See Global Unlock Sample.
CkoSecrets *vault = [[CkoSecrets alloc] init];
vault.Location = @"local_manager";
vault.AutoSplit = YES;
CkoJsonObject *entry = [[CkoJsonObject alloc] init];
[entry UpdateString: @"appName" value: @"ConstantContactV3"];
[entry UpdateString: @"service" value: @"OAuth2"];
[entry UpdateString: @"username" value: @"my-account"];
CkoJsonObject *tokens = [[CkoJsonObject alloc] init];
BOOL loaded = [vault GetSecretJson: entry jsonOut: tokens];
if (loaded != YES) {
NSLog(@"%@",@"Run the desktop authorization example using this account and store.");
return;
}
if ([[tokens JsonTypeOf: @"access_token"] intValue] != 1) {
NSLog(@"%@",@"The saved authorization has no access token.");
return;
}
CkoHttp *http = [[CkoHttp alloc] init];
http.RequireSslCertVerify = YES;
http.AuthToken = [tokens StringOf: @"access_token"];
CkoHttpResponse *resp = [[CkoHttpResponse alloc] init];
// Use the selection file from the draft or campaign lookup example for this account.
CkoJsonObject *selection = [[CkoJsonObject alloc] init];
success = [selection LoadFile: @"newsletter-selection.json"];
NSString *activityId = [selection StringOf: @"campaign_activity_id"];
if ((success != YES) || (activityId == @"")) {
NSLog(@"%@",@"Select a campaign and save its primary activity ID first.");
return;
}
[http SetUrlVar: @"activity" value: activityId];
NSString *action = @"schedule";
if (action == @"unschedule") {
success = [http HttpNoBody: @"DELETE" url: @"https://api.cc.email/v3/emails/activities/{$activity}/schedules" response: resp];
if ((success != YES) && ([resp.StatusCode intValue] <= 0)) {
NSLog(@"%@",@"Remove schedule: HTTP request failed.");
return;
}
if ((success != YES) || ([resp.StatusCode intValue] != 204)) {
NSLog(@"%@%d%@%@",@"Remove schedule: HTTP ",[resp.StatusCode intValue],@" ",resp.BodyStr);
return;
}
NSLog(@"%@%@",@"Schedule removed for activity ",activityId);
return;
}
if (action != @"schedule") {
NSLog(@"%@",@"Choose schedule or unschedule.");
return;
}
// Replace the placeholder with the intended future delivery time, e.g. 2030-05-15T09:00:00-05:00.
NSString *deliveryTime = @"ENTER_FUTURE_TIMESTAMP_WITH_OFFSET";
CkoDateTime *delivery = [[CkoDateTime alloc] init];
CkoDateTime *now = [[CkoDateTime alloc] init];
[now SetFromCurrentSystemTime];
success = [delivery SetFromTimestamp: deliveryTime];
if ((success != YES) || ([[delivery DiffSeconds: now] intValue] <= 0)) {
NSLog(@"%@",@"Enter a valid future delivery time with a UTC offset or Z.");
return;
}
// Use the full display name. AddParam encodes spaces and punctuation.
NSString *listName = @"Workshop announcements";
CkoHttpRequest *query = [[CkoHttpRequest alloc] init];
query.HttpVerb = @"GET";
[query AddParam: @"name" value: listName];
[query AddParam: @"status" value: @"active"];
[query AddParam: @"include_membership_count" value: @"active"];
success = [http HttpReq: @"https://api.cc.email/v3/contact_lists" request: query response: resp];
if ((success != YES) && ([resp.StatusCode intValue] <= 0)) {
NSLog(@"%@",@"Look up list: HTTP request failed.");
return;
}
if ((success != YES) || ([resp.StatusCode intValue] != 200)) {
NSLog(@"%@%d%@%@",@"Look up list: HTTP ",[resp.StatusCode intValue],@" ",resp.BodyStr);
return;
}
CkoJsonObject *matches = [[CkoJsonObject alloc] init];
success = [matches Load: resp.BodyStr];
if (success != YES) {
NSLog(@"%@",@"Expected a JSON object in the response.");
return;
}
if ([[matches SizeOfArray: @"lists"] intValue] == 0) {
NSLog(@"%@%@",@"No active email list named ",listName);
return;
}
if (([[matches SizeOfArray: @"lists"] intValue] != 1) || ([matches StringOfEquals: @"lists[0].name" value: listName caseSensitive: YES] != YES)) {
NSLog(@"%@",@"Expected one exact list name.");
return;
}
NSString *listId = [matches StringOf: @"lists[0].list_id"];
if (listId == @"") {
NSLog(@"%@",@"Missing list ID.");
return;
}
NSLog(@"%@%@%@%@%d",listName,@" | List ID: ",listId,@" | Mailable contacts: ",[[matches IntOf: @"lists[0].membership_count"] intValue]);
// 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 = [http HttpNoBody: @"GET" url: @"https://api.cc.email/v3/emails/activities/{$activity}" response: resp];
if ((success != YES) && ([resp.StatusCode intValue] <= 0)) {
NSLog(@"%@",@"Read draft: HTTP request failed.");
return;
}
if ((success != YES) || ([resp.StatusCode intValue] != 200)) {
NSLog(@"%@%d%@%@",@"Read draft: HTTP ",[resp.StatusCode intValue],@" ",resp.BodyStr);
return;
}
CkoJsonObject *draft = [[CkoJsonObject alloc] init];
success = [draft Load: resp.BodyStr];
if (success != YES) {
NSLog(@"%@",@"Expected a JSON object in the response.");
return;
}
if (([draft StringOfEquals: @"role" value: @"primary_email" caseSensitive: YES] != YES) || ([draft StringOfEquals: @"current_status" value: @"DRAFT" caseSensitive: YES] != YES)) {
NSLog(@"%@",@"Select a primary activity in DRAFT status.");
return;
}
NSLog(@"%@%@%@%@%@%@%@%@",@"LIVE SEND: ",[draft StringOf: @"subject"],@" | Activity: ",activityId,@" | List: ",listName,@" | Delivery: ",deliveryTime);
[draft UpdateNewArray: @"contact_list_ids"];
[draft UpdateString: @"contact_list_ids[0]" value: listId];
[draft UpdateNewArray: @"segment_ids"];
success = [http HttpJson: @"PUT" url: @"https://api.cc.email/v3/emails/activities/{$activity}" json: draft contentType: @"application/json" response: resp];
if ((success != YES) && ([resp.StatusCode intValue] <= 0)) {
NSLog(@"%@",@"Assign audience: HTTP request failed.");
return;
}
if ((success != YES) || ([resp.StatusCode intValue] != 200)) {
NSLog(@"%@%d%@%@",@"Assign audience: HTTP ",[resp.StatusCode intValue],@" ",resp.BodyStr);
return;
}
CkoJsonObject *updated = [[CkoJsonObject alloc] init];
success = [updated Load: resp.BodyStr];
if (success != YES) {
NSLog(@"%@",@"Expected a JSON object in the response.");
return;
}
if (([[updated SizeOfArray: @"contact_list_ids"] intValue] != 1) || ([updated StringOfEquals: @"contact_list_ids[0]" value: listId caseSensitive: YES] != YES) || ([[updated SizeOfArray: @"segment_ids"] intValue] > 0)) {
NSLog(@"%@",@"The saved audience did not match the requested list. Scheduling stopped.");
return;
}
CkoJsonObject *schedule = [[CkoJsonObject alloc] init];
[schedule UpdateString: @"scheduled_date" value: [delivery GetAsTimestamp: NO]];
success = [http HttpJson: @"POST" url: @"https://api.cc.email/v3/emails/activities/{$activity}/schedules" json: schedule contentType: @"application/json" response: resp];
if ((success != YES) || ([resp.StatusCode intValue] < 200) || ([resp.StatusCode intValue] >= 300)) {
NSLog(@"%@%d",@"Schedule not confirmed. HTTP ",[resp.StatusCode intValue]);
NSLog(@"%@",@"The audience was saved. Check the schedule before retrying.");
return;
}
NSLog(@"%@%@%@%@",@"Live delivery scheduled for ",[schedule StringOf: @"scheduled_date"],@" to ",listName);