AutoIt Requires Chilkat v11.0.0+
AutoIt
SendGrid: Send a plain text email to one recipient
See more SendGrid Examples
Send a plain text confirmation with one recipient, one subject, and one body. To try the request without sending mail, set validateOnly to true: sandbox mode returns HTTP 200 for a valid request. A normal send returns HTTP 202 when accepted; subsequent delivery is a separate event.
Use an API key with Mail Send permission and a verified sender identity. Replace the example addresses before sending.
SendGrid documentation: Sandbox mode.
Chilkat AutoIt Downloads
; Assumes Chilkat is unlocked. See Global Unlock Sample.
$oHttp = ObjCreate("Chilkat.Http")
$oHttp.RequireSslCertVerify = True
; Supply a Mail Send API key from your application configuration. AuthToken adds the Bearer prefix.
$oHttp.AuthToken = "SENDGRID_API_KEY"
$oMessage = ObjCreate("Chilkat.JsonObject")
; Replace the sender with a verified sender identity and the recipient with your destination.
$oMessage.UpdateString("from.email","sender@example.com")
$oMessage.UpdateString("personalizations[0].to[0].email","recipient@example.com")
$oMessage.UpdateString("subject","Order confirmation")
$oMessage.UpdateString("content[0].type","text/plain")
$oMessage.UpdateString("content[0].value","We received your order #1042.")
; Set to true to validate the request without delivering an email.
Local $bValidateOnly = False
$oMessage.UpdateBool("mail_settings.sandbox_mode.enable",$bValidateOnly)
Local $iExpectedStatus = 202
If ($bValidateOnly = True) Then
$iExpectedStatus = 200
EndIf
$oResponse = ObjCreate("Chilkat.HttpResponse")
$oHttp.HttpJson("POST","https://api.sendgrid.com/v3/mail/send",$oMessage,"application/json",$oResponse)
; HttpJson also returns false for HTTP errors. A zero status means no HTTP response arrived.
If ($oResponse.StatusCode = 0) Then
ConsoleWrite($oHttp.LastErrorText & @CRLF)
Exit
EndIf
If ($oResponse.StatusCode <> $iExpectedStatus) Then
ConsoleWrite("Mail Send returned HTTP " & $oResponse.StatusCode & @CRLF)
; For validation errors the response body identifies the offending field and explains why.
ConsoleWrite($oResponse.BodyStr & @CRLF)
Exit
EndIf
If ($bValidateOnly = True) Then
ConsoleWrite("Sandbox validation passed. No email was sent." & @CRLF)
Exit
EndIf
ConsoleWrite("SendGrid accepted the email for delivery (HTTP 202)." & @CRLF)