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

AutoIt
Local $bSuccess = False

;  LIVE SEND: Scheduling this activity delivers email to the selected contact list.
;  Assumes Chilkat is unlocked. See Global Unlock Sample.
$oVault = ObjCreate("Chilkat.Secrets")
$oVault.Location = "local_manager"
$oVault.AutoSplit = True
$oEntry = ObjCreate("Chilkat.JsonObject")
$oEntry.UpdateString("appName","ConstantContactV3")
$oEntry.UpdateString("service","OAuth2")
$oEntry.UpdateString("username","my-account")
$oTokens = ObjCreate("Chilkat.JsonObject")
Local $bLoaded = $oVault.GetSecretJson($oEntry,$oTokens)
If ($bLoaded <> True) Then
    ConsoleWrite("Run the desktop authorization example using this account and store." & @CRLF)
    Exit
EndIf

If ($oTokens.JsonTypeOf("access_token") <> 1) Then
    ConsoleWrite("The saved authorization has no access token." & @CRLF)
    Exit
EndIf

$oHttp = ObjCreate("Chilkat.Http")
$oHttp.RequireSslCertVerify = True
$oHttp.AuthToken = $oTokens.StringOf("access_token")
$oResp = ObjCreate("Chilkat.HttpResponse")

;  Use the selection file from the draft or campaign lookup example for this account.
$oSelection = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oSelection.LoadFile("newsletter-selection.json")
Local $sActivityId = $oSelection.StringOf("campaign_activity_id")
If (($bSuccess <> True) Or ($sActivityId = "")) Then
    ConsoleWrite("Select a campaign and save its primary activity ID first." & @CRLF)
    Exit
EndIf

$oHttp.SetUrlVar("activity",$sActivityId)
Local $sAction = "schedule"
If ($sAction = "unschedule") Then
    $bSuccess = $oHttp.HttpNoBody("DELETE","https://api.cc.email/v3/emails/activities/{$activity}/schedules",$oResp)
    If (($bSuccess <> True) And ($oResp.StatusCode <= 0)) Then
        ConsoleWrite("Remove schedule: HTTP request failed." & @CRLF)
        Exit
    EndIf

    If (($bSuccess <> True) Or ($oResp.StatusCode <> 204)) Then
        ConsoleWrite("Remove schedule: HTTP " & $oResp.StatusCode & " " & $oResp.BodyStr & @CRLF)
        Exit
    EndIf

    ConsoleWrite("Schedule removed for activity " & $sActivityId & @CRLF)
    Exit
EndIf

If ($sAction <> "schedule") Then
    ConsoleWrite("Choose schedule or unschedule." & @CRLF)
    Exit
EndIf

;  Replace the placeholder with the intended future delivery time, e.g. 2030-05-15T09:00:00-05:00.
Local $sDeliveryTime = "ENTER_FUTURE_TIMESTAMP_WITH_OFFSET"
$oDelivery = ObjCreate("Chilkat.CkDateTime")
$oNow = ObjCreate("Chilkat.CkDateTime")
$oNow.SetFromCurrentSystemTime()
$bSuccess = $oDelivery.SetFromTimestamp($sDeliveryTime)
If (($bSuccess <> True) Or ($oDelivery.DiffSeconds($oNow) <= 0)) Then
    ConsoleWrite("Enter a valid future delivery time with a UTC offset or Z." & @CRLF)
    Exit
EndIf

;  Use the full display name. AddParam encodes spaces and punctuation.
Local $sListName = "Workshop announcements"
$oQuery = ObjCreate("Chilkat.HttpRequest")
$oQuery.HttpVerb = "GET"
$oQuery.AddParam "name",$sListName
$oQuery.AddParam "status","active"
$oQuery.AddParam "include_membership_count","active"
$bSuccess = $oHttp.HttpReq("https://api.cc.email/v3/contact_lists",$oQuery,$oResp)
If (($bSuccess <> True) And ($oResp.StatusCode <= 0)) Then
    ConsoleWrite("Look up list: HTTP request failed." & @CRLF)
    Exit
EndIf

If (($bSuccess <> True) Or ($oResp.StatusCode <> 200)) Then
    ConsoleWrite("Look up list: HTTP " & $oResp.StatusCode & " " & $oResp.BodyStr & @CRLF)
    Exit
EndIf

