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

SendGrid: Embed an inline image in an HTML email

See more SendGrid Examples

Read inspection.jpg from the working directory and send it with an HTML inspection report. The image travels with the message: its content_id matches the cid: reference in the HTML. Supply plain text first and include useful alt text so the result is understandable even if the mail client hides images.

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

SendGrid documentation: Mail Send attachment fields.

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" "Inspection photo" To iSuccess
    Get ComUpdateString Of hoMessage "content[0].type" "text/plain" To iSuccess
    Get ComUpdateString Of hoMessage "content[0].value" "The inspection is complete. The photo shows the repaired hinge." To iSuccess

    //  SendGrid expects the plain text alternative before HTML.
    Get ComUpdateString Of hoMessage "content[1].type" "text/html" To iSuccess
    Get ComUpdateString Of hoMessage "content[1].value" '<p>The inspection is complete. The photo shows the repaired hinge.</p><img src="cid:hinge-photo" alt="Repaired hinge">' To iSuccess

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

    Get ComNumBytes Of hoFileData To iTemp1
    If (iTemp1 = 0) Begin
        Showln "inspection.jpg 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" "image/jpeg" To iSuccess
    Get ComUpdateString Of hoMessage "attachments[0].filename" "inspection.jpg" To iSuccess
    Get ComUpdateString Of hoMessage "attachments[0].disposition" "inline" To iSuccess
    Get ComUpdateString Of hoMessage "attachments[0].content_id" "hinge-photo" 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