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 Rust Downloads
let mut success = false;
// LIVE SEND: Scheduling this activity delivers email to the selected contact list.
// Assumes Chilkat is unlocked. See Global Unlock Sample.
let vault = chilkat::Secrets::new();
vault.set_location("local_manager");
vault.set_auto_split(true);
let entry = chilkat::JsonObject::new();
let _ = entry.update_string("appName", "ConstantContactV3");
let _ = entry.update_string("service", "OAuth2");
let _ = entry.update_string("username", "my-account");
let tokens = chilkat::JsonObject::new();
if vault.get_secret_json(&entry, &tokens).is_err() {
println!("Run the desktop authorization example using this account and store.");
return;
}
if tokens.json_type_of("access_token") != 1 {
println!("The saved authorization has no access token.");
return;
}
let http = chilkat::Http::new();
http.set_require_ssl_cert_verify(true);
http.set_auth_token(&tokens.string_of("access_token").unwrap_or_default());
let resp = chilkat::HttpResponse::new();
// Use the selection file from the draft or campaign lookup example for this account.
let selection = chilkat::JsonObject::new();
success = selection.load_file("newsletter-selection.json").is_ok();
let activity_id = selection.string_of("campaign_activity_id").unwrap_or_default();
if (!success) || (activity_id == "") {
println!("Select a campaign and save its primary activity ID first.");
return;
}
let _ = http.set_url_var("activity", &activity_id);
let action = "schedule".to_string();
if action == "unschedule" {
success = http.http_no_body("DELETE", "https://api.cc.email/v3/emails/activities/{$activity}/schedules", &resp).is_ok();
if (!success) && (resp.status_code() <= 0) {
println!("Remove schedule: HTTP request failed.");
return;
}
if (!success) || (resp.status_code() != 204) {
println!("Remove schedule: HTTP {} {}", resp.status_code(), resp.body_str());
return;
}
println!("Schedule removed for activity {}", activity_id);
return;
}
if action != "schedule" {
println!("Choose schedule or unschedule.");
return;
}
// Replace the placeholder with the intended future delivery time, e.g. 2030-05-15T09:00:00-05:00.
let delivery_time = "ENTER_FUTURE_TIMESTAMP_WITH_OFFSET".to_string();
let delivery = chilkat::DateTime::new();
let now = chilkat::DateTime::new();
let _ = now.set_from_current_system_time();
success = delivery.set_from_timestamp(&delivery_time).is_ok();
if (!success) || (delivery.diff_seconds(&now) <= 0) {
println!("Enter a valid future delivery time with a UTC offset or Z.");
return;
}
// Use the full display name. AddParam encodes spaces and punctuation.
let list_name = "Workshop announcements".to_string();
let query = chilkat::HttpRequest::new();
query.set_http_verb("GET");
query.add_param("name", &list_name);
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).is_ok();
if (!success) && (resp.status_code() <= 0) {
println!("Look up list: HTTP request failed.");
return;
}
if (!success) || (resp.status_code() != 200) {
println!("Look up list: HTTP {} {}", resp.status_code(), resp.body_str());
return;
}
let matches = chilkat::JsonObject::new();
success = matches.load(&resp.body_str()).is_ok();
if !success {
println!("Expected a JSON object in the response.");
return;
}
if matches.size_of_array("lists") == 0 {
println!("No active email list named {}", list_name);
return;
}
if (matches.size_of_array("lists") != 1) || (!matches.string_of_equals("lists[0].name", &list_name, true)) {
println!("Expected one exact list name.");
return;
}
let list_id = matches.string_of("lists[0].list_id").unwrap_or_default();
if list_id == "" {
println!("Missing list ID.");
return;
}
println!("{} | List ID: {} | Mailable contacts: {}", list_name, list_id, 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).is_ok();
if (!success) && (resp.status_code() <= 0) {
println!("Read draft: HTTP request failed.");
return;
}
if (!success) || (resp.status_code() != 200) {
println!("Read draft: HTTP {} {}", resp.status_code(), resp.body_str());
return;
}
let draft = chilkat::JsonObject::new();
success = draft.load(&resp.body_str()).is_ok();
if !success {
println!("Expected a JSON object in the response.");
return;
}
if (!draft.string_of_equals("role", "primary_email", true)) || (!draft.string_of_equals("current_status", "DRAFT", true)) {
println!("Select a primary activity in DRAFT status.");
return;
}
println!("LIVE SEND: {} | Activity: {} | List: {} | Delivery: {}", draft.string_of("subject").unwrap_or_default(), activity_id, list_name, delivery_time);
let _ = draft.update_new_array("contact_list_ids");
let _ = draft.update_string("contact_list_ids[0]", &list_id);
let _ = draft.update_new_array("segment_ids");
success = http.http_json("PUT", "https://api.cc.email/v3/emails/activities/{$activity}", &draft, "application/json", &resp).is_ok();
if (!success) && (resp.status_code() <= 0) {
println!("Assign audience: HTTP request failed.");
return;
}
if (!success) || (resp.status_code() != 200) {
println!("Assign audience: HTTP {} {}", resp.status_code(), resp.body_str());
return;
}
let updated = chilkat::JsonObject::new();
success = updated.load(&resp.body_str()).is_ok();
if !success {
println!("Expected a JSON object in the response.");
return;
}
if (updated.size_of_array("contact_list_ids") != 1) || ((!updated.string_of_equals("contact_list_ids[0]", &list_id, true)) || (updated.size_of_array("segment_ids") > 0)) {
println!("The saved audience did not match the requested list. Scheduling stopped.");
return;
}
let schedule = chilkat::JsonObject::new();
let _ = schedule.update_string("scheduled_date", &delivery.get_as_timestamp(false).unwrap_or_default());
success = http.http_json("POST", "https://api.cc.email/v3/emails/activities/{$activity}/schedules", &schedule, "application/json", &resp).is_ok();
if (!success) || ((resp.status_code() < 200) || (resp.status_code() >= 300)) {
println!("Schedule not confirmed. HTTP {}", resp.status_code());
println!("The audience was saved. Check the schedule before retrying.");
return;
}
println!("Live delivery scheduled for {} to {}", schedule.string_of("scheduled_date").unwrap_or_default(), list_name);