Sample code for 30+ languages & platforms
Delphi DLL Requires Chilkat v11.0.0+

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 Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
vault: HCkSecrets;
entry: HCkJsonObject;
tokens: HCkJsonObject;
loaded: Boolean;
http: HCkHttp;
resp: HCkHttpResponse;
selection: HCkJsonObject;
activityId: PWideChar;
action: PWideChar;
deliveryTime: PWideChar;
delivery: HCkDateTime;
now: HCkDateTime;
listName: PWideChar;
query: HCkHttpRequest;
matches: HCkJsonObject;
listId: PWideChar;
draft: HCkJsonObject;
updated: HCkJsonObject;
schedule: HCkJsonObject;

begin
success := False;

//  LIVE SEND: Scheduling this activity delivers email to the selected contact list.
//  Assumes Chilkat is unlocked. See Global Unlock Sample.
vault := CkSecrets_Create();
CkSecrets_putLocation(vault,'local_manager');
CkSecrets_putAutoSplit(vault,True);
entry := CkJsonObject_Create();
CkJsonObject_UpdateString(entry,'appName','ConstantContactV3');
CkJsonObject_UpdateString(entry,'service','OAuth2');
CkJsonObject_UpdateString(entry,'username','my-account');
tokens := CkJsonObject_Create();
loaded := CkSecrets_GetSecretJson(vault,entry,tokens);
if (loaded <> True) then
  begin
    Memo1.Lines.Add('Run the desktop authorization example using this account and store.');
    Exit;
  end;
if (CkJsonObject_JsonTypeOf(tokens,'access_token') <> 1) then
  begin
    Memo1.Lines.Add('The saved authorization has no access token.');
    Exit;
  end;
http := CkHttp_Create();
CkHttp_putRequireSslCertVerify(http,True);
CkHttp_putAuthToken(http,CkJsonObject__stringOf(tokens,'access_token'));
resp := CkHttpResponse_Create();

//  Use the selection file from the draft or campaign lookup example for this account.
selection := CkJsonObject_Create();
success := CkJsonObject_LoadFile(selection,'newsletter-selection.json');
activityId := CkJsonObject__stringOf(selection,'campaign_activity_id');
if ((success <> True) or (activityId = '')) then
  begin
    Memo1.Lines.Add('Select a campaign and save its primary activity ID first.');
    Exit;
  end;
