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

Rust
let mut success = false;

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

let html = chilkat::StringBuilder::new();
success = html.load_file("newsletter.html", "utf-8").is_ok();
if !success {
    println!("Could not read newsletter.html.");
    return;
}

if !html.contains("[[trackingImage]]", true) {
    println!("Add [[trackingImage]] inside the HTML body.");
    return;
}

let draft = chilkat::JsonObject::new();
let _ = draft.update_string("name", "Workshop newsletter");
let _ = draft.update_int("email_campaign_activities[0].format_type", 5);
let _ = draft.update_string("email_campaign_activities[0].from_name", "Workshop team");
let _ = draft.update_string("email_campaign_activities[0].from_email", "news@example.com");
let _ = draft.update_string("email_campaign_activities[0].reply_to_email", "workshops@example.com");
let _ = draft.update_string("email_campaign_activities[0].subject", "Upcoming workshops");
let _ = draft.update_string("email_campaign_activities[0].html_content", &html.get_as_string().unwrap_or_default());
success = http.http_json("POST", "https://api.cc.email/v3/emails", &draft, "application/json", &resp).is_ok();
if (!success) && (resp.status_code() <= 0) {
    println!("Create draft: HTTP request failed.");
    return;
}

if (!success) || (resp.status_code() != 201) {
    println!("Create draft: HTTP {} {}", resp.status_code(), resp.body_str());
    return;
}

let campaign = chilkat::JsonObject::new();
success = campaign.load(&resp.body_str()).is_ok();
if !success {
    println!("Expected a JSON object in the response.");
    return;
}

// The campaign identifies the newsletter. The primary activity identifies the email to test or schedule.
let primary = chilkat::JsonObject::new();
success = campaign.find_record2("campaign_activities", "role", "primary_email", true, &primary).is_ok();
if (!success) || (primary.json_type_of("campaign_activity_id") != 1) {
    println!("No primary email activity ID in the response.");
    return;
}

let selected_campaign = campaign.string_of("campaign_id").unwrap_or_default();
let selected_activity = primary.string_of("campaign_activity_id").unwrap_or_default();
if (selected_campaign == "") || (selected_activity == "") {
    println!("Missing campaign or activity ID.");
    return;
}

println!("Campaign ID: {}", selected_campaign);
println!("Primary activity ID: {}", selected_activity);
let selection = chilkat::JsonObject::new();
let _ = selection.update_string("name", &campaign.string_of("name").unwrap_or_default());
let _ = selection.update_string("campaign_id", &selected_campaign);
let _ = selection.update_string("campaign_activity_id", &selected_activity);
if selection.write_file("newsletter-selection.json").is_err() {
    println!("Could not save newsletter-selection.json. Keep the IDs printed above.");
    return;
}

println!("Saved newsletter-selection.json.");