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

Constant Contact V3: Send a test email to reviewers

See more Constant Contact Examples

Read the primary activity ID from newsletter-selection.json. Replace the two reviewer addresses and send a preview with a personal note. Only the specified reviewers receive it. The request leaves the campaign schedule and contact lists alone.

Use up to five addresses per request. Each recipient counts toward the 50 daily test emails. Contact personalization and custom field values are not expanded in test messages. Success is HTTP 204 with an empty body.

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

-- 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 testMail = chilkat.JsonObject.new()
testMail:update_string("email_addresses[0]", "editor@example.com")
testMail:update_string("email_addresses[1]", "reviewer@example.com")
testMail:update_string("personal_message", "Please check the workshop dates and the registration link.")
success = http:http_json("POST", "https://api.cc.email/v3/emails/activities/{$activity}/tests", testMail, "application/json", resp)
if resp.status_code == 429 then
    print("HTTP 429: Test-send limit reached. Each reviewer counts toward the daily allowance.")
    return
end

if (not success) and (resp.status_code <= 0) then
    print("Send reviewer test: HTTP request failed.")
    return
end

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

print("Test email accepted for the two reviewer addresses.")