Skip to content

Embedding Images in Emails

Andrew Theken edited this page Aug 12, 2015 · 3 revisions

Generally speaking, there are two major ways to include images in the html body of an email. We'll cover both below. (These examples also demonstrate using sending via our Template API, but the techniques work the same way for normal emails):

Option 1 - Link directly to the image:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <body>
    <center><img src="http://example.com/logo.png"/></center><br/>
    Hello {{name}}!
  </body>
</html>

This option is convenient, and doesn't require you to send the image attachment with each request, but it does require that you upload your logo to a publicly accessible web server before sending emails that reference the logo.png file.

Option 2 - Attach the image:

Step 1: Create a template that references cid:logo.png in the html markup:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <body>
    <center><img src="cid:logo.png"/></center><br/>
    Hello {{name}}!
  </body>
</html>
Step 2: Include the image as an attachment when using the template:
<?php
require_once('./vendor/autoload.php');

// Import the Postmark Client Classes
use Postmark\PostmarkClient;
use Postmark\Models\PostmarkAttachment;

// Create Client
$client = new PostmarkClient(<server token>);

$attachment = PostmarkAttachment::fromFile(
   dirname(__FILE__) . '/logo.png', "cid:logo.png", "image/png");

// Make a request
$sendResult = $client->sendEmailWithTemplate(
  "sender@example.com",
  "recipient@example.com", 
  <template-id>, 
  ["name" => "John Smith"], 
  true, NULL,true,NULL, NULL,NULL,NULL, 
  [$attachment]
);

echo $sendResult->message ."\r\n";
?>

This allows you to include the logo in the body of the content, and does not require an external webserver to host your logo, but it will increase the message payload size when sending your emails.