Sample code for 30+ languages & platforms
DataFlex

SendGrid -- Send Email with Attachment

See more SendGrid Examples

Sends an email with an attachment.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoHttp
    Variant vJson
    Handle hoJson
    Handle hoBd
    Variant vResp
    Handle hoResp
    Integer iRespStatusCode
    String sTemp1

    Move False To iSuccess

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End

    // Implements the following CURL command:

    // curl --request POST \
    //   --url https://api.sendgrid.com/v3/mail/send \
    //   --header 'authorization: Bearer YOUR_API_KEY' \
    //   --header 'Content-Type: application/json' \
    //   --data '{"personalizations": [{"to": [{"email": "recipient@example.com"}]}],"from": {"email": "sender@example.com"},"subject":"Hello, World!","content": [{"type": "text/html","value": "Hey,<br>Please find attachment."}], "attachments": [{"content": "BASE64_ENCODED_CONTENT", "type": "text/plain", "filename": "attachment.txt"}]}'

    // Use the following online tool to generate HTTP code from a CURL command
    // Convert a cURL Command to HTTP Source Code

    // Use this online tool to generate code from sample JSON:
    // Generate Code to Create JSON

    // The following JSON is sent in the request body.

    // {
    //   "personalizations": [
    //     {
    //       "to": [
    //         {
    //           "email": "recipient@example.com"
    //         }
    //       ]
    //     }
    //   ],
    //   "from": {
    //     "email": "sender@example.com"
    //   },
    //   "subject": "Hello, World!",
    //   "content": [
    //     {
    //       "type": "text/html",
    //       "value": "Hey,<br>Please find attachment."
    //     }
    //   ],
    //   "attachments": [
    //     {
    //       "content": "BASE64_ENCODED_CONTENT",
    //       "type": "text/plain",
    //       "filename": "attachment.txt"
    //     }
    //   ]
    // }

    Get Create (RefClass(cComChilkatJsonObject)) To hoJson
    If (Not(IsComObjectCreated(hoJson))) Begin
        Send CreateComObject of hoJson
    End
    Get ComUpdateString Of hoJson "personalizations[0].to[0].email" "recipient@example.com" To iSuccess
    Get ComUpdateString Of hoJson "from.email" "sender@example.com" To iSuccess
    Get ComUpdateString Of hoJson "subject" "Hello, World!" To iSuccess
    Get ComUpdateString Of hoJson "content[0].type" "text/html" To iSuccess
    Get ComUpdateString Of hoJson "content[0].value" "Hey,<br>Please find attachment." To iSuccess

    // Load a file to be attached.
    Get Create (RefClass(cComChilkatBinData)) To hoBd
    If (Not(IsComObjectCreated(hoBd))) Begin
        Send CreateComObject of hoBd
    End
    Get ComLoadFile Of hoBd "qa_data/pdf/hello.pdf" To iSuccess

    Get ComGetEncoded Of hoBd "base64" To sTemp1
    Get ComUpdateString Of hoJson "attachments[0].content" sTemp1 To iSuccess
    Get ComUpdateString Of hoJson "attachments[0].type" "application/pdf" To iSuccess
    Get ComUpdateString Of hoJson "attachments[0].filename" "hello.pdf" To iSuccess

    // Adds the "Authorization: Bearer YOUR_API_KEY" header.
    Set ComAuthToken Of hoHttp To "YOUR_API_KEY"

    Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
    If (Not(IsComObjectCreated(hoResp))) Begin
        Send CreateComObject of hoResp
    End
    Get pvComObject of hoJson to vJson
    Get pvComObject of hoResp to vResp
    Get ComHttpJson Of hoHttp "POST" "https://api.sendgrid.com/v3/mail/send" vJson "application/json" vResp To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoHttp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComStatusCode Of hoResp To iRespStatusCode
    Showln "Response Status Code = " iRespStatusCode
    If (iRespStatusCode <> 202) Begin
        Showln "Response Header:"
        Get ComHeader Of hoResp To sTemp1
        Showln sTemp1
        Showln "Response Body:"
        Get ComBodyStr Of hoResp To sTemp1
        Showln sTemp1
        Showln "Failed."
        Procedure_Return
    End

    Showln "Success."


End_Procedure