2

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

<?php

// Initialise
$mailTo =  "john@auser.com";    
$mailSubject = "Image Test";

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

// 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

1 Answers1

2

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

HTeuMeuLeu
  • 1,851
  • 1
  • 7
  • 17
  • The link you sent recommends using PHPMailer. This works, but I should warn other users that it is not easy to set up. You first need an SCP client (I used WinSCP) to download and unzip PHPMailer on your site. Then see e.g. https://help.dreamhost.com/hc/en-us/articles/360031174411-PHPMailer-Installing-on-a-Shared-server – user3194684 Nov 10 '21 at 18:30