2015-05-20 110 views
8

用內聯附件和非內聯附件創建HTML-Email的正確方法是什麼?帶內聯附件和非內聯附件的HTML-Email

此外,請告訴我什麼內容類型只能使用內聯附件和只有非內聯附件。

到現在爲止,我沒有這樣說:

MIME-Version: 1.0 
[some more headers] 
Content-type: multipart/mixed; 
boundary="myboundary" 
--myboundary 
Content-Type: text/html; charset=ISO-8859-15 
Content-Transfer-Encoding: 7bit 

[html with img cid:my_image] 

--myboundary 
Content-Type: image/png; 
name="my_image.png" 
Content-Transfer-Encoding: base64 
Content-ID: <my_image> 
Content-Disposition: inline; 
filename="my_image.png" 

[base64 image data] 

--myboundary 
Content-type:application/pdf; 
    name="my_pdf.pdf" 
Content-length:1150 
Content-Transfer-Encoding: base64 
Content-ID: <my_pdf.pdf> 
Content-Disposition: attachment; 
filename="my_pdf.pdf" 

[base64 pdf data] 

--myboundary-- 

郵件看起來前景不錯。但我注意到,Thunderbird沒有顯示我的內嵌圖像,而是顯示了2個附件(我的圖像和我的PDF)。所以我做了一些調試,發現內聯圖片應該通過Content-Type: multipart/related發送。

因此,我將Content-Type: multipart/mixed更改爲Content-Type: multipart/related,並且Thunderbird顯示它正確:圖像以html和一個附件顯示,PDF顯示。

我不舒服,如果這是正確的解決方案,雖然它似乎工作。總是使用multipart/related是否正確(如果我有內聯和非內聯附件,萬一我只有內聯附件並且如果我只有非內聯附件)?

或者是正確的方式來使用一種類型的邊界相關的拆分內聯附件和另一種類型的邊界混合拆分非內聯附件?

我希望你能提供給我一個樣品

  1. 電子郵件與內嵌只有附件
  2. 電子郵件與非內聯僅附件
  3. 電子郵件與內聯和非內聯附件
+0

我寫了一個答案有一個類似的問題,並創建了一個ascii藝術來解釋答案:http://stackoverflow.com/a/40420648/633961 – guettli

回答

15

圖片

是的,它是使用multipart/related內容類型的正確方法。下面是一個例子(請注意 '內容類型' 和 '內容處置' 值):

enter image description here

Example source and detailed info

這裏有你所要求的樣品:

  1. 電子郵件僅帶內聯附件
  2. 帶非內聯附件的電子郵件
  3. 帶內聯和非內聯的電子郵件INE附件

樣品1:直列僅

enter image description here

Subject: Test 01: inline only 
To: Renat Gilmanov 
Content-Type: multipart/related; boundary=089e0149bb0ea4e55c051712afb5 

--089e0149bb0ea4e55c051712afb5 
Content-Type: text/html; charset=UTF-8 
Content-Transfer-Encoding: quoted-printable 

<div dir=3D"ltr">Lorem ipsum dolor sit amet, consectetur adipiscing elit. P= 
ellentesque odio urna, bibendum eu ultricies in, dignissim in magna. Vivamu= 
s risus justo, viverra sed dapibus eu, laoreet eget erat. Sed pretium a urn= 
a id pulvinar.<br><br><img src=3D"cid:ii_ia6yo3z92_14d962f8450cc6f1" height= 
=3D"218" width=3D"320"><br>=E2=80=8B<br>Cras eu velit ac purus feugiat impe= 
rdiet nec sit amet ipsum. Praesent gravida lobortis justo, nec tristique ve= 
lit sagittis finibus. Suspendisse porta ante id diam varius, in cursus ante= 
luctus. Aenean a mollis mi. Pellentesque accumsan lacus sed erat vulputate= 
, et semper tellus condimentum.<br><br>Best regards<br></div> 

--089e0149bb0ea4e55c051712afb5 
Content-Type: image/png; name="test-01.png" 
Content-Disposition: inline; filename="test-01.png" 
Content-Transfer-Encoding: base64 
Content-ID: <ii_ia6yo3z92_14d962f8450cc6f1> 
X-Attachment-Id: ii_ia6yo3z92_14d962f8450cc6f1 

iVBORw0KGgoAAAANSUhEUgAAAUAAAADaCAYAAADXGps7AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz 
AAALewAAC3sBSRnwgAAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAALnSURB 
... 
QCDLAIEsAwSyDBDIMkAgywCBLAMEsgwQyDJAIMsAgSwDBLIMEMgyQCDLAIEsAwSyDBDIMkAg6wK+ 
4gU280YtuwAAAABJRU5ErkJggg== 
--089e0149bb0ea4e55c051712afb5-- 

樣品2:只附件

enter image description here

Subject: Test 02: only attachments 
To: Renat Gilmanov 
Content-Type: multipart/mixed; boundary=047d7b41cc5c82ae5d051712c40c 

--047d7b41cc5c82ae5d051712c40c 
Content-Type: text/plain; charset=UTF-8 

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque 
odio urna, bibendum eu ultricies in, dignissim in magna. Vivamus risus 
justo, viverra sed dapibus eu, laoreet eget erat. Sed pretium a urna 
id pulvinar. 

