Sample code for 30+ languages & platforms
Unicode C 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 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"Invoice 1042");
    CkJsonObjectW_UpdateString(message,L"content[0].type",L"text/plain");
    CkJsonObjectW_UpdateString(message,L"content[0].value",L"Your invoice is attached as invoice-1042.pdf.");

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

    if (CkBinDataW_getNumBytes(fileData) == 0) {
        wprintf(L"invoice.pdf 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"application/pdf");
    CkJsonObjectW_UpdateString(message,L"attachments[0].filename",L"invoice-1042.pdf");
    CkJsonObjectW_UpdateString(message,L"attachments[0].disposition",L"attachment");

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

    }