Search code examples
phpemailmime

PHP email including, but not displaying the inline image


It is including the encoded image in the email, but will not display it. All I get is a placeholder. I know about the libraries, but it just feels like I am sooooo close :)

It is an html email, with an attachment, and the inline image is just a banner at the top. Attachments are working properly.

<?php

date_default_timezone_set('America/Chicago');
$heading_s_date = date("F j, Y",strtotime($start_date));  //to get format 2018-11-31 only
$heading_e_date = date("F j, Y",strtotime($end_date));    //to get format 2018-11-31 only
$subject = 'Account Updater Report for: ' . $merchant_name . '--' . $start_date . ' to ' . $end_date;; 
$random_hash = md5(date('r', time())); 
$headers = "From: customerservice@calligraphydallas.com\r\nReply-To: customerservice@forte.net"; 
$headers .= "\r\nContent-Type: multipart/html; boundary=\"PHP-mixed-".$random_hash."\""; 
$image = 'banner010.jpg';
$attachment = chunk_split(base64_encode(file_get_contents($filename)));
$inline = chunk_split(base64_encode(file_get_contents($image)));
ob_start();
?>

This is a multi-part message in MIME format.

--PHP-mixed-<?php echo $random_hash; ?>  
Content-Type: multipart/mixed; boundary="PHP-alt-<?php echo $random_hash; ?>" 

--PHP-alt-<?php echo $random_hash; ?>  
Content-Type: text/plain; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit

Here is your Account Updater report.

--PHP-alt-<?php echo $random_hash; ?>  
Content-Type: image/jpg; name="banner010.jpg"
Content-Transfer-Encoding: base64  
Content-ID: <image_identifier>
Content-Disposition: inline; filename="banner010.jpg"

<?php $headers .= $inline."\r\n"; ?>  

--PHP-alt-<?php echo $random_hash; ?>  
Content-Type: text/html; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit

<a href="https://www.forte.net/"><img border="0" src="cid:image_identifier"></a><br>
<p><font face="Calibri"><span style="font-size: 15pt;"><b>Attached please find your Account Updater report.</b></span></font></p>

--PHP-alt-<?php echo $random_hash; ?>-- 

--PHP-mixed-<?php echo $random_hash; ?>  
Content-Type: application/text; name="<?php echo $filename; ?>"
Content-Transfer-Encoding: base64  
Content-Disposition: attachment 

--PHP-alt-<?php echo $random_hash; ?>  

<?php echo $attachment; ?> 
--PHP-mixed-<?php echo $random_hash; ?>-- 

<?php 
$message = ob_get_clean(); 
$mail_sent = @mail( $sendto, $subject, $message, $headers ); 
?>

Any help would be appreciated.


Solution

  • Here is what works referencing the image with a URL:

    <?php
    
    date_default_timezone_set('America/Chicago');
    $heading_s_date = date("F j, Y",strtotime($start_date));
    $heading_e_date = date("F j, Y",strtotime($end_date));
    $subject = 'Account Updater Report for: ' . $merchant_name . '--' . $start_date . ' to ' . $end_date;; 
    $random_hash = md5(date('r', time())); 
    $headers = "From: customerservice@calligraphydallas.com\r\nReply-To: customerservice@forte.net"; 
    $headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; 
    $attachment = chunk_split(base64_encode(file_get_contents($filename))); 
    ob_start();
    ?>
    --PHP-mixed-<?php echo $random_hash; ?>  
    Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>" 
    
    --PHP-alt-<?php echo $random_hash; ?>  
    Content-Type: text/plain; charset="iso-8859-1" 
    Content-Transfer-Encoding: 7bit
    
    Here is your Account Updater report. 
    
    --PHP-alt-<?php echo $random_hash; ?>  
    Content-Type: text/html; charset="iso-8859-1" 
    Content-Transfer-Encoding: 7bit
    
    <a href="https://www.forte.net/"><img border="0" src="http://www.calligraphydallas.com/updater/banner02.png" alt="Forte Payment Systems" width="804" height="166"></a><br>
    <p><font face="Calibri"><span style="font-size: 15pt;"><b>Attached please find your Account Updater report.</b></span></font></p>
    <p><font face="Calibri"><span style="font-size: 12pt;">For: <?php echo $merchant_name?></span></font><br>
    <font face="Calibri"><span style="font-size: 12pt;"><?php echo $heading_s_date . ' to ' . $heading_e_date; ?></span></font><br>
    <font face="Calibri"><span style="font-size: 12pt;">Created on: <?php echo date("F j, Y") . ' at ' . date("g:i a"); ?> CST</span></font><br><br>
    <font face="Calibri"><span style="font-size: 13pt;"><b>Forte Customer Service:</b></span><br>
    <span style="margin-left:50px; font-size:12pt;">7:00 am - 7:00 pm CST</span><br>
    <span style="margin-left:50px; font-size:12pt;">866.290.5400 option 1</span><br>
    <span style="margin-left:50px; font-size:12pt;">customerservice@forte.net</span></font></p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    
    --PHP-alt-<?php echo $random_hash; ?>-- 
    
    --PHP-mixed-<?php echo $random_hash; ?>  
    Content-Type: application/text; name="<?php echo $filename; ?>"
    Content-Transfer-Encoding: base64  
    Content-Disposition: attachment  
    
    <?php echo $attachment; ?> 
    --PHP-mixed-<?php echo $random_hash; ?>-- 
    
    <?php 
    $message = ob_get_clean(); 
    $mail_sent = @mail( $sendto, $subject, $message, $headers ); 
    ?>