python / python/cpython

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

Open
#120,946 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic-email type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with email.message.EmailMessage(), make_related(), attach(), add_related(), and as_string() using the example in the report. Trace how the outer multipart/related message is serialized, then verify that the generated output includes MIME-Version: 1.0 and preserves the existing MIME structure.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.