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

C++
#include <CkSecrets.h>
#include <CkJsonObject.h>
#include <CkHttp.h>
#include <CkHttpResponse.h>
#include <CkStringBuilder.h>

void ChilkatSample(void)
    {
    bool success = false;

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    CkSecrets vault;
    vault.put_Location("local_manager");
    vault.put_AutoSplit(true);
    CkJsonObject entry;
    entry.UpdateString("appName","ConstantContactV3");
    entry.UpdateString("service","OAuth2");
    entry.UpdateString("username","my-account");
    CkJsonObject tokens;
    bool loaded = vault.GetSecretJson(entry,tokens);
    if (loaded != true) {
        std::cout << "Run the desktop authorization example using this account and store." << "\r\n";
        return;
    }

    if (tokens.JsonTypeOf("access_token") != 1) {
        std::cout << "The saved authorization has no access token." << "\r\n";
        return;
    }

    CkHttp http;
    http.put_RequireSslCertVerify(true);
    http.put_AuthToken(tokens.stringOf("access_token"));
    CkHttpResponse resp;

    CkStringBuilder html;
    success = html.LoadFile("newsletter.html","utf-8");
    if (success != true) {
        std::cout << "Could not read newsletter.html." << "\r\n";
        return;
    }

    if (html.Contains("[[trackingImage]]",true) != true) {
        std::cout << "Add [[trackingImage]] inside the HTML body." << "\r\n";
        return;
    }

    CkJsonObject draft;
    draft.UpdateString("name","Workshop newsletter");
    draft.UpdateInt("email_campaign_activities[0].format_type",5);
    draft.UpdateString("email_campaign_activities[0].from_name","Workshop team");
    draft.UpdateString("email_campaign_activities[0].from_email","news@example.com");
    draft.UpdateString("email_campaign_activities[0].reply_to_email","workshops@example.com");
    draft.UpdateString("email_campaign_activities[0].subject","Upcoming workshops");
    draft.UpdateString("email_campaign_activities[0].html_content",html.getAsString());
    success = http.HttpJson("POST","https://api.cc.email/v3/emails",draft,"application/json",resp);
    if ((success != true) && (resp.get_StatusCode() <= 0)) {
        std::cout << "Create draft: HTTP request failed." << "\r\n";
        return;
    }

    if ((success != true) || (resp.get_StatusCode() != 201)) {
        std::cout << "Create draft: HTTP " << resp.get_StatusCode() << " " << resp.bodyStr() << "\r\n";
        return;
    }

    CkJsonObject campaign;
    success = campaign.Load(resp.bodyStr());
    if (success != true) {
        std::cout << "Expected a JSON object in the response." << "\r\n";
        return;
    }

    //  The campaign identifies the newsletter. The primary activity identifies the email to test or schedule.
    CkJsonObject primary;
    success = campaign.FindRecord2("campaign_activities","role","primary_email",true,primary);
    if ((success != true) || (primary.JsonTypeOf("campaign_activity_id") != 1)) {
        std::cout << "No primary email activity ID in the response." << "\r\n";
        return;
    }

    const char *selectedCampaign = campaign.stringOf("campaign_id");
    const char *selectedActivity = primary.stringOf("campaign_activity_id");
    if ((selectedCampaign == "") || (selectedActivity == "")) {
        std::cout << "Missing campaign or activity ID." << "\r\n";
        return;
    }

    std::cout << "Campaign ID: " << selectedCampaign << "\r\n";
    std::cout << "Primary activity ID: " << selectedActivity << "\r\n";
    CkJsonObject selection;
    selection.UpdateString("name",campaign.stringOf("name"));
    selection.UpdateString("campaign_id",selectedCampaign);
    selection.UpdateString("campaign_activity_id",selectedActivity);
    success = selection.WriteFile("newsletter-selection.json");
    if (success != true) {
        std::cout << "Could not save newsletter-selection.json. Keep the IDs printed above." << "\r\n";
        return;
    }

    std::cout << "Saved newsletter-selection.json." << "\r\n";
    }