Sample code for 30+ languages & platforms
Visual FoxPro 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 Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loVault
LOCAL loEntry
LOCAL loTokens
LOCAL lnLoaded
LOCAL loHttp
LOCAL loResp
LOCAL loSelection
LOCAL lcActivityId
LOCAL lcAction
LOCAL lcDeliveryTime
LOCAL loDelivery
LOCAL loNow
LOCAL lcListName
LOCAL loQuery
LOCAL loMatches
LOCAL lcListId
LOCAL loDraft
LOCAL loUpdated
LOCAL loSchedule

lnSuccess = 0

*  LIVE SEND: Scheduling this activity delivers email to the selected contact list.
*  Assumes Chilkat is unlocked. See Global Unlock Sample.
loVault = CreateObject('Chilkat.Secrets')
loVault.Location = "local_manager"
loVault.AutoSplit = 1
loEntry = CreateObject('Chilkat.JsonObject')
loEntry.UpdateString("appName","ConstantContactV3")
loEntry.UpdateString("service","OAuth2")
loEntry.UpdateString("username","my-account")
loTokens = CreateObject('Chilkat.JsonObject')
lnLoaded = loVault.GetSecretJson(loEntry,loTokens)
IF (lnLoaded <> 1) THEN
    ? "Run the desktop authorization example using this account and store."
    RELEASE loVault
    RELEASE loEntry
    RELEASE loTokens
    CANCEL
ENDIF

IF (loTokens.JsonTypeOf("access_token") <> 1) THEN
    ? "The saved authorization has no access token."
    RELEASE loVault
    RELEASE loEntry
    RELEASE loTokens
    CANCEL
ENDIF

loHttp = CreateObject('Chilkat.Http')
loHttp.RequireSslCertVerify = 1
loHttp.AuthToken = loTokens.StringOf("access_token")
loResp = CreateObject('Chilkat.HttpResponse')

*  Use the selection file from the draft or campaign lookup example for this account.
loSelection = CreateObject('Chilkat.JsonObject')
lnSuccess = loSelection.LoadFile("newsletter-selection.json")
lcActivityId = loSelection.StringOf("campaign_activity_id")
IF ((lnSuccess <> 1) OR (lcActivityId = "")) THEN
    ? "Select a campaign and save its primary activity ID first."
    RELEASE loVault
    RELEASE loEntry
    RELEASE loTokens
    RELEASE loHttp
    RELEASE loResp
    RELEASE loSelection
    CANCEL
ENDIF

loHttp.SetUrlVar("activity",lcActivityId)
lcAction = "schedule"
IF (lcAction = "unschedule") THEN
    lnSuccess = loHttp.HttpNoBody("DELETE","https://api.cc.email/v3/emails/activities/{$activity}/schedules",loResp)
    IF ((lnSuccess <> 1) AND (loResp.StatusCode <= 0)) THEN
        ? "Remove schedule: HTTP request failed."
        RELEASE loVault
        RELEASE loEntry
        RELEASE loTokens
        RELEASE loHttp
        RELEASE loResp
        RELEASE loSelection
        CANCEL
    ENDIF

    IF ((lnSuccess <> 1) OR (loResp.StatusCode <> 204)) THEN
        ? "Remove schedule: HTTP " + STR(loResp.StatusCode) + " " + loResp.BodyStr
        RELEASE loVault
        RELEASE loEntry
        RELEASE loTokens
        RELEASE loHttp
        RELEASE loResp
        RELEASE loSelection
        CANCEL
    ENDIF

    ? "Schedule removed for activity " + lcActivityId
    RELEASE loVault
    RELEASE loEntry
    RELEASE loTokens
    RELEASE loHttp
    RELEASE loResp
    RELEASE loSelection
    CANCEL
ENDIF

IF (lcAction <> "schedule") THEN
    ? "Choose schedule or unschedule."
    RELEASE loVault
    RELEASE loEntry
    RELEASE loTokens
    RELEASE loHttp
    RELEASE loResp
    RELEASE loSelection
    CANCEL
ENDIF

*  Replace the placeholder with the intended future delivery time, e.g. 2030-05-15T09:00:00-05:00.
lcDeliveryTime = "ENTER_FUTURE_TIMESTAMP_WITH_OFFSET"
loDelivery = CreateObject('Chilkat.CkDateTime')
loNow = CreateObject('Chilkat.CkDateTime')
loNow.SetFromCurrentSystemTime()
lnSuccess = loDelivery.SetFromTimestamp(lcDeliveryTime)
IF ((lnSuccess <> 1) OR (loDelivery.DiffSeconds(loNow) <= 0)) THEN
    ? "Enter a valid future delivery time with a UTC offset or Z."
    RELEASE loVault
    RELEASE loEntry
    RELEASE loTokens
    RELEASE loHttp
    RELEASE loResp
    RELEASE loSelection
    RELEASE loDelivery
    RELEASE loNow
    CANCEL
