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 PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_Vault
oleobject loo_Entry
oleobject loo_Tokens
long li_Loaded
oleobject loo_Http
oleobject loo_Resp
oleobject loo_Selection
string ls_ActivityId
string ls_Action
string ls_DeliveryTime
oleobject loo_Delivery
oleobject loo_Now
string ls_ListName
oleobject loo_Query
oleobject loo_Matches
string ls_ListId
oleobject loo_Draft
oleobject loo_Updated
oleobject loo_Schedule
li_Success = 0
// LIVE SEND: Scheduling this activity delivers email to the selected contact list.
// Assumes Chilkat is unlocked. See Global Unlock Sample.
loo_Vault = create oleobject
li_rc = loo_Vault.ConnectToNewObject("Chilkat.Secrets")
if li_rc < 0 then
destroy loo_Vault
MessageBox("Error","Connecting to COM object failed")
return
end if
loo_Vault.Location = "local_manager"
loo_Vault.AutoSplit = 1
loo_Entry = create oleobject
li_rc = loo_Entry.ConnectToNewObject("Chilkat.JsonObject")
loo_Entry.UpdateString("appName","ConstantContactV3")
loo_Entry.UpdateString("service","OAuth2")
loo_Entry.UpdateString("username","my-account")
loo_Tokens = create oleobject
li_rc = loo_Tokens.ConnectToNewObject("Chilkat.JsonObject")
li_Loaded = loo_Vault.GetSecretJson(loo_Entry,loo_Tokens)
if li_Loaded <> 1 then
Write-Debug "Run the desktop authorization example using this account and store."
destroy loo_Vault
destroy loo_Entry
destroy loo_Tokens
return
end if
if loo_Tokens.JsonTypeOf("access_token") <> 1 then
Write-Debug "The saved authorization has no access token."
destroy loo_Vault
destroy loo_Entry
destroy loo_Tokens
return
end if
loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
loo_Http.RequireSslCertVerify = 1
loo_Http.AuthToken = loo_Tokens.StringOf("access_token")
loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")
// Use the selection file from the draft or campaign lookup example for this account.
loo_Selection = create oleobject
li_rc = loo_Selection.ConnectToNewObject("Chilkat.JsonObject")
li_Success = loo_Selection.LoadFile("newsletter-selection.json")
ls_ActivityId = loo_Selection.StringOf("campaign_activity_id")
if (li_Success <> 1) OR (ls_ActivityId = "") then
Write-Debug "Select a campaign and save its primary activity ID first."
destroy loo_Vault
destroy loo_Entry
destroy loo_Tokens
destroy loo_Http
destroy loo_Resp
destroy loo_Selection
return
end if
loo_Http.SetUrlVar("activity",ls_ActivityId)
ls_Action = "schedule"
if ls_Action = "unschedule" then
li_Success = loo_Http.HttpNoBody("DELETE","https://api.cc.email/v3/emails/activities/{$activity}/schedules",loo_Resp)
if (li_Success <> 1) AND (loo_Resp.StatusCode <= 0) then
Write-Debug "Remove schedule: HTTP request failed."
destroy loo_Vault
destroy loo_Entry
destroy loo_Tokens
destroy loo_Http
destroy loo_Resp
destroy loo_Selection
return
end if
if (li_Success <> 1) OR (loo_Resp.StatusCode <> 204) then
Write-Debug "Remove schedule: HTTP " + string(loo_Resp.StatusCode) + " " + loo_Resp.BodyStr
destroy loo_Vault
destroy loo_Entry
destroy loo_Tokens
destroy loo_Http
destroy loo_Resp
destroy loo_Selection
return
end if
Write-Debug "Schedule removed for activity " + ls_ActivityId
destroy loo_Vault
destroy loo_Entry
destroy loo_Tokens
destroy loo_Http
destroy loo_Resp
destroy loo_Selection
return
end if
if ls_Action <> "schedule" then
Write-Debug "Choose schedule or unschedule."
destroy loo_Vault
destroy loo_Entry
destroy loo_Tokens
destroy loo_Http
destroy loo_Resp
destroy loo_Selection
return
end if
// Replace the placeholder with the intended future delivery time, e.g. 2030-05-15T09:00:00-05:00.
ls_DeliveryTime = "ENTER_FUTURE_TIMESTAMP_WITH_OFFSET"
loo_Delivery = create oleobject
li_rc = loo_Delivery.ConnectToNewObject("Chilkat.CkDateTime")
loo_Now = create oleobject
li_rc = loo_Now.ConnectToNewObject("Chilkat.CkDateTime")
loo_Now.SetFromCurrentSystemTime()
li_Success = loo_Delivery.SetFromTimestamp(ls_DeliveryTime)
if (li_Success <> 1) OR (loo_Delivery.DiffSeconds(loo_Now) <= 0) then
Write-Debug "Enter a valid future delivery time with a UTC offset or Z."
destroy loo_Vault
destroy loo_Entry
destroy loo_Tokens
destroy loo_Http
destroy loo_Resp
destroy loo_Selection
destroy loo_Delivery
destroy loo_Now
return
end if
// Use the full display name. AddParam encodes spaces and punctuation.
ls_ListName = "Workshop announcements"
loo_Query = create oleobject
li_rc = loo_Query.ConnectToNewObject("Chilkat.HttpRequest")
loo_Query.HttpVerb = "GET"
loo_Query.AddParam("name",ls_ListName)
loo_Query.AddParam("status","active")
loo_Query.AddParam("include_membership_count","active")
li_Success = loo_Http.HttpReq("https://api.cc.email/v3/contact_lists",loo_Query,loo_Resp)
if (li_Success <> 1) AND (loo_Resp.StatusCode <= 0) then
Write-Debug "Look up list: HTTP request failed."
destroy loo_Vault
destroy loo_Entry
destroy loo_Tokens
destroy loo_Http
destroy loo_Resp
destroy loo_Selection
destroy loo_Delivery
destroy loo_Now
destroy loo_Query
return
end if
if (li_Success <> 1) OR (loo_Resp.StatusCode <> 200) then
Write-Debug "Look up list: HTTP " + string(loo_Resp.StatusCode) + " " + loo_Resp.BodyStr
destroy loo_Vault
destroy loo_Entry
destroy loo_Tokens
destroy loo_Http
destroy loo_Resp
destroy loo_Selection
destroy loo_Delivery
destroy loo_Now
destroy loo_Query
return
end if
loo_Matches = create oleobject
li_rc = loo_Matches.ConnectToNewObject("Chilkat.JsonObject")
li_Success = loo_Matches.Load(loo_Resp.BodyStr)
if li_Success <> 1 then
Write-Debug "Expected a JSON object in the response."
destroy loo_Vault
destroy loo_Entry
destroy loo_Tokens
destroy loo_Http
destroy loo_Resp
destroy loo_Selection
destroy loo_Delivery
destroy loo_Now
destroy loo_Query
destroy loo_Matches
return
end if
if loo_Matches.SizeOfArray("lists") = 0 then
Write-Debug "No active email list named " + ls_ListName
destroy loo_Vault
destroy loo_Entry
destroy loo_Tokens
destroy loo_Http
destroy loo_Resp
destroy loo_Selection
destroy loo_Delivery
destroy loo_Now
destroy loo_Query
destroy loo_Matches
return
end if
if (loo_Matches.SizeOfArray("lists") <> 1) OR (loo_Matches.StringOfEquals("lists[0].name",ls_ListName,1) <> 1) then
Write-Debug "Expected one exact list name."
destroy loo_Vault
destroy loo_Entry
destroy loo_Tokens
destroy loo_Http
destroy loo_Resp
destroy loo_Selection
destroy loo_Delivery
destroy loo_Now
destroy loo_Query
destroy loo_Matches
return
end if
ls_ListId = loo_Matches.StringOf("lists[0].list_id")
if ls_ListId = "" then
Write-Debug "Missing list ID."
destroy loo_Vault
destroy loo_Entry
destroy loo_Tokens
destroy loo_Http
destroy loo_Resp
destroy loo_Selection
destroy loo_Delivery
destroy loo_Now
destroy loo_Query
destroy loo_Matches
return
end if
Write-Debug ls_ListName + " | List ID: " + ls_ListId + " | Mailable contacts: " + string(loo_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.
li_Success = loo_Http.HttpNoBody("GET","https://api.cc.email/v3/emails/activities/{$activity}",loo_Resp)
if (li_Success <> 1) AND (loo_Resp.StatusCode <= 0) then
Write-Debug "Read draft: HTTP request failed."
destroy loo_Vault
destroy loo_Entry
destroy loo_Tokens
destroy loo_Http
destroy loo_Resp
destroy loo_Selection
destroy loo_Delivery
destroy loo_Now
destroy loo_Query
destroy loo_Matches
return
end if
if (li_Success <> 1) OR (loo_Resp.StatusCode <> 200) then
Write-Debug "Read draft: HTTP " + string(loo_Resp.StatusCode) + " " + loo_Resp.BodyStr
destroy loo_Vault
destroy loo_Entry
destroy loo_Tokens
destroy loo_Http
destroy loo_Resp
destroy loo_Selection
destroy loo_Delivery
destroy loo_Now
destroy loo_Query
destroy loo_Matches
return
end if
loo_Draft = create oleobject
li_rc = loo_Draft.ConnectToNewObject("Chilkat.JsonObject")
li_Success = loo_Draft.Load(loo_Resp.BodyStr)
if li_Success <> 1 then
Write-Debug "Expected a JSON object in the response."
destroy loo_Vault
destroy loo_Entry
destroy loo_Tokens
destroy loo_Http
destroy loo_Resp
destroy loo_Selection
destroy loo_Delivery
destroy loo_Now
destroy loo_Query
destroy loo_Matches
destroy loo_Draft
return
end if
if (loo_Draft.StringOfEquals("role","primary_email",1) <> 1) OR (loo_Draft.StringOfEquals("current_status","DRAFT",1) <> 1) then
Write-Debug "Select a primary activity in DRAFT status."
destroy loo_Vault
destroy loo_Entry
destroy loo_Tokens
destroy loo_Http
destroy loo_Resp
destroy loo_Selection
destroy loo_Delivery
destroy loo_Now
destroy loo_Query
destroy loo_Matches
destroy loo_Draft
return
end if
Write-Debug "LIVE SEND: " + loo_Draft.StringOf("subject") + " | Activity: " + ls_ActivityId + " | List: " + ls_ListName + " | Delivery: " + ls_DeliveryTime
loo_Draft.UpdateNewArray("contact_list_ids")
loo_Draft.UpdateString("contact_list_ids[0]",ls_ListId)
loo_Draft.UpdateNewArray("segment_ids")
li_Success = loo_Http.HttpJson("PUT","https://api.cc.email/v3/emails/activities/{$activity}",loo_Draft,"application/json",loo_Resp)
if (li_Success <> 1) AND (loo_Resp.StatusCode <= 0) then
Write-Debug "Assign audience: HTTP request failed."
destroy loo_Vault
destroy loo_Entry
destroy loo_Tokens
destroy loo_Http
destroy loo_Resp
destroy loo_Selection
destroy loo_Delivery
destroy loo_Now
destroy loo_Query
destroy loo_Matches
destroy loo_Draft
return
end if
if (li_Success <> 1) OR (loo_Resp.StatusCode <> 200) then
Write-Debug "Assign audience: HTTP " + string(loo_Resp.StatusCode) + " " + loo_Resp.BodyStr
destroy loo_Vault
destroy loo_Entry
destroy loo_Tokens
destroy loo_Http
destroy loo_Resp
destroy loo_Selection
destroy loo_Delivery
destroy loo_Now
destroy loo_Query
destroy loo_Matches
destroy loo_Draft
return
end if
loo_Updated = create oleobject
li_rc = loo_Updated.ConnectToNewObject("Chilkat.JsonObject")
li_Success = loo_Updated.Load(loo_Resp.BodyStr)
if li_Success <> 1 then
Write-Debug "Expected a JSON object in the response."
destroy loo_Vault
destroy loo_Entry
destroy loo_Tokens
destroy loo_Http
destroy loo_Resp
destroy loo_Selection
destroy loo_Delivery
destroy loo_Now
destroy loo_Query
destroy loo_Matches
destroy loo_Draft
destroy loo_Updated
return
end if
if (loo_Updated.SizeOfArray("contact_list_ids") <> 1) OR (loo_Updated.StringOfEquals("contact_list_ids[0]",ls_ListId,1) <> 1) OR (loo_Updated.SizeOfArray("segment_ids") > 0) then
Write-Debug "The saved audience did not match the requested list. Scheduling stopped."
destroy loo_Vault
destroy loo_Entry
destroy loo_Tokens
destroy loo_Http
destroy loo_Resp
destroy loo_Selection
destroy loo_Delivery
destroy loo_Now
destroy loo_Query
destroy loo_Matches
destroy loo_Draft
destroy loo_Updated
return
end if
loo_Schedule = create oleobject
li_rc = loo_Schedule.ConnectToNewObject("Chilkat.JsonObject")
loo_Schedule.UpdateString("scheduled_date",loo_Delivery.GetAsTimestamp(0))
li_Success = loo_Http.HttpJson("POST","https://api.cc.email/v3/emails/activities/{$activity}/schedules",loo_Schedule,"application/json",loo_Resp)
if (li_Success <> 1) OR (loo_Resp.StatusCode < 200) OR (loo_Resp.StatusCode >= 300) then
Write-Debug "Schedule not confirmed. HTTP " + string(loo_Resp.StatusCode)
Write-Debug "The audience was saved. Check the schedule before retrying."
destroy loo_Vault
destroy loo_Entry
destroy loo_Tokens
destroy loo_Http
destroy loo_Resp
destroy loo_Selection
destroy loo_Delivery
destroy loo_Now
destroy loo_Query
destroy loo_Matches
destroy loo_Draft
destroy loo_Updated
destroy loo_Schedule
return
end if
Write-Debug "Live delivery scheduled for " + loo_Schedule.StringOf("scheduled_date") + " to " + ls_ListName
destroy loo_Vault
destroy loo_Entry
destroy loo_Tokens
destroy loo_Http
destroy loo_Resp
destroy loo_Selection
destroy loo_Delivery
destroy loo_Now
destroy loo_Query
destroy loo_Matches
destroy loo_Draft
destroy loo_Updated
destroy loo_Schedule