Unicode C
Unicode C
SendGrid HTML Email with Embedded Images
See more SendGrid Examples
Demonstrates how to send an HTML email with embedded images using SendGrid.Chilkat Unicode C Downloads
#include <C_CkHttpRequestW.h>
#include <C_CkHttpW.h>
#include <C_CkBinDataW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkHttpResponseW.h>
void ChilkatSample(void)
{
BOOL success;
HCkHttpRequestW req;
HCkHttpW http;
HCkBinDataW bdJpg;
HCkStringBuilderW sbHtml;
HCkJsonObjectW json;
HCkHttpResponseW resp;
success = FALSE;
// This requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code
req = CkHttpRequestW_Create();
http = CkHttpW_Create();
// First.. load a JPG file and build an HTML img tag with the JPG data inline encoded.
// Our HTML img tag will look like this:
// <img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEA3ADcAA..." alt="" style="border-width:0px;" />
bdJpg = CkBinDataW_Create();
success = CkBinDataW_LoadFile(bdJpg,L"qa_data/jpg/starfish.jpg");
if (success == FALSE) {
wprintf(L"Failed to load JPG file.\n");
CkHttpRequestW_Dispose(req);
CkHttpW_Dispose(http);
CkBinDataW_Dispose(bdJpg);
return;
}
// Note: HTML containing embedded base64 image data may not be visible in all email clients.
// For example, GMail might not display the image, but viewing in Outlook might be OK.
sbHtml = CkStringBuilderW_Create();
CkStringBuilderW_Append(sbHtml,L"<html><body><b>This is a test<b><br /><img src=\"data:image/jpeg;base64,");
// Append the base64 image data to sbHtml.
CkBinDataW_GetEncodedSb(bdJpg,L"base64",sbHtml);
CkStringBuilderW_Append(sbHtml,L"\" alt=\"\" style=\"border-width:0px;\" /></body></html>");
json = CkJsonObjectW_Create();
CkJsonObjectW_UpdateString(json,L"personalizations[0].to[0].email",L"matt@chilkat.io");
CkJsonObjectW_UpdateString(json,L"personalizations[0].to[0].name",L"Matt");
CkJsonObjectW_UpdateString(json,L"from.email",L"admin@chilkatsoft.com");
CkJsonObjectW_UpdateString(json,L"subject",L"Test HTML email with images");
CkJsonObjectW_UpdateString(json,L"content[0].type",L"text/html");
CkJsonObjectW_UpdateSb(json,L"content[0].value",sbHtml);
CkHttpW_putAuthToken(http,L"SENDGRID_API_KEY");
resp = CkHttpResponseW_Create();
success = CkHttpW_HttpJson(http,L"POST",L"https://api.sendgrid.com/v3/mail/send",json,L"application/json",resp);
if (success == FALSE) {
wprintf(L"%s\n",CkHttpW_lastErrorText(http));
CkHttpRequestW_Dispose(req);
CkHttpW_Dispose(http);
CkBinDataW_Dispose(bdJpg);
CkStringBuilderW_Dispose(sbHtml);
CkJsonObjectW_Dispose(json);
CkHttpResponseW_Dispose(resp);
return;
}
wprintf(L"response status code: %d\n",CkHttpResponseW_getStatusCode(resp));
// Display the response.
// If successful, the response code is 202 and the response body string is empty.
// (The response body string may also be empty for error response codes.)
wprintf(L"%s\n",CkHttpResponseW_bodyStr(resp));
CkHttpRequestW_Dispose(req);
CkHttpW_Dispose(http);
CkBinDataW_Dispose(bdJpg);
CkStringBuilderW_Dispose(sbHtml);
CkJsonObjectW_Dispose(json);
CkHttpResponseW_Dispose(resp);
}