如果我想包含电子邮件的文本版本和附加文件,应使用哪个 MIME

Which MIME to use if I want to include text version of email and attach files

据我了解,电子邮件只能包含一个主要 Content-Type header,是否正确?无论您是要发送带有附件的消息,还是带有 html 内容和纯文本版本的消息,您都应该使用 Multipart Content-Type。在每个部分中,还可以为该特定部分明确指定 content-type。

rfc1341 我了解到 multipart 有两种主要的子类型 content-type:

如果我想同时包含 html 和纯文本版本,我会使用 Multipart/alternative 作为最外层的 Content-Type header 因为它的语义是(来自 rfc1341):

In particular, each of the parts is an "alternative" version of the same information. User agents should recognize that the content of the various parts are interchangeable. The user agent should either choose the "best" type based on the user's environment and preferences, or offer the user the available alternatives. In general, choosing the best type means displaying only the LAST part that can be displayed. This may be used, for example, to send mail in a fancy text format in such a way that it can easily be displayed anywhere

另一方面,如果我需要附加文件,我应该使用 The Multipart/mixedrfc1341 所述:

Mixed is intended for use when the body parts are independent and intended to be displayed serially

当您想要附加文件时显然就是这种情况。

但是我如何发送带有文件 AND 的文本版本的电子邮件,即当我希望我的多部分信件的某些部分连续显示(主要内容和附件),而其他部分交替显示(html 和纯文本)?那我应该用什么最外层的 Content-Type header?

您将 multipart/alternative 封装在 multipart/mixed 中。

换句话说,如果您想要一个文本和 HTML 正文并附加一个 zip 文件,MIME 树结构将如下所示:

multipart/mixed
  multipart/alternative
    text/plain
    text/html
  application/zip