Dart Requires Chilkat v11.0.0+
Dart
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 Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
// Assumes Chilkat is unlocked. See Global Unlock Sample.
final http = CkHttp();
http.requireSslCertVerify = true;
// Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
http.authToken = 'SENDGRID_API_KEY';
final resp = CkHttpResponse();
// 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.
final batchToCancel = '';
if (batchToCancel != '') {
final cancellation = CkJsonObject();
cancellation.updateString('batch_id', batchToCancel);
cancellation.updateString('status', 'cancel');
try {
http.httpJson('POST', 'https://api.sendgrid.com/v3/user/scheduled_sends', cancellation, 'application/json', resp);
} on ChilkatException catch (e) {
if (resp.statusCode == 0) {
print(e.lastErrorText);
} else {
print('HTTP ${resp.statusCode}: ${resp.bodyStr}');
}
return;
}
if (resp.statusCode != 201) {
print('HTTP ${resp.statusCode}: ${resp.bodyStr}');
return;
}
print('Batch cancellation accepted.');
return;
}
try {
http.httpNoBody('POST', 'https://api.sendgrid.com/v3/mail/batch', resp);
} on ChilkatException catch (e) {
if (resp.statusCode == 0) {
print(e.lastErrorText);
} else {
print('HTTP ${resp.statusCode}: ${resp.bodyStr}');
}
return;
}
if (resp.statusCode != 201) {
print('HTTP ${resp.statusCode}: ${resp.bodyStr}');
return;
}
final batch = CkJsonObject();
try {
batch.load(resp.bodyStr);
} on ChilkatException {
print('Unexpected response: expected JSON.');
return;
}
final batchId = batch.stringOf('batch_id');
if (!batch.hasMember('batch_id')) {
print('Missing batch_id in response.');
return;
}
// Allocate one batch per independently cancelable appointment.
print('Save this batch ID with the appointment: $batchId');
final sendTime = CkDateTime();
sendTime.setFromCurrentSystemTime();
sendTime.addSeconds(3600);
final message = CkJsonObject();
message.updateString('batch_id', batchId);
message.updateInt('send_at', sendTime.getAsUnixTime(false));
message.updateString('from.email', 'sender@example.com');
message.updateString('personalizations[0].to[0].email', 'recipient@example.com');
message.updateString('subject', 'Appointment reminder');
message.updateString('content[0].type', 'text/plain');
message.updateString('content[0].value', 'Your appointment is tomorrow at 9:30 AM Central.');
try {
http.httpJson('POST', 'https://api.sendgrid.com/v3/mail/send', message, 'application/json', resp);
} on ChilkatException catch (e) {
if (resp.statusCode == 0) {
print(e.lastErrorText);
} else {
print('HTTP ${resp.statusCode}: ${resp.bodyStr}');
}
return;
}
if (resp.statusCode != 202) {
print('HTTP ${resp.statusCode}: ${resp.bodyStr}');
return;
}
print('Reminder scheduled for ${sendTime.getAsTimestamp(false)}');
}