Delphi DLL Requires Chilkat v11.0.0+
Delphi DLL
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 Delphi DLL Downloads
var
success: Boolean;
vault: HCkSecrets;
entry: HCkJsonObject;
tokens: HCkJsonObject;
loaded: Boolean;
http: HCkHttp;
resp: HCkHttpResponse;
html: HCkStringBuilder;
draft: HCkJsonObject;
campaign: HCkJsonObject;
primary: HCkJsonObject;
selectedCampaign: PWideChar;
selectedActivity: PWideChar;
selection: HCkJsonObject;
begin
success := False;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
vault := CkSecrets_Create();
CkSecrets_putLocation(vault,'local_manager');
CkSecrets_putAutoSplit(vault,True);
entry := CkJsonObject_Create();
CkJsonObject_UpdateString(entry,'appName','ConstantContactV3');
CkJsonObject_UpdateString(entry,'service','OAuth2');
CkJsonObject_UpdateString(entry,'username','my-account');
tokens := CkJsonObject_Create();
loaded := CkSecrets_GetSecretJson(vault,entry,tokens);
if (loaded <> True) then
begin
Memo1.Lines.Add('Run the desktop authorization example using this account and store.');
Exit;
end;
if (CkJsonObject_JsonTypeOf(tokens,'access_token') <> 1) then
begin
Memo1.Lines.Add('The saved authorization has no access token.');
Exit;
end;
http := CkHttp_Create();
CkHttp_putRequireSslCertVerify(http,True);
CkHttp_putAuthToken(http,CkJsonObject__stringOf(tokens,'access_token'));
resp := CkHttpResponse_Create();
html := CkStringBuilder_Create();
success := CkStringBuilder_LoadFile(html,'newsletter.html','utf-8');
if (success <> True) then
begin
Memo1.Lines.Add('Could not read newsletter.html.');
Exit;
end;
if (CkStringBuilder_Contains(html,'[[trackingImage]]',True) <> True) then
begin
Memo1.Lines.Add('Add [[trackingImage]] inside the HTML body.');
Exit;
end;
draft := CkJsonObject_Create();
CkJsonObject_UpdateString(draft,'name','Workshop newsletter');
CkJsonObject_UpdateInt(draft,'email_campaign_activities[0].format_type',5);
CkJsonObject_UpdateString(draft,'email_campaign_activities[0].from_name','Workshop team');
CkJsonObject_UpdateString(draft,'email_campaign_activities[0].from_email','news@example.com');
CkJsonObject_UpdateString(draft,'email_campaign_activities[0].reply_to_email','workshops@example.com');
CkJsonObject_UpdateString(draft,'email_campaign_activities[0].subject','Upcoming workshops');
CkJsonObject_UpdateString(draft,'email_campaign_activities[0].html_content',CkStringBuilder__getAsString(html));
success := CkHttp_HttpJson(http,'POST','https://api.cc.email/v3/emails',draft,'application/json',resp);
if ((success <> True) and (CkHttpResponse_getStatusCode(resp) <= 0)) then
begin
Memo1.Lines.Add('Create draft: HTTP request failed.');
Exit;
end;
if ((success <> True) or (CkHttpResponse_getStatusCode(resp) <> 201)) then
begin
Memo1.Lines.Add('Create draft: HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)) + ' ' + CkHttpResponse__bodyStr(resp));
Exit;
end;
campaign := CkJsonObject_Create();
success := CkJsonObject_Load(campaign,CkHttpResponse__bodyStr(resp));
if (success <> True) then
begin
Memo1.Lines.Add('Expected a JSON object in the response.');
Exit;
end;
// The campaign identifies the newsletter. The primary activity identifies the email to test or schedule.
primary := CkJsonObject_Create();
success := CkJsonObject_FindRecord2(campaign,'campaign_activities','role','primary_email',True,primary);
if ((success <> True) or (CkJsonObject_JsonTypeOf(primary,'campaign_activity_id') <> 1)) then
begin
Memo1.Lines.Add('No primary email activity ID in the response.');
Exit;
end;
selectedCampaign := CkJsonObject__stringOf(campaign,'campaign_id');
selectedActivity := CkJsonObject__stringOf(primary,'campaign_activity_id');
if ((selectedCampaign = '') or (selectedActivity = '')) then
begin
Memo1.Lines.Add('Missing campaign or activity ID.');
Exit;
end;
Memo1.Lines.Add('Campaign ID: ' + selectedCampaign);
Memo1.Lines.Add('Primary activity ID: ' + selectedActivity);
selection := CkJsonObject_Create();
CkJsonObject_UpdateString(selection,'name',CkJsonObject__stringOf(campaign,'name'));
CkJsonObject_UpdateString(selection,'campaign_id',selectedCampaign);
CkJsonObject_UpdateString(selection,'campaign_activity_id',selectedActivity);
success := CkJsonObject_WriteFile(selection,'newsletter-selection.json');
if (success <> True) then
begin
Memo1.Lines.Add('Could not save newsletter-selection.json. Keep the IDs printed above.');
Exit;
end;
Memo1.Lines.Add('Saved newsletter-selection.json.');
CkSecrets_Dispose(vault);
CkJsonObject_Dispose(entry);
CkJsonObject_Dispose(tokens);
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkStringBuilder_Dispose(html);
CkJsonObject_Dispose(draft);
CkJsonObject_Dispose(campaign);
CkJsonObject_Dispose(primary);
CkJsonObject_Dispose(selection);