ENDIF

*  Use the full display name. AddParam encodes spaces and punctuation.
lcListName = "Workshop announcements"
loQuery = CreateObject('Chilkat.HttpRequest')
loQuery.HttpVerb = "GET"
loQuery.AddParam("name",lcListName)
loQuery.AddParam("status","active")
loQuery.AddParam("include_membership_count","active")
lnSuccess = loHttp.HttpReq("https://api.cc.email/v3/contact_lists",loQuery,loResp)
IF ((lnSuccess <> 1) AND (loResp.StatusCode <= 0)) THEN
    ? "Look up list: HTTP request failed."
    RELEASE loVault
    RELEASE loEntry
    RELEASE loTokens
    RELEASE loHttp
    RELEASE loResp
    RELEASE loSelection
    RELEASE loDelivery
    RELEASE loNow
    RELEASE loQuery
    CANCEL
ENDIF

IF ((lnSuccess <> 1) OR (loResp.StatusCode <> 200)) THEN
    ? "Look up list: HTTP " + STR(loResp.StatusCode) + " " + loResp.BodyStr
    RELEASE loVault
    RELEASE loEntry
    RELEASE loTokens
    RELEASE loHttp
    RELEASE loResp
    RELEASE loSelection
    RELEASE loDelivery
    RELEASE loNow
    RELEASE loQuery
    CANCEL
ENDIF

loMatches = CreateObject('Chilkat.JsonObject')
lnSuccess = loMatches.Load(loResp.BodyStr)
IF (lnSuccess <> 1) THEN
    ? "Expected a JSON object in the response."
    RELEASE loVault
    RELEASE loEntry
    RELEASE loTokens
    RELEASE loHttp
    RELEASE loResp
    RELEASE loSelection
    RELEASE loDelivery
    RELEASE loNow
    RELEASE loQuery
    RELEASE loMatches
    CANCEL
ENDIF

IF (loMatches.SizeOfArray("lists") = 0) THEN
    ? "No active email list named " + lcListName
    RELEASE loVault
    RELEASE loEntry
    RELEASE loTokens
    RELEASE loHttp
    RELEASE loResp
    RELEASE loSelection
    RELEASE loDelivery
    RELEASE loNow
    RELEASE loQuery
    RELEASE loMatches
    CANCEL
ENDIF

IF ((loMatches.SizeOfArray("lists") <> 1) OR (loMatches.StringOfEquals("lists[0].name",lcListName,1) <> 1)) THEN
    ? "Expected one exact list name."
    RELEASE loVault
    RELEASE loEntry
    RELEASE loTokens
    RELEASE loHttp
    RELEASE loResp
    RELEASE loSelection
    RELEASE loDelivery
    RELEASE loNow
    RELEASE loQuery
    RELEASE loMatches
    CANCEL
ENDIF

lcListId = loMatches.StringOf("lists[0].list_id")
IF (lcListId = "") THEN
    ? "Missing list ID."
    RELEASE loVault
    RELEASE loEntry
    RELEASE loTokens
    RELEASE loHttp
    RELEASE loResp
    RELEASE loSelection
    RELEASE loDelivery
    RELEASE loNow
    RELEASE loQuery
    RELEASE loMatches
    CANCEL
ENDIF

