带有内联附件和非内联附件的HTML-Email

时间:2015-05-20 13:41:32

标签: email mime email-attachments

使用内联附件和非内联附件创建HTML电子邮件的正确方法是什么?

此外,请告诉我仅使用内联附件和仅使用非内联附件的内容类型。

直到现在我这样做了:

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. 包含内联和非内联附件的电子邮件

1 个答案:

答案 0 :(得分:24)

图片

是的,使用multipart/related内容类型是正确的方法。以下是一个示例(请注意'Content-Type'和'Content-Disposition'值):

enter image description here

Example source and detailed info

以下是您要求的样本:

  1. 仅使用内联附件的电子邮件
  2. 仅使用非内联附件的电子邮件
  3. 包含内联和非内联附件的电子邮件
  4. 示例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
    4. <强>更新

      这是一篇非常有趣的文章:Using Images in HTML Email