2010-06-28 124 views
0

有很多像这样的问题,但问题是他们每个人都建议使用某种第三方库。这对我来说不是一种选择,因为我们使用内部排队系统,在这个系统中,电子邮件会被发送到我们的数据库中,直到它被发送。如何手动在电子邮件中嵌入图像?

如何在不使用第三方软件的情况下将图像嵌入到电子邮件中?

+0

请将我们链接到您提到的潜在重复项。 – 2010-06-28 14:49:55

+0

http://stackoverflow.com/questions/1851728/how-to-embed-images-in-html-email是一个例子..但就像我说的这些*不*给我我想要的,所以我是不知道为什么你想要一个链接到他们。 – ryeguy 2010-06-28 14:54:19

回答

0

通常情况下,如果附加的图像使用以下MIME头的电子邮件,这将提供给text/html为图像:

Content-Type: image/jpeg 
Attachment-Disposition: inline; filename=MYIMAGE.JPG 

然后在邮件正文:

Content-Type: text/html; charset=utf-8 
Content-Transfer-Encoding: 7bit 

<html> 

    <!-- HTML tags --> 
    <img src="MYIMAGE.JPG" /> 
    <!-- more HTML tags --> 

</html> 
0

我正在使用香草PHP发送内嵌嵌入式图像的电子邮件。代码的核心是这样的:

$from = "$from_name <$from_email>"; 
$reply = "$replyto_name <$replyto_email>"; 
$to = "$to_name <$to_email>"; 

$main_boundary = substr(sha1(rand()), 0, 20); 
$part_boundary = substr(sha1(rand()), 0, 20); 

$headers = "From: $from\n"; 
$headers .= "Reply-To: $reply\n"; 
$headers .= "X-Mailer: PHP script mailer\n"; 
$headers .= "MIME-Version: 1.0\n"; 
$headers .= 'Content-Type: multipart/alternative;'."\n".' boundary="'.$main_boundary.'"'."\n"; 

$message= 
'This is a multi-part message in MIME format. 
--'.$main_boundary.' 
Content-Type: text/plain; charset=UTF-8; format=flowed 
Content-Transfer-Encoding: 7bit 

'.$msgTXT.' 

--'.$main_boundary.' 
Content-Type: multipart/related; 
boundary="'.$part_boundary.'" 


--'.$part_boundary.' 
Content-Type: text/html; UTF-8 
Content-Transfer-Encoding: 7bit 

'.$msgHTML.' 

--'.$part_boundary.' 
Content-Type: image/png; name="logo.png" 
Content-Transfer-Encoding: base64 
Content-Disposition: inline; filename="logo.png" 
Content-ID: <[email protected]> 

<base64 encoded image here> 
--'.$part_boundary.' 
Content-Type: image/gif; name="logo2.gif" 
Content-Transfer-Encoding: base64 
Content-Disposition: inline; filename="logo2.gif" 
Content-ID: <[email protected]> 

<base64 encoded image here> 
--'.$part_boundary.'-- 

--'.$main_boundary.'-- 

'; 

$mailsent = mail ($to, $subject, $message, $headers); 

什么是重要的:

  • 声明内容类型为多部分/替代
  • 使用2边界块作为标记/分隔电子邮件主边界和部分主要区域内电子邮件组件的边界。请注意,它发送文本和备用HTML消息。所有内联图像必须是base64编码的。请注意,\ r \ n \ r \ n应保持原样,以便代码正常工作,它充当RFC规范中定义的分隔符。用同样的方法可以使用这种机制发送带有附件的电子邮件,只需要选择适当的内容类型即可。
相关问题