2010-06-28 52 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規範中定義的分隔符。用同樣的方法可以使用這種機制發送帶有附件的電子郵件,只需要選擇適當的內容類型即可。
相關問題