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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoVault
    Variant vEntry
    Handle hoEntry
    Variant vTokens
    Handle hoTokens
    Boolean iLoaded
    Handle hoHttp
    Variant vResp
    Handle hoResp
    Handle hoHtml
    Variant vDraft
    Handle hoDraft
    Handle hoCampaign
    Variant vPrimary
    Handle hoPrimary
    String sSelectedCampaign
    String sSelectedActivity
    Handle hoSelection
    String sTemp1
    Integer iTemp1
    Boolean bTemp1

    Move False To iSuccess

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    Get Create (RefClass(cComChilkatSecrets)) To hoVault
    If (Not(IsComObjectCreated(hoVault))) Begin
        Send CreateComObject of hoVault
    End
    Set ComLocation Of hoVault To "local_manager"
    Set ComAutoSplit Of hoVault To True
    Get Create (RefClass(cComChilkatJsonObject)) To hoEntry
    If (Not(IsComObjectCreated(hoEntry))) Begin
        Send CreateComObject of hoEntry
    End
    Get ComUpdateString Of hoEntry "appName" "ConstantContactV3" To iSuccess
    Get ComUpdateString Of hoEntry "service" "OAuth2" To iSuccess
    Get ComUpdateString Of hoEntry "username" "my-account" To iSuccess
    Get Create (RefClass(cComChilkatJsonObject)) To hoTokens
    If (Not(IsComObjectCreated(hoTokens))) Begin
        Send CreateComObject of hoTokens
    End
    Get pvComObject of hoEntry to vEntry
    Get pvComObject of hoTokens to vTokens
    Get ComGetSecretJson Of hoVault vEntry vTokens To iLoaded
    If (iLoaded <> True) Begin
        Showln "Run the desktop authorization example using this account and store."
        Procedure_Return
    End

    Get ComJsonTypeOf Of hoTokens "access_token" To iTemp1
    If (iTemp1 <> 1) Begin
        Showln "The saved authorization has no access token."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End
    Set ComRequireSslCertVerify Of hoHttp To True
    Get ComStringOf Of hoTokens "access_token" To sTemp1
    Set ComAuthToken Of hoHttp To sTemp1
    Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
    If (Not(IsComObjectCreated(hoResp))) Begin
        Send CreateComObject of hoResp
    End

    Get Create (RefClass(cComChilkatStringBuilder)) To hoHtml
    If (Not(IsComObjectCreated(hoHtml))) Begin
        Send CreateComObject of hoHtml
    End
    Get ComLoadFile Of hoHtml "newsletter.html" "utf-8" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not read newsletter.html."
        Procedure_Return
    End

    Get ComContains Of hoHtml "[[trackingImage]]" True To bTemp1
    If (bTemp1 <> True) Begin
        Showln "Add [[trackingImage]] inside the HTML body."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoDraft
    If (Not(IsComObjectCreated(hoDraft))) Begin
        Send CreateComObject of hoDraft
    End
    Get ComUpdateString Of hoDraft "name" "Workshop newsletter" To iSuccess
    Get ComUpdateInt Of hoDraft "email_campaign_activities[0].format_type" 5 To iSuccess
    Get ComUpdateString Of hoDraft "email_campaign_activities[0].from_name" "Workshop team" To iSuccess
    Get ComUpdateString Of hoDraft "email_campaign_activities[0].from_email" "news@example.com" To iSuccess
    Get ComUpdateString Of hoDraft "email_campaign_activities[0].reply_to_email" "workshops@example.com" To iSuccess
    Get ComUpdateString Of hoDraft "email_campaign_activities[0].subject" "Upcoming workshops" To iSuccess
    Get ComGetAsString Of hoHtml To sTemp1
    Get ComUpdateString Of hoDraft "email_campaign_activities[0].html_content" sTemp1 To iSuccess
    Get pvComObject of hoDraft to vDraft
    Get pvComObject of hoResp to vResp
    Get ComHttpJson Of hoHttp "POST" "https://api.cc.email/v3/emails" vDraft "application/json" vResp To iSuccess
    Get ComStatusCode Of hoResp To iTemp1
    If ((iSuccess <> True) And (iTemp1 <= 0)) Begin
        Showln "Create draft: HTTP request failed."
        Procedure_Return
    End

    Get ComStatusCode Of hoResp To iTemp1
    If ((iSuccess <> True) Or (iTemp1 <> 201)) Begin
        Get ComStatusCode Of hoResp To iTemp1
        Get ComBodyStr Of hoResp To sTemp1
        Showln "Create draft: HTTP " iTemp1 " " sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoCampaign
    If (Not(IsComObjectCreated(hoCampaign))) Begin
        Send CreateComObject of hoCampaign
    End
    Get ComBodyStr Of hoResp To sTemp1
    Get ComLoad Of hoCampaign sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Expected a JSON object in the response."
        Procedure_Return
    End

    //  The campaign identifies the newsletter. The primary activity identifies the email to test or schedule.
    Get Create (RefClass(cComChilkatJsonObject)) To hoPrimary
    If (Not(IsComObjectCreated(hoPrimary))) Begin
        Send CreateComObject of hoPrimary
    End
    Get pvComObject of hoPrimary to vPrimary
    Get ComFindRecord2 Of hoCampaign "campaign_activities" "role" "primary_email" True vPrimary To iSuccess
    Get ComJsonTypeOf Of hoPrimary "campaign_activity_id" To iTemp1
    If ((iSuccess <> True) Or (iTemp1 <> 1)) Begin
        Showln "No primary email activity ID in the response."
        Procedure_Return
    End

    Get ComStringOf Of hoCampaign "campaign_id" To sSelectedCampaign
    Get ComStringOf Of hoPrimary "campaign_activity_id" To sSelectedActivity
    If ((sSelectedCampaign = "") Or (sSelectedActivity = "")) Begin
        Showln "Missing campaign or activity ID."
        Procedure_Return
    End

    Showln "Campaign ID: " sSelectedCampaign
    Showln "Primary activity ID: " sSelectedActivity
    Get Create (RefClass(cComChilkatJsonObject)) To hoSelection
    If (Not(IsComObjectCreated(hoSelection))) Begin
        Send CreateComObject of hoSelection
    End
    Get ComStringOf Of hoCampaign "name" To sTemp1
    Get ComUpdateString Of hoSelection "name" sTemp1 To iSuccess
    Get ComUpdateString Of hoSelection "campaign_id" sSelectedCampaign To iSuccess
    Get ComUpdateString Of hoSelection "campaign_activity_id" sSelectedActivity To iSuccess
    Get ComWriteFile Of hoSelection "newsletter-selection.json" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not save newsletter-selection.json. Keep the IDs printed above."
        Procedure_Return
    End

    Showln "Saved newsletter-selection.json."


End_Procedure