insert image in mail body

insert image in mail body

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Embedding Images in PHP Mail: Beyond Base64

Attempting to embed an image directly into a plain text email sent via mail() is a common misconception. The standard mail() function delivers raw MIME parts, but it does not inherently understand how to inline resources unless you construct the specific headers and body structure yourself. When a user clicks "send" on a form that generates an image, simply attaching the file as an attachment or trying to reference a local path will result in broken links for the recipient. The solution lies in constructing a proper MIME multipart message with Content-ID headers.

To embed an image, you must switch from a simple text body to a multipart/related structure. This allows the email client to understand that certain parts of the message are actually images intended to be displayed inline, rather than downloaded as attachments. The core mechanism involves converting the image into a Base64 string and injecting it into the HTML body using a cid: reference.

Here is how you can construct this in PHP:

// Load the image file path relative to your project root
$imagePath = __DIR__ . '/assets/logo.png';

// Read the image file and convert it to a Base64 string
$imageData = file_get_contents($imagePath);
$imageContentId = uniqid() . '.png'; // Generate a unique ID for the image
$imageBase64 = chunk_split(base64_encode($imageData));

// Define the MIME boundary
$boundary = 'b1=' . md5(uniqid());

// Construct the HTML body with the cid reference
$emailBody = "
<html>
<body>
    <h2>Welcome to our service</h2>
    <p>Here is your logo:</p>
    <img src=\"cid:$imageContentId\" alt=\"Your Logo\">
</body>
</html>
";

// Construct the full MIME message
$message = "MIME-Version: 1.0\r\n";
$message .= "Content-Type: multipart/related; boundary=\"$boundary\"\r\n";
$message .= "\r\n";

// Part 1: The HTML Content
$message .= "--$boundary\r\n";
$message .= "Content-Type: text/html; charset=\"UTF-8\"\r\n";
$message .= "Content-Transfer-Encoding: base64\r\n";
$message .= "\r\n";
$message .= chunk_split(base64_encode($emailBody)) . "\r\n";

// Part 2: The Image Attachment (referenced by cid)
$message .= "--$boundary\r\n";
$message .= "Content-Type: image/png\r\n";
$message .= "Content-Transfer-Encoding: base64\r\n";
$message .= "Content-ID: <$imageContentId>\r\n";
$message .= "\r\n";
$message .= $imageBase64 . "\r\n";

// Part 3: Closing the multipart message
$message .= "--$boundary--\r\n";

// Send the email
mail($to, $subject, $message, $headers);

The critical decision here is generating a unique Content-ID for every image sent. Using uniqid() ensures that if you send multiple emails with images, the client doesn't cache the first image and display it incorrectly in subsequent emails. This uniqueness also prevents email clients from reusing local copies of images from previous messages, which often leads to broken links if the original server file path changes.

A significant trade-off with this approach is performance. Base64 encoding increases the size of the payload by approximately 33%, and embedding large images directly into the MIME body can push your emails close to or over standard SMTP limits (usually around 10MB). Furthermore, relying on mail() assumes you have a properly configured PHP mail server. If your environment lacks an MTA like Postfix or Exim, this method will fail silently.

For production applications, especially those built with frameworks like Laravel, it is often better to avoid handling raw MIME construction manually. The phpmailer library abstracts these complexities, allowing you to use the simpler addAttachment() method with the $inline flag set to true. This handles the boundary generation and header manipulation internally, reducing the chance of syntax errors in your HTML or MIME strings.

If you are managing high-volume email campaigns or require robust tracking and deliverability features, consider leveraging a specialized service. Services like those offered at https://laravelcompany.com can handle the infrastructure required for reliable image embedding without requiring you to manage SMTP servers or worry about Base64 size limits in your own codebase.

Our network