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

Lua
local chilkat = require("chilkat")

-- LIVE SEND: Scheduling this activity delivers email to the selected contact list.
-- Assumes Chilkat is unlocked. See Global Unlock Sample.
local vault = chilkat.Secrets.new()
vault.location = "local_manager"
vault.auto_split = true
local entry = chilkat.JsonObject.new()
entry:update_string("appName", "ConstantContactV3")
entry:update_string("service", "OAuth2")
entry:update_string("username", "my-account")
local tokens = chilkat.JsonObject.new()
local loaded = vault:get_secret_json(entry, tokens)
if not loaded then
    print("Run the desktop authorization example using this account and store.")
    return
end

if tokens:json_type_of("access_token") ~= 1 then
    print("The saved authorization has no access token.")
    return
end

local http = chilkat.Http.new()
http.require_ssl_cert_verify = true
http.auth_token = tokens:string_of("access_token")
local resp = chilkat.HttpResponse.new()

-- Use the selection file from the draft or campaign lookup example for this account.
local selection = chilkat.JsonObject.new()
local success = selection:load_file("newsletter-selection.json")
local activityId = selection:string_of("campaign_activity_id")
if (not success) or (activityId == "") then
    print("Select a campaign and save its primary activity ID first.")
    return
end

http:set_url_var("activity", activityId)
local action = "schedule"
if action == "unschedule" then
    success = http:http_no_body("DELETE", "https://api.cc.email/v3/emails/activities/{$activity}/schedules", resp)
    if (not success) and (resp.status_code <= 0) then
        print("Remove schedule: HTTP request failed.")
        return
    end

    if (not success) or (resp.status_code ~= 204) then
        print("Remove schedule: HTTP " .. resp.status_code .. " " .. resp.body_str)
        return
    end

    print("Schedule removed for activity " .. activityId)
    return
end

if action ~= "schedule" then
    print("Choose schedule or unschedule.")
    return
end

-- Replace the placeholder with the intended future delivery time, e.g. 2030-05-15T09:00:00-05:00.
local deliveryTime = "ENTER_FUTURE_TIMESTAMP_WITH_OFFSET"
local delivery = chilkat.DateTime.new()
local now = chilkat.DateTime.new()
now:set_from_current_system_time()
success = delivery:set_from_timestamp(deliveryTime)
if (not success) or (delivery:diff_seconds(now) <= 0) then
    print("Enter a valid future delivery time with a UTC offset or Z.")
    return
end

-- Use the full display name. AddParam encodes spaces and punctuation.
local listName = "Workshop announcements"
local query = chilkat.HttpRequest.new()
query.http_verb = "GET"
query:add_param("name", listName)
query:add_param("status", "active")
query:add_param("include_membership_count", "active")
success = http:http_req("https://api.cc.email/v3/contact_lists", query, resp)
if (not success) and (resp.status_code <= 0) then
    print("Look up list: HTTP request failed.")
    return
end

if (not success) or (resp.status_code ~= 200) then
    print("Look up list: HTTP " .. resp.status_code .. " " .. resp.body_str)
    return
end

local matches = chilkat.JsonObject.new()
success = matches:load(resp.body_str)
if not success then
    print("Expected a JSON object in the response.")
    return
end

if matches:size_of_array("lists") == 0 then
    print("No active email list named " .. listName)
    return
end

if (matches:size_of_array("lists") ~= 1) or (not matches:string_of_equals("lists[0].name", listName, true)) then
    print("Expected one exact list name.")
    return
end

local listId = matches:string_of("lists[0].list_id")
if listId == "" then
    print("Missing list ID.")
    return
end

print(listName .. " | List ID: " .. listId .. " | Mailable contacts: " .. matches:int_of("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:http_no_body("GET", "https://api.cc.email/v3/emails/activities/{$activity}", resp)
if (not success) and (resp.status_code <= 0) then
    print("Read draft: HTTP request failed.")
    return
end

if (not success) or (resp.status_code ~= 200) then
    print("Read draft: HTTP " .. resp.status_code .. " " .. resp.body_str)
    return
end

local draft = chilkat.JsonObject.new()
success = draft:load(resp.body_str)
if not success then
    print("Expected a JSON object in the response.")
    return
end

if (not draft:string_of_equals("role", "primary_email", true)) or (not draft:string_of_equals("current_status", "DRAFT", true)) then
    print("Select a primary activity in DRAFT status.")
    return
end

print("LIVE SEND: " .. draft:string_of("subject") .. " | Activity: " .. activityId .. " | List: " .. listName .. " | Delivery: " .. deliveryTime)
draft:update_new_array("contact_list_ids")
draft:update_string("contact_list_ids[0]", listId)
draft:update_new_array("segment_ids")
success = http:http_json("PUT", "https://api.cc.email/v3/emails/activities/{$activity}", draft, "application/json", resp)
if (not success) and (resp.status_code <= 0) then
    print("Assign audience: HTTP request failed.")
    return
end

if (not success) or (resp.status_code ~= 200) then
    print("Assign audience: HTTP " .. resp.status_code .. " " .. resp.body_str)
    return
end

local updated = chilkat.JsonObject.new()
success = updated:load(resp.body_str)
if not success then
    print("Expected a JSON object in the response.")
    return
end

if (updated:size_of_array("contact_list_ids") ~= 1) or ((not updated:string_of_equals("contact_list_ids[0]", listId, true)) or (updated:size_of_array("segment_ids") > 0)) then
    print("The saved audience did not match the requested list. Scheduling stopped.")
    return
end

local schedule = chilkat.JsonObject.new()
schedule:update_string("scheduled_date", delivery:get_as_timestamp(false))
success = http:http_json("POST", "https://api.cc.email/v3/emails/activities/{$activity}/schedules", schedule, "application/json", resp)
if (not success) or ((resp.status_code < 200) or (resp.status_code >= 300)) then
    print("Schedule not confirmed. HTTP " .. resp.status_code)
    print("The audience was saved. Check the schedule before retrying.")
    return
end

print("Live delivery scheduled for " .. schedule:string_of("scheduled_date") .. " to " .. listName)