Sample code for 30+ languages & platforms
DataFlex Requires Chilkat v11.0.0+

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 DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Handle hoHttp
    Variant vMessage
    Handle hoMessage
    Boolean iSuccess
    Boolean iValidateOnly
    Integer iExpectedStatus
    Variant vResponse
    Handle hoResponse
    String sTemp1
    Integer iTemp1

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End
    Set ComRequireSslCertVerify Of hoHttp To True
    //  Supply a Mail Send API key from your application configuration. AuthToken adds the Bearer prefix.
    Set ComAuthToken Of hoHttp To "SENDGRID_API_KEY"

    Get Create (RefClass(cComChilkatJsonObject)) To hoMessage
    If (Not(IsComObjectCreated(hoMessage))) Begin
        Send CreateComObject of hoMessage
    End
    //  Replace the sender with a verified sender identity and the recipient with your destination.
    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 ComUpdateString Of hoMessage "subject" "Order confirmation" To iSuccess
    Get ComUpdateString Of hoMessage "content[0].type" "text/plain" To iSuccess
    Get ComUpdateString Of hoMessage "content[0].value" "We received your order #1042." To iSuccess

    //  Set to true to validate the request without delivering an email.
    Move False To iValidateOnly
    Get ComUpdateBool Of hoMessage "mail_settings.sandbox_mode.enable" iValidateOnly To iSuccess
    Move 202 To iExpectedStatus
    If (iValidateOnly = True) Begin
        Move 200 To iExpectedStatus
    End

    Get Create (RefClass(cComChilkatHttpResponse)) To hoResponse
    If (Not(IsComObjectCreated(hoResponse))) Begin
        Send CreateComObject of hoResponse
    End
    Get pvComObject of hoMessage to vMessage
    Get pvComObject of hoResponse to vResponse
    Get ComHttpJson Of hoHttp "POST" "https://api.sendgrid.com/v3/mail/send" vMessage "application/json" vResponse To iSuccess
    //  HttpJson also returns false for HTTP errors. A zero status means no HTTP response arrived.
    Get ComStatusCode Of hoResponse To iTemp1
    If (iTemp1 = 0) Begin
        Get ComLastErrorText Of hoHttp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComStatusCode Of hoResponse To iTemp1
    If (iTemp1 <> iExpectedStatus) Begin
        Get ComStatusCode Of hoResponse To iTemp1
        Showln "Mail Send returned HTTP " iTemp1
        //  For validation errors the response body identifies the offending field and explains why.
        Get ComBodyStr Of hoResponse To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    If (iValidateOnly = True) Begin
        Showln "Sandbox validation passed. No email was sent."
        Procedure_Return
    End

    Showln "SendGrid accepted the email for delivery (HTTP 202)."


End_Procedure