Delphi ActiveX Requires Chilkat v11.0.0+
Delphi ActiveX
SendGrid: Create a newsletter draft, then schedule the reviewed draft
See more SendGrid Examples
Prepare a Marketing Campaigns design with a subject, content, and an unsubscribe module. Supply the design and list UUIDs, a verified marketing sender ID, and the unsubscribe group ID. The first run creates only a draft. After reviewing the audience and content, run the scheduling branch with that same ID. Single Sends use an ISO 8601 UTC string, unlike Mail Send’s numeric Unix timestamp.
Create a Single Send · Schedule a Single Send
Chilkat Delphi ActiveX Downloads
var
success: Integer;
http: TChilkatHttp;
resp: TChilkatHttpResponse;
reviewedDraftId: WideString;
draft: TChilkatJsonObject;
created: TChilkatJsonObject;
draftId: WideString;
releaseTime: TCkDateTime;
schedule: TChilkatJsonObject;
scheduled: TChilkatJsonObject;
begin
success := 0;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success := 0;
http := TChilkatHttp.Create(Self);
http.RequireSslCertVerify := 1;
// Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
http.AuthToken := 'SENDGRID_API_KEY';
resp := TChilkatHttpResponse.Create(Self);
// Leave this empty to create a draft. To schedule, supply the ID of an already reviewed draft.
reviewedDraftId := '';
if (reviewedDraftId = '') then
begin
draft := TChilkatJsonObject.Create(Self);
draft.UpdateString('name','November newsletter');
draft.UpdateString('send_to.list_ids[0]','YOUR_LIST_UUID');
draft.UpdateInt('email_config.sender_id',12345);
draft.UpdateInt('email_config.suppression_group_id',67890);
draft.UpdateString('email_config.design_id','YOUR_NEWSLETTER_DESIGN_UUID');
success := http.HttpJson('POST','https://api.sendgrid.com/v3/marketing/singlesends',draft.ControlInterface,'application/json',resp.ControlInterface);
if (success <> 1) then
begin
if (resp.StatusCode = 0) then
begin
Memo1.Lines.Add(http.LastErrorText);
end
else
begin
Memo1.Lines.Add('HTTP ' + IntToStr(resp.StatusCode) + ': ' + resp.BodyStr);
end;
Exit;
end;
if (resp.StatusCode <> 201) then
begin
Memo1.Lines.Add('HTTP ' + IntToStr(resp.StatusCode) + ': ' + resp.BodyStr);
Exit;
end;
created := TChilkatJsonObject.Create(Self);
success := created.Load(resp.BodyStr);
if (success <> 1) then
begin
Memo1.Lines.Add('Unexpected response: expected JSON.');
Exit;
end;
draftId := created.StringOf('id');
if (created.HasMember('id') <> 1) then
begin
Memo1.Lines.Add('Missing id in response.');
Exit;
end;
Memo1.Lines.Add('Draft created: ' + draftId + '. Review it in Marketing Campaigns before scheduling.');
Exit;
end;
// The example selects tomorrow at the current UTC time. Use your approved send time.
releaseTime := TCkDateTime.Create(Self);
releaseTime.SetFromCurrentSystemTime();
releaseTime.AddDays(1);
schedule := TChilkatJsonObject.Create(Self);
schedule.UpdateString('send_at',releaseTime.GetAsTimestamp(0));
http.SetUrlVar('draft',reviewedDraftId);
success := http.HttpJson('PUT','https://api.sendgrid.com/v3/marketing/singlesends/{$draft}/schedule',schedule.ControlInterface,'application/json',resp.ControlInterface);
if (success <> 1) then
begin
if (resp.StatusCode = 0) then
begin
Memo1.Lines.Add(http.LastErrorText);
end
else
begin
Memo1.Lines.Add('HTTP ' + IntToStr(resp.StatusCode) + ': ' + resp.BodyStr);
end;
Exit;
end;
if (resp.StatusCode <> 201) then
begin
Memo1.Lines.Add('HTTP ' + IntToStr(resp.StatusCode) + ': ' + resp.BodyStr);
Exit;
end;
scheduled := TChilkatJsonObject.Create(Self);
success := scheduled.Load(resp.BodyStr);
if (success <> 1) then
begin
Memo1.Lines.Add('Unexpected response: expected JSON.');
Exit;
end;
Memo1.Lines.Add(scheduled.StringOf('status') + ': ' + scheduled.StringOf('send_at'));