Delphi DLL Requires Chilkat v11.0.0+
Delphi DLL
SendGrid: Schedule a reminder and cancel it by batch ID
See more SendGrid Examples
Schedule a reminder one hour from now. Save its batch ID when it is allocated. If the appointment is canceled, use that saved ID in the cancellation branch. Mail Send accepts a Unix timestamp up to 72 hours ahead; cancel at least 10 minutes before the send time. Cancellation applies to the entire batch.
Create a batch ID ยท Cancel a batch
Chilkat Delphi DLL Downloads
var
success: Boolean;
http: HCkHttp;
resp: HCkHttpResponse;
batchToCancel: PWideChar;
cancellation: HCkJsonObject;
batch: HCkJsonObject;
batchId: PWideChar;
sendTime: HCkDateTime;
message: HCkJsonObject;
begin
success := False;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success := False;
http := CkHttp_Create();
CkHttp_putRequireSslCertVerify(http,True);
// Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
CkHttp_putAuthToken(http,'SENDGRID_API_KEY');
resp := CkHttpResponse_Create();
// On cancellation, set this to the batch ID saved with the original appointment.
// This branch does not create a new batch or send another reminder.
batchToCancel := '';
if (batchToCancel <> '') then
begin
cancellation := CkJsonObject_Create();
CkJsonObject_UpdateString(cancellation,'batch_id',batchToCancel);
CkJsonObject_UpdateString(cancellation,'status','cancel');
success := CkHttp_HttpJson(http,'POST','https://api.sendgrid.com/v3/user/scheduled_sends',cancellation,'application/json',resp);
if (success <> True) then
begin
if (CkHttpResponse_getStatusCode(resp) = 0) then
begin
Memo1.Lines.Add(CkHttp__lastErrorText(http));
end
else
begin
Memo1.Lines.Add('HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)) + ': ' + CkHttpResponse__bodyStr(resp));
end;
Exit;
end;
if (CkHttpResponse_getStatusCode(resp) <> 201) then
begin
Memo1.Lines.Add('HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)) + ': ' + CkHttpResponse__bodyStr(resp));
Exit;
end;
Memo1.Lines.Add('Batch cancellation accepted.');
Exit;
end;
success := CkHttp_HttpNoBody(http,'POST','https://api.sendgrid.com/v3/mail/batch',resp);
if (success <> True) then
begin
if (CkHttpResponse_getStatusCode(resp) = 0) then
begin
Memo1.Lines.Add(CkHttp__lastErrorText(http));
end
else
begin
Memo1.Lines.Add('HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)) + ': ' + CkHttpResponse__bodyStr(resp));
end;
Exit;
end;
if (CkHttpResponse_getStatusCode(resp) <> 201) then
begin
Memo1.Lines.Add('HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)) + ': ' + CkHttpResponse__bodyStr(resp));
Exit;
end;
batch := CkJsonObject_Create();
success := CkJsonObject_Load(batch,CkHttpResponse__bodyStr(resp));
if (success <> True) then
begin
Memo1.Lines.Add('Unexpected response: expected JSON.');
Exit;
end;
batchId := CkJsonObject__stringOf(batch,'batch_id');
if (CkJsonObject_HasMember(batch,'batch_id') <> True) then
begin
Memo1.Lines.Add('Missing batch_id in response.');
Exit;
end;
// Allocate one batch per independently cancelable appointment.
Memo1.Lines.Add('Save this batch ID with the appointment: ' + batchId);
sendTime := CkDateTime_Create();
CkDateTime_SetFromCurrentSystemTime(sendTime);
CkDateTime_AddSeconds(sendTime,3600);
message := CkJsonObject_Create();
CkJsonObject_UpdateString(message,'batch_id',batchId);
CkJsonObject_UpdateInt(message,'send_at',CkDateTime_GetAsUnixTime(sendTime,False));
CkJsonObject_UpdateString(message,'from.email','sender@example.com');
CkJsonObject_UpdateString(message,'personalizations[0].to[0].email','recipient@example.com');
CkJsonObject_UpdateString(message,'subject','Appointment reminder');
CkJsonObject_UpdateString(message,'content[0].type','text/plain');
CkJsonObject_UpdateString(message,'content[0].value','Your appointment is tomorrow at 9:30 AM Central.');
success := CkHttp_HttpJson(http,'POST','https://api.sendgrid.com/v3/mail/send',message,'application/json',resp);
if (success <> True) then
begin
if (CkHttpResponse_getStatusCode(resp) = 0) then
begin
Memo1.Lines.Add(CkHttp__lastErrorText(http));
end
else
begin
Memo1.Lines.Add('HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)) + ': ' + CkHttpResponse__bodyStr(resp));
end;
Exit;
end;
if (CkHttpResponse_getStatusCode(resp) <> 202) then
begin
Memo1.Lines.Add('HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)) + ': ' + CkHttpResponse__bodyStr(resp));
Exit;
end;
Memo1.Lines.Add('Reminder scheduled for ' + CkDateTime__getAsTimestamp(sendTime,False));
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(cancellation);
CkJsonObject_Dispose(batch);
CkDateTime_Dispose(sendTime);
CkJsonObject_Dispose(message);