PHP 电子邮件包括但不显示内联图像

Posted

技术标签:

【中文标题】PHP 电子邮件包括但不显示内联图像【英文标题】:PHP email including, but not displaying the inline image 【发布时间】:2018-08-11 07:35:48 【问题描述】:

它在电子邮件中包含编码图像,但不会显示它。我得到的只是一个占位符。我知道图书馆,但感觉就像我很接近:)

这是一封带有附件的 html 电子邮件,而内嵌图片只是顶部的横幅。附件工作正常。

<?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 ); 
?>

任何帮助将不胜感激。

【问题讨论】:

【参考方案1】:

以下是使用 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"   ></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 ); 
?>

所以是的....现在我只想将横幅图像嵌入内联而不是链接到它。

帮个忙?

【讨论】:

这是一个解决方案吗?【参考方案2】:

天哪,我搞定了,哈哈

<?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/mixed; boundary=\"PHP-mixed-".$random_hash."\""; 
$image = 'banner02.png';
$attachment = chunk_split(base64_encode(file_get_contents($filename))); 
$banner = chunk_split(base64_encode(file_get_contents($image))); 
ob_start();
?>
--PHP-mixed-<?php echo $random_hash; ?>  
Content-Type: multipart/related; boundary="PHP-rel-<?php echo $random_hash; ?>"  

--PHP-rel-<?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: quoted-printable  

Here is your Account Updater report.  

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

<a href="https://www.forte.net/"><img border="0" src="cid:image_identifier" title="Forte Payment Systems"   ></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 &nbsp;option 1</span><br>
<span style="margin-left:50px; font-size:12pt;"><a href="mailto:customerservice@forte.net">customerservice@forte.net</a></span></font><br>
<span style="margin-left:50px; font-size:12pt;"><a href="https://www.forte.net/">www.forte.net</a></span></font></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>

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

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

<?php echo $banner; ?>

--PHP-rel-<?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 ); 
?>

【讨论】:

以上是关于PHP 电子邮件包括但不显示内联图像的主要内容,如果未能解决你的问题,请参考以下文章

在 HTML 电子邮件 Django 中显示内联图像

当我们使用 phpMailer 发送带有动态内容的邮件时,如何在电子邮件正文中显示多个内联图像

Python将内联图像添加到多部分/替代电子邮件

如何使用 Python/Django 在电子邮件中发送内联图像? [复制]

如何使用 Python/Django 在电子邮件中发送内联图像? [复制]

使用 c# windows 应用程序在电子邮件正文中添加多个图像(内联)