python / python/cpython

multipart/related mail generated with EmailMessage() class lacks mandatory MIME-Version header

未关闭
#120,946 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

topic-email type-bug
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

Bug report

Bug description:

When creating an email with EmailMessage() that contains an HTML part with images using a multipart/related structure as the outter MIME structure, the resulting mail contains non MIME-Version header. While the only valid value for the MIME-Version header is "1.0", this is a required header field for MIME structures, see https://datatracker.ietf.org/doc/html/rfc2045#section-4

Here is a simple example code:

#!/usr/bin/python3

import email.message

svgimg='<svg xmlns="http://www.w3.org/2000/svg"><circle cx="100" cy="100" r="50" fill="green"/></svg>'

outer = email.message.EmailMessage()

outer.make_related()

inner = email.message.EmailMessage()
inner.set_content("Hello")
inner.add_alternative("<img src='cid:imgid'>", subtype="html")

outer.attach(inner)
outer.add_related(svgimg.encode(), "image", "svg", cid="imgid")

print(outer.as_string())

The output starts like this:

From: test@example.com
To: test@example.org
Subject: Hello
Content-Type: multipart/related;
 boundary="===============4597207535328563093=="

[...]

It needs a "MIME-Version: 1.0" header in the main part. This will also cause some spam rating in e.g. spamassassin.

(Note: The example code uses multipart/related as the outer structure and multipart/alternative as the inner structure. While counterintuitive, this is how it is usually done by almost all mail clients. The example given in the python docs here https://docs.python.org/3/library/email.examples.html does it the other way round.)

CPython versions tested on:

3.12

Operating systems tested on:

Linux

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

使用报告中的示例,重现通过 email.message.EmailMessage()、make_related()、attach()、add_related() 和 as_string() 产生的问题。跟踪外层 multipart/related 消息的序列化过程,然后验证生成的输出包含 MIME-Version: 1.0,并保留现有的 MIME 结构。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
backend
Issue 类型
缺陷
难度
2/5
预计耗时
1-3 小时
活跃度
停滞
描述清晰度
描述清楚
新手友好度
45/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。