Rust Requires Chilkat v11.0.0+
Rust
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 Rust Downloads
// Assumes Chilkat is unlocked. See Global Unlock Sample.
let _ = false;
let http = chilkat::Http::new();
http.set_require_ssl_cert_verify(true);
// Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
http.set_auth_token("SENDGRID_API_KEY");
let resp = chilkat::HttpResponse::new();
// 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.
let batch_to_cancel = String::new();
if batch_to_cancel != "" {
let cancellation = chilkat::JsonObject::new();
let _ = cancellation.update_string("batch_id", &batch_to_cancel);
let _ = cancellation.update_string("status", "cancel");
if http.http_json("POST", "https://api.sendgrid.com/v3/user/scheduled_sends", &cancellation, "application/json", &resp).is_err() {
if resp.status_code() == 0 {
println!("{}", http.last_error_text());
} else {
println!("HTTP {}: {}", resp.status_code(), resp.body_str());
}
return;
}
if resp.status_code() != 201 {
println!("HTTP {}: {}", resp.status_code(), resp.body_str());
return;
}
println!("Batch cancellation accepted.");
return;
}
if http.http_no_body("POST", "https://api.sendgrid.com/v3/mail/batch", &resp).is_err() {
if resp.status_code() == 0 {
println!("{}", http.last_error_text());
} else {
println!("HTTP {}: {}", resp.status_code(), resp.body_str());
}
return;
}
if resp.status_code() != 201 {
println!("HTTP {}: {}", resp.status_code(), resp.body_str());
return;
}
let batch = chilkat::JsonObject::new();
if batch.load(&resp.body_str()).is_err() {
println!("Unexpected response: expected JSON.");
return;
}
let batch_id = batch.string_of("batch_id").unwrap_or_default();
if !batch.has_member("batch_id") {
println!("Missing batch_id in response.");
return;
}
// Allocate one batch per independently cancelable appointment.
println!("Save this batch ID with the appointment: {}", batch_id);
let send_time = chilkat::DateTime::new();
let _ = send_time.set_from_current_system_time();
let _ = send_time.add_seconds(3600);
let message = chilkat::JsonObject::new();
let _ = message.update_string("batch_id", &batch_id);
let _ = message.update_int("send_at", send_time.get_as_unix_time(false) as i32);
let _ = message.update_string("from.email", "sender@example.com");
let _ = message.update_string("personalizations[0].to[0].email", "recipient@example.com");
let _ = message.update_string("subject", "Appointment reminder");
let _ = message.update_string("content[0].type", "text/plain");
let _ = message.update_string("content[0].value", "Your appointment is tomorrow at 9:30 AM Central.");
if http.http_json("POST", "https://api.sendgrid.com/v3/mail/send", &message, "application/json", &resp).is_err() {
if resp.status_code() == 0 {
println!("{}", http.last_error_text());
} else {
println!("HTTP {}: {}", resp.status_code(), resp.body_str());
}
return;
}
if resp.status_code() != 202 {
println!("HTTP {}: {}", resp.status_code(), resp.body_str());
return;
}
println!("Reminder scheduled for {}", send_time.get_as_timestamp(false).unwrap_or_default());