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

SendGrid: Attach a PDF invoice to an email

See more SendGrid Examples

Read invoice.pdf from the working directory and send it as a downloadable file named invoice-1042.pdf. SendGrid expects Base64 file content in the JSON request, not a path or a multipart upload. The whole message, including attachments, must be under 30 MB; allow for Base64 to add about a third to the file size.

Use an API key with Mail Send permission and a verified sender identity. Replace the example addresses before sending.

SendGrid documentation: Mail Send size limits.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Handle hoHttp
    Variant vMessage
    Handle hoMessage
    Boolean iSuccess
    Handle hoFileData
    Boolean iLoaded
    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" "Invoice 1042" To iSuccess
    Get ComUpdateString Of hoMessage "content[0].type" "text/plain" To iSuccess
    Get ComUpdateString Of hoMessage "content[0].value" "Your invoice is attached as invoice-1042.pdf." To iSuccess

    Get Create (RefClass(cComChilkatBinData)) To hoFileData
    If (Not(IsComObjectCreated(hoFileData))) Begin
        Send CreateComObject of hoFileData
    End
    Get ComLoadFile Of hoFileData "invoice.pdf" To iLoaded
    If (iLoaded <> True) Begin
        Showln "Could not read invoice.pdf. Check the path and file permissions."
        Procedure_Return
    End

    Get ComNumBytes Of hoFileData To iTemp1
    If (iTemp1 = 0) Begin
        Showln "invoice.pdf is empty."
        Procedure_Return
    End

    //  Encode the file bytes once. The content field contains only Base64, without a data URI prefix.
    Get ComGetEncoded Of hoFileData "base64" To sTemp1
    Get ComUpdateString Of hoMessage "attachments[0].content" sTemp1 To iSuccess
    Get ComUpdateString Of hoMessage "attachments[0].type" "application/pdf" To iSuccess
    Get ComUpdateString Of hoMessage "attachments[0].filename" "invoice-1042.pdf" To iSuccess
    Get ComUpdateString Of hoMessage "attachments[0].disposition" "attachment" To iSuccess

    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 <> 202) 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

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


End_Procedure