Visual FoxPro Requires Chilkat v11.0.0+
Visual FoxPro
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 Visual FoxPro Downloads
LOCAL lnSuccess
LOCAL loHttp
LOCAL loResp
LOCAL lcBatchToCancel
LOCAL loCancellation
LOCAL loBatch
LOCAL lcBatchId
LOCAL loSendTime
LOCAL loMessage
lnSuccess = 0
* Assumes Chilkat is unlocked. See Global Unlock Sample.
lnSuccess = 0
loHttp = CreateObject('Chilkat.Http')
loHttp.RequireSslCertVerify = 1
* Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
loHttp.AuthToken = "SENDGRID_API_KEY"
loResp = CreateObject('Chilkat.HttpResponse')
* 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.
lcBatchToCancel = ""
IF (lcBatchToCancel <> "") THEN
loCancellation = CreateObject('Chilkat.JsonObject')
loCancellation.UpdateString("batch_id",lcBatchToCancel)
loCancellation.UpdateString("status","cancel")
lnSuccess = loHttp.HttpJson("POST","https://api.sendgrid.com/v3/user/scheduled_sends",loCancellation,"application/json",loResp)
IF (lnSuccess <> 1) THEN
IF (loResp.StatusCode = 0) THEN
? loHttp.LastErrorText
ELSE
? "HTTP " + STR(loResp.StatusCode) + ": " + loResp.BodyStr
ENDIF
RELEASE loHttp
RELEASE loResp
RELEASE loCancellation
CANCEL
ENDIF
IF (loResp.StatusCode <> 201) THEN
? "HTTP " + STR(loResp.StatusCode) + ": " + loResp.BodyStr
RELEASE loHttp
RELEASE loResp
RELEASE loCancellation
CANCEL
ENDIF
? "Batch cancellation accepted."
RELEASE loHttp
RELEASE loResp
RELEASE loCancellation
CANCEL
ENDIF
lnSuccess = loHttp.HttpNoBody("POST","https://api.sendgrid.com/v3/mail/batch",loResp)
IF (lnSuccess <> 1) THEN
IF (loResp.StatusCode = 0) THEN
? loHttp.LastErrorText
ELSE
? "HTTP " + STR(loResp.StatusCode) + ": " + loResp.BodyStr
ENDIF
RELEASE loHttp
RELEASE loResp
RELEASE loCancellation
CANCEL
ENDIF
IF (loResp.StatusCode <> 201) THEN
? "HTTP " + STR(loResp.StatusCode) + ": " + loResp.BodyStr
RELEASE loHttp
RELEASE loResp
RELEASE loCancellation
CANCEL
ENDIF
loBatch = CreateObject('Chilkat.JsonObject')
lnSuccess = loBatch.Load(loResp.BodyStr)
IF (lnSuccess <> 1) THEN
? "Unexpected response: expected JSON."
RELEASE loHttp
RELEASE loResp
RELEASE loCancellation
RELEASE loBatch
CANCEL
ENDIF
lcBatchId = loBatch.StringOf("batch_id")
IF (loBatch.HasMember("batch_id") <> 1) THEN
? "Missing batch_id in response."
RELEASE loHttp
RELEASE loResp
RELEASE loCancellation
RELEASE loBatch
CANCEL
ENDIF
* Allocate one batch per independently cancelable appointment.
? "Save this batch ID with the appointment: " + lcBatchId
loSendTime = CreateObject('Chilkat.CkDateTime')
loSendTime.SetFromCurrentSystemTime()
loSendTime.AddSeconds(3600)
loMessage = CreateObject('Chilkat.JsonObject')
loMessage.UpdateString("batch_id",lcBatchId)
loMessage.UpdateInt("send_at",loSendTime.GetAsUnixTime(0))
loMessage.UpdateString("from.email","sender@example.com")
loMessage.UpdateString("personalizations[0].to[0].email","recipient@example.com")
loMessage.UpdateString("subject","Appointment reminder")
loMessage.UpdateString("content[0].type","text/plain")
loMessage.UpdateString("content[0].value","Your appointment is tomorrow at 9:30 AM Central.")
lnSuccess = loHttp.HttpJson("POST","https://api.sendgrid.com/v3/mail/send",loMessage,"application/json",loResp)
IF (lnSuccess <> 1) THEN
IF (loResp.StatusCode = 0) THEN
? loHttp.LastErrorText
ELSE
? "HTTP " + STR(loResp.StatusCode) + ": " + loResp.BodyStr
ENDIF
RELEASE loHttp
RELEASE loResp
RELEASE loCancellation
RELEASE loBatch
RELEASE loSendTime
RELEASE loMessage
CANCEL
ENDIF
IF (loResp.StatusCode <> 202) THEN
? "HTTP " + STR(loResp.StatusCode) + ": " + loResp.BodyStr
RELEASE loHttp
RELEASE loResp
RELEASE loCancellation
RELEASE loBatch
RELEASE loSendTime
RELEASE loMessage
CANCEL
ENDIF
? "Reminder scheduled for " + loSendTime.GetAsTimestamp(0)
RELEASE loHttp
RELEASE loResp
RELEASE loCancellation
RELEASE loBatch
RELEASE loSendTime
RELEASE loMessage