python / cpython

The Python programming language
https://www.python.org
Other
62.46k stars 29.98k forks source link

EmailMessage may lack Mime-Version #91297

Open 7847cec3-4592-4e73-bbaf-00603df0fc62 opened 2 years ago

7847cec3-4592-4e73-bbaf-00603df0fc62 commented 2 years ago
BPO 47141
Nosy @warsaw, @bitdancer

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields: ```python assignee = None closed_at = None created_at = labels = ['type-bug', 'expert-email', '3.10', '3.9'] title = 'EmailMessage may lack Mime-Version' updated_at = user = 'https://bugs.python.org/VlastimilZma' ``` bugs.python.org fields: ```python activity = actor = 'Vlastimil.Z\xc3\xadma' assignee = 'none' closed = False closed_date = None closer = None components = ['email'] creation = creator = 'Vlastimil.Z\xc3\xadma' dependencies = [] files = [] hgrepos = [] issue_num = 47141 keywords = [] message_count = 1.0 messages = ['416163'] nosy_count = 3.0 nosy_names = ['barry', 'r.david.murray', 'Vlastimil.Z\xc3\xadma'] pr_nums = [] priority = 'normal' resolution = None stage = None status = 'open' superseder = None type = 'behavior' url = 'https://bugs.python.org/issue47141' versions = ['Python 3.9', 'Python 3.10'] ```

7847cec3-4592-4e73-bbaf-00603df0fc62 commented 2 years ago

When an EmailMessage is created without setting its content, it may lack the MIME-Version header. I encountered this behavior when creating a S/MIME signed message, but I believe it applies to any EmailMessage in general.

Example:

from email.message import EmailMessage

nested_message = EmailMessage()
nested_message.set_content("Gazpacho!")

env = EmailMessage()
env.add_header("Content-Type", "multipart/signed", protocol="application/pkcs7-signature", micalg='sha-256')
env.preamble = "This is an S/MIME signed message"
env.attach(nested_message)
print(str(env))

This results in a message with no MIME-Version header

Content-Type: multipart/signed; protocol="application/pkcs7-signature"; micalg="sha-256"; boundary="===============4414940364499332856=="

This is an S/MIME signed message --===============4414940364499332856== Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit MIME-Version: 1.0

Gazpacho!

--===============4414940364499332856==--

which violates section 4 of RFC 2045, see https://datatracker.ietf.org/doc/html/rfc2045#section-4

Messages composed in accordance with this document MUST include such a header field, with the following verbatim text:

 MIME-Version: 1.0

and the section doesn't seem to be obsoleted by newer MIME related RFCs.

It's not clear why the EmailMessage shouldn't have the MIME-Version header defined in all cases, since it should represent the email message. I suggest to set the MIME-version header on __init__ the same way MIMEBase does.