? lcListName + " | List ID: " + lcListId + " | Mailable contacts: " + STR(loMatches.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.
lnSuccess = loHttp.HttpNoBody("GET","https://api.cc.email/v3/emails/activities/{$activity}",loResp)
IF ((lnSuccess <> 1) AND (loResp.StatusCode <= 0)) THEN
    ? "Read draft: HTTP request failed."
    RELEASE loVault
    RELEASE loEntry
    RELEASE loTokens
    RELEASE loHttp
    RELEASE loResp
    RELEASE loSelection
    RELEASE loDelivery
    RELEASE loNow
    RELEASE loQuery
    RELEASE loMatches
    CANCEL
ENDIF

IF ((lnSuccess <> 1) OR (loResp.StatusCode <> 200)) THEN
    ? "Read draft: HTTP " + STR(loResp.StatusCode) + " " + loResp.BodyStr
    RELEASE loVault
    RELEASE loEntry
    RELEASE loTokens
    RELEASE loHttp
    RELEASE loResp
    RELEASE loSelection
    RELEASE loDelivery
    RELEASE loNow
    RELEASE loQuery
    RELEASE loMatches
    CANCEL
ENDIF

loDraft = CreateObject('Chilkat.JsonObject')
lnSuccess = loDraft.Load(loResp.BodyStr)
IF (lnSuccess <> 1) THEN
    ? "Expected a JSON object in the response."
    RELEASE loVault
    RELEASE loEntry
    RELEASE loTokens
    RELEASE loHttp
    RELEASE loResp
    RELEASE loSelection
    RELEASE loDelivery
    RELEASE loNow
    RELEASE loQuery
    RELEASE loMatches
    RELEASE loDraft
    CANCEL
ENDIF

IF ((loDraft.StringOfEquals("role","primary_email",1) <> 1) OR (loDraft.StringOfEquals("current_status","DRAFT",1) <> 1)) THEN
    ? "Select a primary activity in DRAFT status."
    RELEASE loVault
    RELEASE loEntry
    RELEASE loTokens
    RELEASE loHttp
    RELEASE loResp
    RELEASE loSelection
    RELEASE loDelivery
    RELEASE loNow
    RELEASE loQuery
    RELEASE loMatches
    RELEASE loDraft
    CANCEL
ENDIF

? "LIVE SEND: " + loDraft.StringOf("subject") + " | Activity: " + lcActivityId + " | List: " + lcListName + " | Delivery: " + lcDeliveryTime
loDraft.UpdateNewArray("contact_list_ids")
loDraft.UpdateString("contact_list_ids[0]",lcListId)
loDraft.UpdateNewArray("segment_ids")
lnSuccess = loHttp.HttpJson("PUT","https://api.cc.email/v3/emails/activities/{$activity}",loDraft,"application/json",loResp)
IF ((lnSuccess <> 1) AND (loResp.StatusCode <= 0)) THEN
    ? "Assign audience: HTTP request failed."
    RELEASE loVault
    RELEASE loEntry
    RELEASE loTokens
    RELEASE loHttp
    RELEASE loResp
    RELEASE loSelection
    RELEASE loDelivery
    RELEASE loNow
    RELEASE loQuery
    RELEASE loMatches
    RELEASE loDraft
    CANCEL
ENDIF

IF ((lnSuccess <> 1) OR (loResp.StatusCode <> 200)) THEN
    ? "Assign audience: HTTP " + STR(loResp.StatusCode) + " " + loResp.BodyStr
    RELEASE loVault
    RELEASE loEntry
    RELEASE loTokens
    RELEASE loHttp
    RELEASE loResp
    RELEASE loSelection
    RELEASE loDelivery
    RELEASE loNow
    RELEASE loQuery
    RELEASE loMatches
    RELEASE loDraft
    CANCEL
ENDIF

loUpdated = CreateObject('Chilkat.JsonObject')
lnSuccess = loUpdated.Load(loResp.BodyStr)
IF (lnSuccess <> 1) THEN
    ? "Expected a JSON object in the response."
    RELEASE loVault
    RELEASE loEntry
    RELEASE loTokens
    RELEASE loHttp
    RELEASE loResp
    RELEASE loSelection
    RELEASE loDelivery
    RELEASE loNow
    RELEASE loQuery
    RELEASE loMatches
    RELEASE loDraft
    RELEASE loUpdated
    CANCEL
ENDIF

IF ((loUpdated.SizeOfArray("contact_list_ids") <> 1) OR (loUpdated.StringOfEquals("contact_list_ids[0]",lcListId,1) <> 1) OR (loUpdated.SizeOfArray("segment_ids") > 0)) THEN
    ? "The saved audience did not match the requested list. Scheduling stopped."
    RELEASE loVault
    RELEASE loEntry
    RELEASE loTokens
    RELEASE loHttp
    RELEASE loResp
    RELEASE loSelection
    RELEASE loDelivery
    RELEASE loNow
    RELEASE loQuery
    RELEASE loMatches
    RELEASE loDraft
    RELEASE loUpdated
    CANCEL
ENDIF

loSchedule = CreateObject('Chilkat.JsonObject')
loSchedule.UpdateString("scheduled_date",loDelivery.GetAsTimestamp(0))
lnSuccess = loHttp.HttpJson("POST","https://api.cc.email/v3/emails/activities/{$activity}/schedules",loSchedule,"application/json",loResp)
IF ((lnSuccess <> 1) OR (loResp.StatusCode < 200) OR (loResp.StatusCode >= 300)) THEN
    ? "Schedule not confirmed. HTTP " + STR(loResp.StatusCode)
    ? "The audience was saved. Check the schedule before retrying."
    RELEASE loVault
    RELEASE loEntry
    RELEASE loTokens
    RELEASE loHttp
    RELEASE loResp
    RELEASE loSelection
    RELEASE loDelivery
    RELEASE loNow
    RELEASE loQuery
    RELEASE loMatches
    RELEASE loDraft
    RELEASE loUpdated
    RELEASE loSchedule
    CANCEL
ENDIF

? "Live delivery scheduled for " + loSchedule.StringOf("scheduled_date") + " to " + lcListName

RELEASE loVault
RELEASE loEntry
RELEASE loTokens
RELEASE loHttp
RELEASE loResp
RELEASE loSelection
RELEASE loDelivery
RELEASE loNow
RELEASE loQuery
RELEASE loMatches
RELEASE loDraft
RELEASE loUpdated
RELEASE loSchedule