Sample code for 30+ languages & platforms
Lua Requires Chilkat v11.0.0+

Constant Contact V3: Create a newsletter draft from an HTML file

See more Constant Contact Examples

Save your newsletter as UTF-8 in newsletter.html. Put the tracking tag inside the body, for example <body><h1>Workshop news</h1>[[trackingImage]]</body>. Supply a unique campaign name and confirmed sender and reply-to addresses for your account. Constant Contact uses the account’s physical mailing address in the footer by default.

This creates a custom-code draft. The example writes the campaign and primary activity IDs to newsletter-selection.json for the test and scheduling examples. If the response is lost after creation, look up the campaign by name before creating another draft.

Authorize the application using the same account and credential store.

Constant Contact developer guide

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- 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()

local html = chilkat.StringBuilder.new()
local success = html:load_file("newsletter.html", "utf-8")
if not success then
    print("Could not read newsletter.html.")
    return
end

if not html:contains("[[trackingImage]]", true) then
    print("Add [[trackingImage]] inside the HTML body.")
    return
end

local draft = chilkat.JsonObject.new()
draft:update_string("name", "Workshop newsletter")
draft:update_int("email_campaign_activities[0].format_type", 5)
draft:update_string("email_campaign_activities[0].from_name", "Workshop team")
draft:update_string("email_campaign_activities[0].from_email", "news@example.com")
draft:update_string("email_campaign_activities[0].reply_to_email", "workshops@example.com")
draft:update_string("email_campaign_activities[0].subject", "Upcoming workshops")
draft:update_string("email_campaign_activities[0].html_content", html:get_as_string())
success = http:http_json("POST", "https://api.cc.email/v3/emails", draft, "application/json", resp)
if (not success) and (resp.status_code <= 0) then
    print("Create draft: HTTP request failed.")
    return
end

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

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

-- The campaign identifies the newsletter. The primary activity identifies the email to test or schedule.
local primary = chilkat.JsonObject.new()
success = campaign:find_record2("campaign_activities", "role", "primary_email", true, primary)
if (not success) or (primary:json_type_of("campaign_activity_id") ~= 1) then
    print("No primary email activity ID in the response.")
    return
end

local selectedCampaign = campaign:string_of("campaign_id")
local selectedActivity = primary:string_of("campaign_activity_id")
if (selectedCampaign == "") or (selectedActivity == "") then
    print("Missing campaign or activity ID.")
    return
end

print("Campaign ID: " .. selectedCampaign)
print("Primary activity ID: " .. selectedActivity)
local selection = chilkat.JsonObject.new()
selection:update_string("name", campaign:string_of("name"))
selection:update_string("campaign_id", selectedCampaign)
selection:update_string("campaign_activity_id", selectedActivity)
success = selection:write_file("newsletter-selection.json")
if not success then
    print("Could not save newsletter-selection.json. Keep the IDs printed above.")
    return
end

print("Saved newsletter-selection.json.")