PureBasic Requires Chilkat v11.0.0+
PureBasic
SendGrid: Send product updates with a topic unsubscribe link
See more SendGrid Examples
Use the numeric ID of your existing Product updates unsubscribe group and a verified sender. The ASM tag expands to the recipient-specific link for this group. A click opts the recipient out of this topic; a global unsubscribe applies across topics.
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,"personalizations[0].to[0].email","recipient@example.com")
CkJsonObject::ckUpdateInt(message,"asm.group_id",12345)
CkJsonObject::ckUpdateString(message,"subject","Product updates: November edition")
CkJsonObject::ckUpdateString(message,"content[0].type","text/html")
CkJsonObject::ckUpdateString(message,"content[0].value","<p>The new catalog is available.</p><p><a href=" + Chr(34) + "<%asm_group_unsubscribe_raw_url%>" + Chr(34) + ">Unsubscribe from product updates</a></p>")
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