Cras eu velit ac purus feugiat imperdiet nec sit amet ipsum. Praesent 
gravida lobortis justo, nec tristique velit sagittis finibus. 
Suspendisse porta ante id diam varius, in cursus ante luctus. Aenean a 
mollis mi. Pellentesque accumsan lacus sed erat vulputate, et semper 
tellus condimentum. 

Best regards 

--047d7b41cc5c82ae5d051712c40c 
Content-Type: image/png; name="test-02.png" 
Content-Disposition: attachment; filename="test-02.png" 
Content-Transfer-Encoding: base64 
X-Attachment-Id: f_ia6yvl4b0 

iVBORw0KGgoAAAANSUhEUgAAAUAAAADaCAYAAADXGps7AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz 
AAALewAAC3sBSRnwgAAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAALnSURB 
... 
gECWAQJZBghkGSCQZYBAlgECWQYIZBkgkGWAQJYBAlkGCGQZIJBlgECWAQJZBghkGSCQZYBA1gWV 
ywTWDU1tpwAAAABJRU5ErkJggg== 
--047d7b41cc5c82ae5d051712c40c-- 

示例3:內聯和附件

enter image description here

Subject: Test 03: inline and attachments 
To: Renat Gilmanov 
Content-Type: multipart/mixed; boundary=001a11c24d809f1525051712cc78 

--001a11c24d809f1525051712cc78 
Content-Type: multipart/related; boundary=001a11c24d809f1523051712cc77 

--001a11c24d809f1523051712cc77 
Content-Type: text/html; charset=UTF-8 
Content-Transfer-Encoding: quoted-printable 

<div dir=3D"ltr">Lorem ipsum dolor sit amet, consectetur adipiscing elit. P= 
ellentesque odio urna, bibendum eu ultricies in, dignissim in magna. Vivamu= 
s risus justo, viverra sed dapibus eu, laoreet eget erat. Sed pretium a urn= 
a id pulvinar.<br><br><img src=3D"cid:ii_ia6yyemg0_14d9636d8ac7a587" height= 
=3D"218" width=3D"320"><br>=E2=80=8B<br>Cras eu velit ac purus feugiat impe= 
rdiet nec sit amet ipsum. Praesent gravida lobortis justo, nec tristique ve= 
lit sagittis finibus. Suspendisse porta ante id diam varius, in cursus ante= 
luctus. Aenean a mollis mi. Pellentesque accumsan lacus sed erat vulputate= 
, et semper tellus condimentum.<br><br>Best regards</div> 

--001a11c24d809f1523051712cc77 
Content-Type: image/png; name="test-01.png" 
Content-Disposition: inline; filename="test-01.png" 
Content-Transfer-Encoding: base64 
Content-ID: <ii_ia6yyemg0_14d9636d8ac7a587> 
X-Attachment-Id: ii_ia6yyemg0_14d9636d8ac7a587 

iVBORw0KGgoAAAANSUhEUgAAAUAAAADaCAYAAADXGps7AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz 
AAALewAAC3sBSRnwgAAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAALnSURB 
... 
QCDLAIEsAwSyDBDIMkAgywCBLAMEsgwQyDJAIMsAgSwDBLIMEMgyQCDLAIEsAwSyDBDIMkAg6wK+ 
4gU280YtuwAAAABJRU5ErkJggg== 
--001a11c24d809f1523051712cc77-- 
--001a11c24d809f1525051712cc78 
Content-Type: image/png; name="test-02.png" 
Content-Disposition: attachment; filename="test-02.png" 
Content-Transfer-Encoding: base64 
X-Attachment-Id: f_ia6yymei1 

iVBORw0KGgoAAAANSUhEUgAAAUAAAADaCAYAAADXGps7AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz 
AAALewAAC3sBSRnwgAAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAALnSURB 
... 
gECWAQJZBghkGSCQZYBAlgECWQYIZBkgkGWAQJYBAlkGCGQZIJBlgECWAQJZBghkGSCQZYBA1gWV 
ywTWDU1tpwAAAABJRU5ErkJggg== 
--001a11c24d809f1525051712cc78-- 

快速彙總

  1. 內嵌只有附件:使用multipart/related
  2. 非在線僅附件:使用multipart/mixed
  3. 在線和非在線的附件使用multipart/mixedmultipart/related

更新

這是一個很有趣的文章:Using Images in HTML Email

+0

首先感謝您的回覆。如果我錯了,請糾正我的錯誤,我從你的回答中瞭解到,如果同時使用:inline和non-inline附件,我必須同時使用'multipart/mixed'和'multipart/related'邊界。混合版本包括以下幾部分:1.包含相關附件的HTML-Mail和2.第一個非內聯附件3.第二個非內聯附件......第1部分本身被相關邊界分割。 – steven

+0

也許你應該改善你的答案。你可以通過content-type application/pdf刪除你解釋應該發送pdf的部分,因爲它不是問題的一部分。問題只是關於邊界內容類型。並且最好給出最後一個樣例這樣的三個示例:1.僅內聯圖像附件,2.僅限非內聯附件,以及3.內聯和非內聯附件(這是您的示例顯示的內容,現在),但你可以刪除'multipart/alternative'部分,因爲它不是問題的一部分。 – steven

+0

我會爲你的答案提供+100,如果你像我解釋的那樣改進它的一點點。我希望你確認我打算爲所有三種變體提供一個樣本。所以我希望通過閱讀RFC可以幫助理解不容易獲得的邊界條件。非常感謝你。 – steven