PureBasic Requires Chilkat v11.0.0+
PureBasic
SendGrid: Send two private appointment reminders in one request
See more SendGrid Examples
Create an active reminder template with subject Appointment reminder and body Hello {{first_name}}, your appointment is {{appointment_time}}. Use your template ID, verified sender and two test addresses. Each recipient has a separate personalization; placing both addresses in a single To array would expose them to one another and share the same template data.
Chilkat PureBasic Downloads
IncludeFile "CkHttpResponse.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkJsonObject.pb"
Procedure ChilkatExample()
success.i = 0
; Assumes Chilkat is unlocked. See Global Unlock Sample.
success = 0
http.i = CkHttp::ckCreate()
If http.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkHttp::setCkRequireSslCertVerify(http, 1)
; Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
CkHttp::setCkAuthToken(http, "SENDGRID_API_KEY")
resp.i = CkHttpResponse::ckCreate()
If resp.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
message.i = CkJsonObject::ckCreate()
If message.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckUpdateString(message,"from.email","sender@example.com")
CkJsonObject::ckUpdateString(message,"template_id","d-YOUR_ACTIVE_TEMPLATE_ID")
; Each recipient gets a separate envelope and a private appointment time.
CkJsonObject::ckUpdateString(message,"personalizations[0].to[0].email","avery@example.com")
CkJsonObject::ckUpdateString(message,"personalizations[0].dynamic_template_data.first_name","Avery")
CkJsonObject::ckUpdateString(message,"personalizations[0].dynamic_template_data.appointment_time","Tuesday at 9:30 AM Central")
CkJsonObject::ckUpdateString(message,"personalizations[1].to[0].email","robin@example.com")
CkJsonObject::ckUpdateString(message,"personalizations[1].dynamic_template_data.first_name","Robin")
CkJsonObject::ckUpdateString(message,"personalizations[1].dynamic_template_data.appointment_time","Wednesday at 2:00 PM Central")
success = CkHttp::ckHttpJson(http,"POST","https://api.sendgrid.com/v3/mail/send",message,"application/json",resp)
If success <> 1
If CkHttpResponse::ckStatusCode(resp) = 0
Debug CkHttp::ckLastErrorText(http)
Else
Debug "HTTP " + Str(CkHttpResponse::ckStatusCode(resp)) + ": " + CkHttpResponse::ckBodyStr(resp)
EndIf
CkHttp::ckDispose(http)
CkHttpResponse::ckDispose(resp)
CkJsonObject::ckDispose(message)
ProcedureReturn
EndIf
If CkHttpResponse::ckStatusCode(resp) <> 202
Debug "HTTP " + Str(CkHttpResponse::ckStatusCode(resp)) + ": " + CkHttpResponse::ckBodyStr(resp)
CkHttp::ckDispose(http)
CkHttpResponse::ckDispose(resp)
CkJsonObject::ckDispose(message)
ProcedureReturn
EndIf
Debug "Accepted for delivery (HTTP 202)."
CkHttp::ckDispose(http)
CkHttpResponse::ckDispose(resp)
CkJsonObject::ckDispose(message)
ProcedureReturn
EndProcedure