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
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.)
That's interesting. EmailMessage seems to only set MIME-Version in the set_content() method, so your outer.make_related() and outer.add_related() didn't really do anything more than what MIMEPart already defines. (FYI MIMEPart is the base class of EmailMessage that does not implicitly set MIME-Version.)
Maybe we can do the same thing in the make_*() and add_*() methods too? But I am not a Python contributor, so I don't have a say... In the meantime though, adding outer['MIME-Version'] = '1.0' somewhere should do the trick. :)
Activity
rapidcow commented on Jul 14, 2024
That's interesting. EmailMessage seems to only set MIME-Version in the set_content() method, so your outer.make_related() and outer.add_related() didn't really do anything more than what MIMEPart already defines. (FYI MIMEPart is the base class of EmailMessage that does not implicitly set MIME-Version.)
Maybe we can do the same thing in the make_*() and add_*() methods too? But I am not a Python contributor, so I don't have a say... In the meantime though, adding outer['MIME-Version'] = '1.0' somewhere should do the trick. :)