B4X Requires Chilkat v11.0.0+
B4X
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 B4X Downloads
Dim success As Boolean = False
' Assumes Chilkat is unlocked. See Global Unlock Sample.
Dim vault As ChilkatSecrets
vault.Initialize("vault")
vault.Location = "local_manager"
vault.AutoSplit = True
Dim entry As ChilkatJsonObject
entry.Initialize
entry.UpdateString("appName", "ConstantContactV3")
entry.UpdateString("service", "OAuth2")
entry.UpdateString("username", "my-account")
Dim tokens As ChilkatJsonObject
tokens.Initialize
Dim loaded As Boolean = vault.GetSecretJson(entry, tokens)
If loaded <> True Then
Log("Run the desktop authorization example using this account and store.")
Return
End If
If tokens.JsonTypeOf("access_token") <> 1 Then
Log("The saved authorization has no access token.")
Return
End If
Dim http As ChilkatHttp
http.Initialize("http")
http.RequireSslCertVerify = True
http.AuthToken = tokens.StringOf("access_token")
Dim resp As ChilkatHttpResponse
resp.Initialize
Dim html As ChilkatStringBuilder
html.Initialize
success = html.LoadFile("newsletter.html", "utf-8")
If success <> True Then
Log("Could not read newsletter.html.")
Return
End If
If html.Contains("[[trackingImage]]", True) <> True Then
Log("Add [[trackingImage]] inside the HTML body.")
Return
End If
Dim draft As ChilkatJsonObject
draft.Initialize
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) And (resp.StatusCode <= 0) Then
Log("Create draft: HTTP request failed.")
Return
End If
If (success <> True) Or (resp.StatusCode <> 201) Then
Log("Create draft: HTTP " & resp.StatusCode & " " & resp.BodyStr)
Return
End If
Dim campaign As ChilkatJsonObject
campaign.Initialize
success = campaign.Load(resp.BodyStr)
If success <> True Then
Log("Expected a JSON object in the response.")
Return
End If
' The campaign identifies the newsletter. The primary activity identifies the email to test or schedule.
Dim primary As ChilkatJsonObject
primary.Initialize
success = campaign.FindRecord2("campaign_activities", "role", "primary_email", True, primary)
If (success <> True) Or (primary.JsonTypeOf("campaign_activity_id") <> 1) Then
Log("No primary email activity ID in the response.")
Return
End If
Dim selectedCampaign As String = campaign.StringOf("campaign_id")
Dim selectedActivity As String = primary.StringOf("campaign_activity_id")
If (selectedCampaign = "") Or (selectedActivity = "") Then
Log("Missing campaign or activity ID.")
Return
End If
Log("Campaign ID: " & selectedCampaign)
Log("Primary activity ID: " & selectedActivity)
Dim selection As ChilkatJsonObject
selection.Initialize
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 Then
Log("Could not save newsletter-selection.json. Keep the IDs printed above.")
Return
End If
Log("Saved newsletter-selection.json.")