Sample code for 30+ languages & platforms
Delphi DLL

SendGrid HTML Email with Embedded Images

See more SendGrid Examples

Demonstrates how to send an HTML email with embedded images using SendGrid.

Chilkat Delphi DLL Downloads

Delphi DLL
uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Http, HttpResponse, StringBuilder, JsonObject, HttpRequest, BinData;

...

procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
req: HCkHttpRequest;
http: HCkHttp;
bdJpg: HCkBinData;
sbHtml: HCkStringBuilder;
json: HCkJsonObject;
resp: HCkHttpResponse;

begin
success := False;

// This requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code

req := CkHttpRequest_Create();
http := CkHttp_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 := CkBinData_Create();
success := CkBinData_LoadFile(bdJpg,'qa_data/jpg/starfish.jpg');
if (success = False) then
  begin
    Memo1.Lines.Add('Failed to load JPG file.');
    Exit;
  end;

// 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 := CkStringBuilder_Create();
CkStringBuilder_Append(sbHtml,'<html><body><b>This is a test<b><br /><img src="data:image/jpeg;base64,');
// Append the base64 image data to sbHtml.
CkBinData_GetEncodedSb(bdJpg,'base64',sbHtml);
CkStringBuilder_Append(sbHtml,'" alt="" style="border-width:0px;" /></body></html>');

json := CkJsonObject_Create();
CkJsonObject_UpdateString(json,'personalizations[0].to[0].email','matt@chilkat.io');
CkJsonObject_UpdateString(json,'personalizations[0].to[0].name','Matt');
CkJsonObject_UpdateString(json,'from.email','admin@chilkatsoft.com');
CkJsonObject_UpdateString(json,'subject','Test HTML email with images');
CkJsonObject_UpdateString(json,'content[0].type','text/html');
CkJsonObject_UpdateSb(json,'content[0].value',sbHtml);

CkHttp_putAuthToken(http,'SENDGRID_API_KEY');

resp := CkHttpResponse_Create();
success := CkHttp_HttpJson(http,'POST','https://api.sendgrid.com/v3/mail/send',json,'application/json',resp);
if (success = False) then
  begin
    Memo1.Lines.Add(CkHttp__lastErrorText(http));
    Exit;
  end;

Memo1.Lines.Add('response status code: ' + IntToStr(CkHttpResponse_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.)
Memo1.Lines.Add(CkHttpResponse__bodyStr(resp));

CkHttpRequest_Dispose(req);
CkHttp_Dispose(http);
CkBinData_Dispose(bdJpg);
CkStringBuilder_Dispose(sbHtml);
CkJsonObject_Dispose(json);
CkHttpResponse_Dispose(resp);

end;