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

SendGrid: Send a blind copy (BCC) to an archive mailbox

See more SendGrid Examples

Send the customer a confirmation and give the archive mailbox a blind copy. The customer does not see the BCC address. Put both recipients in the same personalization to share the message. Each address must be unique across To, CC, and BCC within that personalization. Use separate personalizations for messages with different recipient-specific values.

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

SendGrid documentation: How personalizations work.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Handle hoHttp
    Variant vMessage
    Handle hoMessage
    Boolean iSuccess
    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
    //  Index 0 on both paths puts the visible recipient and blind copy on the same message.
    Get ComUpdateString Of hoMessage "personalizations[0].bcc[0].email" "archive@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

    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