Sample code for 30+ languages & platforms
Unicode C 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 Unicode C Downloads

Unicode C
#include <C_CkHttpW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkBinDataW.h>
#include <C_CkHttpResponseW.h>

void ChilkatSample(void)
    {
    HCkHttpW http;
    HCkJsonObjectW message;
    HCkBinDataW fileData;
    BOOL loaded;
    HCkHttpResponseW response;

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    http = CkHttpW_Create();
    CkHttpW_putRequireSslCertVerify(http,TRUE);
    //  Supply a Mail Send API key from your application configuration. AuthToken adds the Bearer prefix.
    CkHttpW_putAuthToken(http,L"SENDGRID_API_KEY");

    message = CkJsonObjectW_Create();
    //  Replace the sender with a verified sender identity and the recipient with your destination.
    CkJsonObjectW_UpdateString(message,L"from.email",L"sender@example.com");
    CkJsonObjectW_UpdateString(message,L"personalizations[0].to[0].email",L"recipient@example.com");
    CkJsonObjectW_UpdateString(message,L"subject",L"Inspection photo");
    CkJsonObjectW_UpdateString(message,L"content[0].type",L"text/plain");
    CkJsonObjectW_UpdateString(message,L"content[0].value",L"The inspection is complete. The photo shows the repaired hinge.");

    //  SendGrid expects the plain text alternative before HTML.
    CkJsonObjectW_UpdateString(message,L"content[1].type",L"text/html");
    CkJsonObjectW_UpdateString(message,L"content[1].value",L"<p>The inspection is complete. The photo shows the repaired hinge.</p><img src=\"cid:hinge-photo\" alt=\"Repaired hinge\">");

    fileData = CkBinDataW_Create();
    loaded = CkBinDataW_LoadFile(fileData,L"inspection.jpg");
    if (loaded != TRUE) {
        wprintf(L"Could not read inspection.jpg. Check the path and file permissions.\n");
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(message);
        CkBinDataW_Dispose(fileData);
        return;
    }

    if (CkBinDataW_getNumBytes(fileData) == 0) {
        wprintf(L"inspection.jpg is empty.\n");
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(message);
        CkBinDataW_Dispose(fileData);
        return;
    }

    //  Encode the file bytes once. The content field contains only Base64, without a data URI prefix.
    CkJsonObjectW_UpdateString(message,L"attachments[0].content",CkBinDataW_getEncoded(fileData,L"base64"));
    CkJsonObjectW_UpdateString(message,L"attachments[0].type",L"image/jpeg");
    CkJsonObjectW_UpdateString(message,L"attachments[0].filename",L"inspection.jpg");
    CkJsonObjectW_UpdateString(message,L"attachments[0].disposition",L"inline");
    CkJsonObjectW_UpdateString(message,L"attachments[0].content_id",L"hinge-photo");

    response = CkHttpResponseW_Create();
    CkHttpW_HttpJson(http,L"POST",L"https://api.sendgrid.com/v3/mail/send",message,L"application/json",response);
    //  HttpJson also returns false for HTTP errors. A zero status means no HTTP response arrived.
    if (CkHttpResponseW_getStatusCode(response) == 0) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(message);
        CkBinDataW_Dispose(fileData);
        CkHttpResponseW_Dispose(response);
        return;
    }

    if (CkHttpResponseW_getStatusCode(response) != 202) {
        wprintf(L"Mail Send returned HTTP %d\n",CkHttpResponseW_getStatusCode(response));
        //  For validation errors the response body identifies the offending field and explains why.
        wprintf(L"%s\n",CkHttpResponseW_bodyStr(response));
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(message);
        CkBinDataW_Dispose(fileData);
        CkHttpResponseW_Dispose(response);
        return;
    }

    wprintf(L"SendGrid accepted the email for delivery (HTTP 202).\n");


    CkHttpW_Dispose(http);
    CkJsonObjectW_Dispose(message);
    CkBinDataW_Dispose(fileData);
    CkHttpResponseW_Dispose(response);

    }