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

Lazarus Pascal
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.Http,
  Chilkat.Secrets,
  Chilkat.StringBuilder,
  Chilkat.HttpResponse,
  Chilkat.JsonObject;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  vault: TSecrets;
  entry: TJsonObject;
  tokens: TJsonObject;
  loaded: Boolean;
  http: THttp;
  resp: THttpResponse;
  html: TStringBuilder;
  draft: TJsonObject;
  campaign: TJsonObject;
  primary: TJsonObject;
  selectedCampaign: string;
  selectedActivity: string;
  selection: TJsonObject;

begin
  success := False;

  //  Assumes Chilkat is unlocked. See Global Unlock Sample.
  vault := TSecrets.Create;
  vault.Location := 'local_manager';
  vault.AutoSplit := True;
  entry := TJsonObject.Create;
  entry.UpdateString('appName','ConstantContactV3');
  entry.UpdateString('service','OAuth2');
  entry.UpdateString('username','my-account');
  tokens := TJsonObject.Create;
  loaded := vault.GetSecretJson(entry,tokens);
  if (loaded <> True) then
    begin
      WriteLn('Run the desktop authorization example using this account and store.');
      Exit;
    end;
  if (tokens.JsonTypeOf('access_token') <> 1) then
    begin
      WriteLn('The saved authorization has no access token.');
      Exit;
    end;
  http := THttp.Create;
  http.RequireSslCertVerify := True;
  http.AuthToken := tokens.StringOf('access_token');
  resp := THttpResponse.Create;

  html := TStringBuilder.Create;
  success := html.LoadFile('newsletter.html','utf-8');
  if (success <> True) then
    begin
      WriteLn('Could not read newsletter.html.');
      Exit;
    end;
  if (html.Contains('[[trackingImage]]',True) <> True) then
    begin
      WriteLn('Add [[trackingImage]] inside the HTML body.');
      Exit;
    end;
  draft := TJsonObject.Create;
  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
    begin
      WriteLn('Create draft: HTTP request failed.');
      Exit;
    end;
  if ((success <> True) or (resp.StatusCode <> 201)) then
    begin
      WriteLn('Create draft: HTTP ' + resp.StatusCode + ' ' + resp.BodyStr);
      Exit;
    end;
  campaign := TJsonObject.Create;
  success := campaign.Load(resp.BodyStr);
  if (success <> True) then
    begin
      WriteLn('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 := TJsonObject.Create;
  success := campaign.FindRecord2('campaign_activities','role','primary_email',True,primary);
  if ((success <> True) or (primary.JsonTypeOf('campaign_activity_id') <> 1)) then
    begin
      WriteLn('No primary email activity ID in the response.');
      Exit;
    end;
  selectedCampaign := campaign.StringOf('campaign_id');
  selectedActivity := primary.StringOf('campaign_activity_id');
  if ((selectedCampaign = '') or (selectedActivity = '')) then
    begin
      WriteLn('Missing campaign or activity ID.');
      Exit;
    end;
  WriteLn('Campaign ID: ' + selectedCampaign);
  WriteLn('Primary activity ID: ' + selectedActivity);
  selection := TJsonObject.Create;
  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
    begin
      WriteLn('Could not save newsletter-selection.json. Keep the IDs printed above.');
      Exit;
    end;
  WriteLn('Saved newsletter-selection.json.');


  vault.Free;
  entry.Free;
  tokens.Free;
  http.Free;
  resp.Free;
  html.Free;
  draft.Free;
  campaign.Free;
  primary.Free;
  selection.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.