DataFlex Requires Chilkat v11.0.0+
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoHttp
Variant vResp
Handle hoResp
Variant vMessage
Handle hoMessage
String sTemp1
Integer iTemp1
Move False To iSuccess
// Assumes Chilkat is unlocked. See Global Unlock Sample.
Move False To iSuccess
Get Create (RefClass(cComChilkatHttp)) To hoHttp
If (Not(IsComObjectCreated(hoHttp))) Begin
Send CreateComObject of hoHttp
End
Set ComRequireSslCertVerify Of hoHttp To True
// Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
Set ComAuthToken Of hoHttp To "SENDGRID_API_KEY"
Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
If (Not(IsComObjectCreated(hoResp))) Begin
Send CreateComObject of hoResp
End
Get Create (RefClass(cComChilkatJsonObject)) To hoMessage
If (Not(IsComObjectCreated(hoMessage))) Begin
Send CreateComObject of hoMessage
End
Get ComUpdateString Of hoMessage "from.email" "sender@example.com" To iSuccess
Get ComUpdateString Of hoMessage "personalizations[0].to[0].email" "recipient@example.com" To iSuccess
Get ComUpdateInt Of hoMessage "asm.group_id" 12345 To iSuccess
Get ComUpdateString Of hoMessage "subject" "Product updates: November edition" To iSuccess
Get ComUpdateString Of hoMessage "content[0].type" "text/html" To iSuccess
Get ComUpdateString Of hoMessage "content[0].value" '<p>The new catalog is available.</p><p><a href="<%asm_group_unsubscribe_raw_url%>">Unsubscribe from product updates</a></p>' To iSuccess
Get pvComObject of hoMessage to vMessage
Get pvComObject of hoResp to vResp
Get ComHttpJson Of hoHttp "POST" "https://api.sendgrid.com/v3/mail/send" vMessage "application/json" vResp To iSuccess
If (iSuccess <> True) Begin
Get ComStatusCode Of hoResp To iTemp1
If (iTemp1 = 0) Begin
Get ComLastErrorText Of hoHttp To sTemp1
Showln sTemp1
End
Else Begin
Get ComStatusCode Of hoResp To iTemp1
Get ComBodyStr Of hoResp To sTemp1
Showln "HTTP " iTemp1 ": " sTemp1
End
Procedure_Return
End
Get ComStatusCode Of hoResp To iTemp1
If (iTemp1 <> 202) Begin
Get ComStatusCode Of hoResp To iTemp1
Get ComBodyStr Of hoResp To sTemp1
Showln "HTTP " iTemp1 ": " sTemp1
Procedure_Return
End
Showln "Accepted for delivery (HTTP 202)."
End_Procedure