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