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

AutoIt
Local $bSuccess = False

;  Assumes Chilkat is unlocked. See Global Unlock Sample.
$oVault = ObjCreate("Chilkat.Secrets")
$oVault.Location = "local_manager"
$oVault.AutoSplit = True
$oEntry = ObjCreate("Chilkat.JsonObject")
$oEntry.UpdateString("appName","ConstantContactV3")
$oEntry.UpdateString("service","OAuth2")
$oEntry.UpdateString("username","my-account")
$oTokens = ObjCreate("Chilkat.JsonObject")
Local $bLoaded = $oVault.GetSecretJson($oEntry,$oTokens)
If ($bLoaded <> True) Then
    ConsoleWrite("Run the desktop authorization example using this account and store." & @CRLF)
    Exit
EndIf

If ($oTokens.JsonTypeOf("access_token") <> 1) Then
    ConsoleWrite("The saved authorization has no access token." & @CRLF)
    Exit
EndIf

$oHttp = ObjCreate("Chilkat.Http")
$oHttp.RequireSslCertVerify = True
$oHttp.AuthToken = $oTokens.StringOf("access_token")
$oResp = ObjCreate("Chilkat.HttpResponse")

$oHtml = ObjCreate("Chilkat.StringBuilder")
$bSuccess = $oHtml.LoadFile("newsletter.html","utf-8")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not read newsletter.html." & @CRLF)
    Exit
EndIf

If ($oHtml.Contains("[[trackingImage]]",True) <> True) Then
    ConsoleWrite("Add [[trackingImage]] inside the HTML body." & @CRLF)
    Exit
EndIf

$oDraft = ObjCreate("Chilkat.JsonObject")
$oDraft.UpdateString("name","Workshop newsletter")
$oDraft.UpdateInt("email_campaign_activities[0].format_type",5)
$oDraft.UpdateString("email_campaign_activities[0].from_name","Workshop team")
$oDraft.UpdateString("email_campaign_activities[0].from_email","news@example.com")
$oDraft.UpdateString("email_campaign_activities[0].reply_to_email","workshops@example.com")
$oDraft.UpdateString("email_campaign_activities[0].subject","Upcoming workshops")
$oDraft.UpdateString("email_campaign_activities[0].html_content",$oHtml.GetAsString())
$bSuccess = $oHttp.HttpJson("POST","https://api.cc.email/v3/emails",$oDraft,"application/json",$oResp)
If (($bSuccess <> True) And ($oResp.StatusCode <= 0)) Then
    ConsoleWrite("Create draft: HTTP request failed." & @CRLF)
    Exit
EndIf

If (($bSuccess <> True) Or ($oResp.StatusCode <> 201)) Then
    ConsoleWrite("Create draft: HTTP " & $oResp.StatusCode & " " & $oResp.BodyStr & @CRLF)
    Exit
EndIf

$oCampaign = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oCampaign.Load($oResp.BodyStr)
If ($bSuccess <> True) Then
    ConsoleWrite("Expected a JSON object in the response." & @CRLF)
    Exit
EndIf

;  The campaign identifies the newsletter. The primary activity identifies the email to test or schedule.
$oPrimary = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oCampaign.FindRecord2("campaign_activities","role","primary_email",True,$oPrimary)
If (($bSuccess <> True) Or ($oPrimary.JsonTypeOf("campaign_activity_id") <> 1)) Then
    ConsoleWrite("No primary email activity ID in the response." & @CRLF)
    Exit
EndIf

Local $selectedCampaign = $oCampaign.StringOf("campaign_id")
Local $selectedActivity = $oPrimary.StringOf("campaign_activity_id")
If (($selectedCampaign = "") Or ($selectedActivity = "")) Then
    ConsoleWrite("Missing campaign or activity ID." & @CRLF)
    Exit
EndIf

ConsoleWrite("Campaign ID: " & $selectedCampaign & @CRLF)
ConsoleWrite("Primary activity ID: " & $selectedActivity & @CRLF)
$oSelection = ObjCreate("Chilkat.JsonObject")
$oSelection.UpdateString("name",$oCampaign.StringOf("name"))
$oSelection.UpdateString("campaign_id",$selectedCampaign)
$oSelection.UpdateString("campaign_activity_id",$selectedActivity)
$bSuccess = $oSelection.WriteFile("newsletter-selection.json")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not save newsletter-selection.json. Keep the IDs printed above." & @CRLF)
    Exit
EndIf

ConsoleWrite("Saved newsletter-selection.json." & @CRLF)