Home > Back-end >  How can I embed an image into an email acceptable to Outlook
How can I embed an image into an email acceptable to Outlook

Time:11-11

I've got some working php code to embed an image into email, as follows:

<?php

// Initialise
$mailTo =  "[email protected]";    
$mailSubject = "Image Test";

// (Construct HTML header
$mailHead = implode("\r\n", [
  "MIME-Version: 1.0",
  "Content-type: text/html; charset=utf-8",
  "From: [email protected]"
]);

// Build email body - the image is directly embedded using Base64 Encode
$path = 'test.jpg';
$img = file_get_contents($path);
$imgdata = base64_encode($img);
$mailBody = "<p>Here is an image.</p>";
$mailBody = $mailBody . "<img src='data:image/jpg;base64," . $imgdata . "'/><br>";

// Send email
echo $mailBody;
echo mail($mailTo, $mailSubject, $mailBody, $mailHead)
  ? "OK" : "ERROR" ;

?>

This code displays the whole email, including the image, when you run it as a confirmation. I can receive this OK on my mobile using the default Email app, but in Outlook it doesn't come through. I get the small square to show an image is present, but right-clicking and selecting Download pictures shows nothing.

Can anyone suggest a fix or recommend a different method that will work on Outlook? Many thanks

CodePudding user response:

Most email clients do not support Base 64 images in the HTML. (See https://www.caniemail.com/features/image-base64/)

However, you can add your image as a CID embedded attachment to your email, and then link to that in your HTML email.

Here’s another Stack Overflow thread answering this question : How to embed images in html email

  • Related