PowerBuilder Requires Chilkat v11.0.0+
PowerBuilder
SendGrid: Send an order receipt with a Dynamic Template
See more SendGrid Examples
Activate a version of your receipt template and set its subject to Receipt for order {{order_number}}. Try this HTML in the template:
<p>Thank you, {{first_name}}.</p>
<ul>{{#each items}}<li>{{quantity}} × {{name}}</li>{{/each}}</ul>Replace the template ID, verified sender, and recipient. Template data is visible to the recipient. The separate, opaque notification reference is for matching delivery events to an application record; keep personal data out of custom arguments.
Chilkat PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_Http
oleobject loo_Resp
oleobject loo_Message
li_Success = 0
// Assumes Chilkat is unlocked. See Global Unlock Sample.
li_Success = 0
loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
if li_rc < 0 then
destroy loo_Http
MessageBox("Error","Connecting to COM object failed")
return
end if
loo_Http.RequireSslCertVerify = 1
// Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
loo_Http.AuthToken = "SENDGRID_API_KEY"
loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")
loo_Message = create oleobject
li_rc = loo_Message.ConnectToNewObject("Chilkat.JsonObject")
loo_Message.UpdateString("from.email","sender@example.com")
loo_Message.UpdateString("template_id","d-YOUR_ACTIVE_TEMPLATE_ID")
loo_Message.UpdateString("personalizations[0].to[0].email","recipient@example.com")
loo_Message.UpdateString("personalizations[0].dynamic_template_data.order_number","1042")
loo_Message.UpdateString("personalizations[0].dynamic_template_data.first_name","Avery")
loo_Message.UpdateString("personalizations[0].dynamic_template_data.items[0].name","Notebook")
loo_Message.UpdateInt("personalizations[0].dynamic_template_data.items[0].quantity",2)
loo_Message.UpdateString("personalizations[0].dynamic_template_data.items[1].name","Pen")
loo_Message.UpdateInt("personalizations[0].dynamic_template_data.items[1].quantity",1)
// This reference is returned on Event Webhook events; it is not a template variable.
loo_Message.UpdateString("personalizations[0].custom_args.notification_ref","ntf_8042")
li_Success = loo_Http.HttpJson("POST","https://api.sendgrid.com/v3/mail/send",loo_Message,"application/json",loo_Resp)
if li_Success <> 1 then
if loo_Resp.StatusCode = 0 then
Write-Debug loo_Http.LastErrorText
else
Write-Debug "HTTP " + string(loo_Resp.StatusCode) + ": " + loo_Resp.BodyStr
end if
destroy loo_Http
destroy loo_Resp
destroy loo_Message
return
end if
if loo_Resp.StatusCode <> 202 then
Write-Debug "HTTP " + string(loo_Resp.StatusCode) + ": " + loo_Resp.BodyStr
destroy loo_Http
destroy loo_Resp
destroy loo_Message
return
end if
Write-Debug "Accepted for delivery (HTTP 202)."
destroy loo_Http
destroy loo_Resp
destroy loo_Message