带有纯文本部分、html 部分和附件的电子邮件

Posted

技术标签:

【中文标题】带有纯文本部分、html 部分和附件的电子邮件【英文标题】:E-Mail with plain text part, html part and attachments 【发布时间】:2012-04-24 18:24:06 【问题描述】:

我正在尝试编写一个小 Perl 脚本来发送带有附件的纯文本/html 电子邮件:

      use Encode();
      use MIME::Entity();
      my $msg = MIME::Entity->build(
        From        => '"' . Encode::encode( 'MIME-Header', 'Any User' ) . '" <any.user@example.test>',
        To      => '"' . Encode::encode( 'MIME-Header', 'Some User' ) . '" <some.user@example.test>',
        Subject     => Encode::encode( 'MIME-Header', 'Multipart alternative with attachments' ),
        Type        => 'multipart/alternative',
      );
      my $plain = $msg->attach(
        Type            => 'text/plain; charset=UTF-8',
        Data            => [ "*Multipart alternative with attachments*\n\nHere comes the plain text." ],
      );
      my $fancy = $msg->attach( Type => 'multipart/related' );
      $fancy->attach(
        Type            => 'text/html; charset=UTF-8',
        Data            => [ "<html>\n\t<body>\n\t\t<h1>Multipart alternative with attachments</h1>\n\t\t<p>Here comes the fancy text.</p>\n\t</body>\n</html>" ],
      );
      $fancy->attach(
        Type            => 'text/plain; charset=UTF-8',
        Disposition     => 'attachment',
        Filename        => 'test.txt',
        Data            => [ "*Multipart alternative with attachments*\n\nHere comes the attachment." ],
      );

我得到以下 MIME 源代码:

      Content-Type: multipart/alternative; boundary="----------=_1334231022-8218-0"
      Content-Transfer-Encoding: binary
      MIME-Version: 1.0
      X-Mailer: MIME-tools 5.502 (Entity 5.502)
      From: "Any User" <any.user@example.test>
      To: "Some User" <some.user@example.test>
      Subject: Multipart alternative with attachments

      This is a multi-part message in MIME format...

      ------------=_1334231022-8218-0
      Content-Type: text/plain; charset=UTF-8
      Content-Disposition: inline
      Content-Transfer-Encoding: binary

      *Multipart alternative with attachments*

      Here comes the plain text.
      ------------=_1334231022-8218-0
      Content-Type: multipart/related; boundary="----------=_1334231022-8218-1"
      Content-Transfer-Encoding: binary

      This is a multi-part message in MIME format...

      ------------=_1334231022-8218-1
      Content-Type: text/html; charset=UTF-8
      Content-Disposition: inline
      Content-Transfer-Encoding: binary

      <html>
              <body>
                      <h1>Multipart alternative with attachments</h1>
                      <p>Here comes the fancy text.</p>
              </body>
      </html>
      ------------=_1334231022-8218-1
      Content-Type: text/plain; charset=UTF-8; name="test.txt"
      Content-Disposition: attachment; filename="test.txt"
      Content-Transfer-Encoding: binary

      *Multipart alternative with attachments*

      Here comes the attachment.
      ------------=_1334231022-8218-1--

      ------------=_1334231022-8218-0--

如果我在The Bat 中查看电子邮件,则可以正确显示纯文本和html 版本。在 Mozilla Thunderbird 11.0.1 中,不会显示普通版本,而是显示简化的 html 版本。

我的逻辑有错误还是 Thunderbird 失败了?

【问题讨论】:

【参考方案1】:

您不需要$fancy,将所有内容添加到$msg

Content-Transfer-Encoding 标头更好base64quoted-printable。更万无一失。

Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: base64

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

【讨论】:

RFC 1521 已过时。将来,使用 IETF 网站查找 RFC 文本;靠近顶部,显示了相关的 RFC。

以上是关于带有纯文本部分、html 部分和附件的电子邮件的主要内容,如果未能解决你的问题,请参考以下文章

将一些文件附加到 MIME 多部分电子邮件

使用 perl 发送带有文件附件的多部分文本/html 替代消息,

在 python 中解析多部分电子邮件并保存附件

带有文本和日历的多部分电子邮件:Outlook 无法识别 ics

如何使用 System.NET.mail 发送带有附件但没有任何纯文本正文的电子邮件?

Rails:如何在多部分/替代电子邮件中使用部分(HTML 和纯文本)