$oMatches = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oMatches.Load($oResp.BodyStr)
If ($bSuccess <> True) Then
    ConsoleWrite("Expected a JSON object in the response." & @CRLF)
    Exit
EndIf

If ($oMatches.SizeOfArray("lists") = 0) Then
    ConsoleWrite("No active email list named " & $sListName & @CRLF)
    Exit
EndIf

If (($oMatches.SizeOfArray("lists") <> 1) Or ($oMatches.StringOfEquals("lists[0].name",$sListName,True) <> True)) Then
    ConsoleWrite("Expected one exact list name." & @CRLF)
    Exit
EndIf

Local $sListId = $oMatches.StringOf("lists[0].list_id")
If ($sListId = "") Then
    ConsoleWrite("Missing list ID." & @CRLF)
    Exit
EndIf

ConsoleWrite($sListName & " | List ID: " & $sListId & " | Mailable contacts: " & $oMatches.IntOf("lists[0].membership_count") & @CRLF)

;  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.
$bSuccess = $oHttp.HttpNoBody("GET","https://api.cc.email/v3/emails/activities/{$activity}",$oResp)
If (($bSuccess <> True) And ($oResp.StatusCode <= 0)) Then
    ConsoleWrite("Read draft: HTTP request failed." & @CRLF)
    Exit
EndIf

If (($bSuccess <> True) Or ($oResp.StatusCode <> 200)) Then
    ConsoleWrite("Read draft: HTTP " & $oResp.StatusCode & " " & $oResp.BodyStr & @CRLF)
    Exit
EndIf

$oDraft = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oDraft.Load($oResp.BodyStr)
If ($bSuccess <> True) Then
    ConsoleWrite("Expected a JSON object in the response." & @CRLF)
    Exit
EndIf

If (($oDraft.StringOfEquals("role","primary_email",True) <> True) Or ($oDraft.StringOfEquals("current_status","DRAFT",True) <> True)) Then
    ConsoleWrite("Select a primary activity in DRAFT status." & @CRLF)
    Exit
EndIf

ConsoleWrite("LIVE SEND: " & $oDraft.StringOf("subject") & " | Activity: " & $sActivityId & " | List: " & $sListName & " | Delivery: " & $sDeliveryTime & @CRLF)
$oDraft.UpdateNewArray("contact_list_ids")
$oDraft.UpdateString("contact_list_ids[0]",$sListId)
$oDraft.UpdateNewArray("segment_ids")
$bSuccess = $oHttp.HttpJson("PUT","https://api.cc.email/v3/emails/activities/{$activity}",$oDraft,"application/json",$oResp)
If (($bSuccess <> True) And ($oResp.StatusCode <= 0)) Then
    ConsoleWrite("Assign audience: HTTP request failed." & @CRLF)
    Exit
EndIf

If (($bSuccess <> True) Or ($oResp.StatusCode <> 200)) Then
    ConsoleWrite("Assign audience: HTTP " & $oResp.StatusCode & " " & $oResp.BodyStr & @CRLF)
    Exit
EndIf

$oUpdated = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oUpdated.Load($oResp.BodyStr)
If ($bSuccess <> True) Then
    ConsoleWrite("Expected a JSON object in the response." & @CRLF)
    Exit
EndIf

If (($oUpdated.SizeOfArray("contact_list_ids") <> 1) Or ($oUpdated.StringOfEquals("contact_list_ids[0]",$sListId,True) <> True) Or ($oUpdated.SizeOfArray("segment_ids") > 0)) Then
    ConsoleWrite("The saved audience did not match the requested list. Scheduling stopped." & @CRLF)
    Exit
EndIf

$oSchedule = ObjCreate("Chilkat.JsonObject")
$oSchedule.UpdateString("scheduled_date",$oDelivery.GetAsTimestamp(False))
$bSuccess = $oHttp.HttpJson("POST","https://api.cc.email/v3/emails/activities/{$activity}/schedules",$oSchedule,"application/json",$oResp)
If (($bSuccess <> True) Or ($oResp.StatusCode < 200) Or ($oResp.StatusCode >= 300)) Then
    ConsoleWrite("Schedule not confirmed. HTTP " & $oResp.StatusCode & @CRLF)
    ConsoleWrite("The audience was saved. Check the schedule before retrying." & @CRLF)
    Exit
EndIf

ConsoleWrite("Live delivery scheduled for " & $oSchedule.StringOf("scheduled_date") & " to " & $sListName & @CRLF)