首页 文章

MIME类型,以满足HTML,电子邮件,图像和纯文本?

提问于
浏览
21

Mail multipart/alternative vs multipart/mixed的答案表明附件应该是 multipart/alternative 消息的对等,如:

  • multipart / mixed

  • multipart / alternative

  • text / plain

  • text / html

  • some / thing(处置:附件)

  • some / thing(处置:附件)

  • ......

我想发送带有一些内嵌图像和纯文本替代品的html部分的电子邮件 . 各个部分的首选MIME布局是什么?在示例代码和其他问题中出现了几个选项,但哪些选项在实践中效果最好?我倾向于这样:

  • multipart / alternative

  • text / plain

  • multipart / related

  • text / html(通过cid引用图像)

  • image / gif

  • image / gif

  • ......

这样,图像显然是为了渲染html部分 . 一个完整的例子是:

From: Rich Example <rich-example@example.org>
To: A Recipient <recipient@example.org>
Subject: An example of email with images and a plain alternative
MIME-Version: 1.0
Content-Type: multipart/alternative; boundary="outer-boundary"

This is a MIME-encoded message. If you are seeing this, your mail
reader is old.
--outer-boundary
Content-Type: text/plain; charset=us-ascii

This message might make you :) or it might make you :(

--outer-boundary
MIME-Version: 1.0
Content-Type: multipart/related;
  type="text/html"; start="<body@here>"; boundary="inner-boundary"

--inner-boundary
Content-Type: text/html; charset=us-ascii
Content-Disposition: inline
Content-ID: <body@here>

<html>
 <body>
  This message might make you
  <img src="cid:smile@here" alt="smile">
  or it might make you
  <img src="cid:frown@here" alt="frown">
 </body>
</html>

--inner-boundary
Content-Type: image/gif
Content-Disposition: inline
Content-Transfer-Encoding: base64
Content-ID: <smile@here>

R0lGODlhEAAQAKEBAAAAAP//AP//AP//ACH5BAEKAAIALAAAAAAQABAAAAIzlA2px6IBw2
IpWglOvTahDgGdI0ZlGW5meKlci6JrasrqkypxJr8S0oNpgqkGLtcY6hoFADs=

--inner-boundary
Content-Type: image/gif
Content-Disposition: inline
Content-Transfer-Encoding: base64
Content-ID: <frown@here>

R0lGODlhEAAQAKEBAAAAAAD//wD//wD//yH5BAEKAAIALAAAAAAQABAAAAIzlA2px6IBw2
IpWglOvTahDgGdI0ZlGW5meKlci75drDzm5uLZyZ1I3Mv8ZB5Krtgg1RoFADs=

--inner-boundary--

--outer-boundary--

1 回答

  • 8

    你是对的 . 内联图像应存储在 multipart/related mime-entity(RFC 2387)中,并且可以使用 multipart/alternative (RFC 2046)提供多种内容类型选项 .
    要添加附件,您可以将整个结构放入 multipart/mixed 并添加附件 .

    • multipart / mixed

    • multipart / alternative

    • text / plain

    • multipart / related

    • text / html

    • image / gif

    • image / gif

    • some / thing(处置:附件)

    • some / thing(处置:附件)

    您还可以在text / plain消息中使用内联图像,但并非所有MUA都支持此功能 . (使用none或disposition:inline)

    • multipart / mixed

    • text / plain(上图中的文字)

    • image / gif

    • text / plain(图片下方文字)

    而且我不知道将它与多部分/替代HTML电子邮件结合起来的简洁方法 .

相关问题