Dart Requires Chilkat v11.0.0+
Dart
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 Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
var success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
final vault = CkSecrets();
vault.location = 'local_manager';
vault.autoSplit = true;
final entry = CkJsonObject();
entry.updateString('appName', 'ConstantContactV3');
entry.updateString('service', 'OAuth2');
entry.updateString('username', 'my-account');
final tokens = CkJsonObject();
try {
vault.getSecretJson(entry, tokens);
} on ChilkatException {
print('Run the desktop authorization example using this account and store.');
return;
}
if (tokens.jsonTypeOf('access_token') != 1) {
print('The saved authorization has no access token.');
return;
}
final http = CkHttp();
http.requireSslCertVerify = true;
http.authToken = tokens.stringOf('access_token');
final resp = CkHttpResponse();
final html = CkStringBuilder();
try {
html.loadFile('newsletter.html', 'utf-8');
} on ChilkatException {
print('Could not read newsletter.html.');
return;
}
if (!html.contains('[[trackingImage]]', true)) {
print('Add [[trackingImage]] inside the HTML body.');
return;
}
final draft = CkJsonObject();
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 = true;
try {
http.httpJson('POST', 'https://api.cc.email/v3/emails', draft, 'application/json', resp);
} on ChilkatException {
success = false;
}
if ((!success) && (resp.statusCode <= 0)) {
print('Create draft: HTTP request failed.');
return;
}
if ((!success) || (resp.statusCode != 201)) {
print('Create draft: HTTP ${resp.statusCode} ${resp.bodyStr}');
return;
}
final campaign = CkJsonObject();
try {
campaign.load(resp.bodyStr);
} on ChilkatException {
print('Expected a JSON object in the response.');
return;
}
// The campaign identifies the newsletter. The primary activity identifies the email to test or schedule.
final primary = CkJsonObject();
success = true;
try {
campaign.findRecord2('campaign_activities', 'role', 'primary_email', true, primary);
} on ChilkatException {
success = false;
}
if ((!success) || (primary.jsonTypeOf('campaign_activity_id') != 1)) {
print('No primary email activity ID in the response.');
return;
}
final selectedCampaign = campaign.stringOf('campaign_id');
final selectedActivity = primary.stringOf('campaign_activity_id');
if ((selectedCampaign == '') || (selectedActivity == '')) {
print('Missing campaign or activity ID.');
return;
}
print('Campaign ID: $selectedCampaign');
print('Primary activity ID: $selectedActivity');
final selection = CkJsonObject();
selection.updateString('name', campaign.stringOf('name'));
selection.updateString('campaign_id', selectedCampaign);
selection.updateString('campaign_activity_id', selectedActivity);
try {
selection.writeFile('newsletter-selection.json');
} on ChilkatException {
print('Could not save newsletter-selection.json. Keep the IDs printed above.');
return;
}
print('Saved newsletter-selection.json.');
}