CkHttp_SetUrlVar(http,'activity',activityId);
action := 'schedule';
if (action = 'unschedule') then
  begin
    success := CkHttp_HttpNoBody(http,'DELETE','https://api.cc.email/v3/emails/activities/{$activity}/schedules',resp);
    if ((success <> True) and (CkHttpResponse_getStatusCode(resp) <= 0)) then
      begin
        Memo1.Lines.Add('Remove schedule: HTTP request failed.');
        Exit;
      end;
    if ((success <> True) or (CkHttpResponse_getStatusCode(resp) <> 204)) then
      begin
        Memo1.Lines.Add('Remove schedule: HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)) + ' ' + CkHttpResponse__bodyStr(resp));
        Exit;
      end;
    Memo1.Lines.Add('Schedule removed for activity ' + activityId);
    Exit;
  end;
if (action <> 'schedule') then
  begin
    Memo1.Lines.Add('Choose schedule or unschedule.');
    Exit;
  end;
//  Replace the placeholder with the intended future delivery time, e.g. 2030-05-15T09:00:00-05:00.
deliveryTime := 'ENTER_FUTURE_TIMESTAMP_WITH_OFFSET';
delivery := CkDateTime_Create();
now := CkDateTime_Create();
CkDateTime_SetFromCurrentSystemTime(now);
success := CkDateTime_SetFromTimestamp(delivery,deliveryTime);
if ((success <> True) or (CkDateTime_DiffSeconds(delivery,now) <= 0)) then
  begin
    Memo1.Lines.Add('Enter a valid future delivery time with a UTC offset or Z.');
    Exit;
  end;
//  Use the full display name. AddParam encodes spaces and punctuation.
listName := 'Workshop announcements';
query := CkHttpRequest_Create();
CkHttpRequest_putHttpVerb(query,'GET');
CkHttpRequest_AddParam(query,'name',listName);
CkHttpRequest_AddParam(query,'status','active');
CkHttpRequest_AddParam(query,'include_membership_count','active');
success := CkHttp_HttpReq(http,'https://api.cc.email/v3/contact_lists',query,resp);
if ((success <> True) and (CkHttpResponse_getStatusCode(resp) <= 0)) then
  begin
    Memo1.Lines.Add('Look up list: HTTP request failed.');
    Exit;
  end;
if ((success <> True) or (CkHttpResponse_getStatusCode(resp) <> 200)) then
  begin
    Memo1.Lines.Add('Look up list: HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)) + ' ' + CkHttpResponse__bodyStr(resp));
    Exit;
  end;
matches := CkJsonObject_Create();
success := CkJsonObject_Load(matches,CkHttpResponse__bodyStr(resp));
if (success <> True) then
  begin
    Memo1.Lines.Add('Expected a JSON object in the response.');
    Exit;
  end;
if (CkJsonObject_SizeOfArray(matches,'lists') = 0) then
  begin
    Memo1.Lines.Add('No active email list named ' + listName);
    Exit;
  end;
if ((CkJsonObject_SizeOfArray(matches,'lists') <> 1) or (CkJsonObject_StringOfEquals(matches,'lists[0].name',listName,True) <> True)) then
  begin
    Memo1.Lines.Add('Expected one exact list name.');
    Exit;
  end;
listId := CkJsonObject__stringOf(matches,'lists[0].list_id');
if (listId = '') then
  begin
    Memo1.Lines.Add('Missing list ID.');
    Exit;
  end;
Memo1.Lines.Add(listName + ' | List ID: ' + listId + ' | Mailable contacts: ' + IntToStr(CkJsonObject_IntOf(matches,'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 := CkHttp_HttpNoBody(http,'GET','https://api.cc.email/v3/emails/activities/{$activity}',resp);
if ((success <> True) and (CkHttpResponse_getStatusCode(resp) <= 0)) then
  begin
    Memo1.Lines.Add('Read draft: HTTP request failed.');
    Exit;
  end;
if ((success <> True) or (CkHttpResponse_getStatusCode(resp) <> 200)) then
  begin
    Memo1.Lines.Add('Read draft: HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)) + ' ' + CkHttpResponse__bodyStr(resp));
    Exit;
  end;
draft := CkJsonObject_Create();
success := CkJsonObject_Load(draft,CkHttpResponse__bodyStr(resp));
if (success <> True) then
  begin
    Memo1.Lines.Add('Expected a JSON object in the response.');
    Exit;
  end;
if ((CkJsonObject_StringOfEquals(draft,'role','primary_email',True) <> True) or (CkJsonObject_StringOfEquals(draft,'current_status','DRAFT',True) <> True)) then
  begin
    Memo1.Lines.Add('Select a primary activity in DRAFT status.');
    Exit;
  end;
Memo1.Lines.Add('LIVE SEND: ' + CkJsonObject__stringOf(draft,'subject') + ' | Activity: ' + activityId + ' | List: ' + listName + ' | Delivery: ' + deliveryTime);
CkJsonObject_UpdateNewArray(draft,'contact_list_ids');
CkJsonObject_UpdateString(draft,'contact_list_ids[0]',listId);
CkJsonObject_UpdateNewArray(draft,'segment_ids');
success := CkHttp_HttpJson(http,'PUT','https://api.cc.email/v3/emails/activities/{$activity}',draft,'application/json',resp);
if ((success <> True) and (CkHttpResponse_getStatusCode(resp) <= 0)) then
  begin
    Memo1.Lines.Add('Assign audience: HTTP request failed.');
    Exit;
  end;
if ((success <> True) or (CkHttpResponse_getStatusCode(resp) <> 200)) then
  begin
    Memo1.Lines.Add('Assign audience: HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)) + ' ' + CkHttpResponse__bodyStr(resp));
    Exit;
  end;
updated := CkJsonObject_Create();
success := CkJsonObject_Load(updated,CkHttpResponse__bodyStr(resp));
if (success <> True) then
  begin
    Memo1.Lines.Add('Expected a JSON object in the response.');
    Exit;
  end;
if ((CkJsonObject_SizeOfArray(updated,'contact_list_ids') <> 1) or (CkJsonObject_StringOfEquals(updated,'contact_list_ids[0]',listId,True) <> True) or (CkJsonObject_SizeOfArray(updated,'segment_ids') > 0)) then
  begin
    Memo1.Lines.Add('The saved audience did not match the requested list. Scheduling stopped.');
    Exit;
  end;
schedule := CkJsonObject_Create();
CkJsonObject_UpdateString(schedule,'scheduled_date',CkDateTime__getAsTimestamp(delivery,False));
success := CkHttp_HttpJson(http,'POST','https://api.cc.email/v3/emails/activities/{$activity}/schedules',schedule,'application/json',resp);
if ((success <> True) or (CkHttpResponse_getStatusCode(resp) < 200) or (CkHttpResponse_getStatusCode(resp) >= 300)) then
  begin
    Memo1.Lines.Add('Schedule not confirmed. HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)));
    Memo1.Lines.Add('The audience was saved. Check the schedule before retrying.');
    Exit;
  end;
Memo1.Lines.Add('Live delivery scheduled for ' + CkJsonObject__stringOf(schedule,'scheduled_date') + ' to ' + listName);

CkSecrets_Dispose(vault);
CkJsonObject_Dispose(entry);
CkJsonObject_Dispose(tokens);
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(selection);
CkDateTime_Dispose(delivery);
CkDateTime_Dispose(now);
CkHttpRequest_Dispose(query);
CkJsonObject_Dispose(matches);
CkJsonObject_Dispose(draft);
CkJsonObject_Dispose(updated);
CkJsonObject_Dispose(schedule);