This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: smtplib: allow custom policy or use msg.policy in send_message
Type: Stage:
Components: email, Library (Lib) Versions: Python 3.11, Python 3.10, Python 3.9, Python 3.8, Python 3.7
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Miksus, barry, r.david.murray
Priority: normal Keywords:

Created on 2022-03-17 16:48 by Miksus, last changed 2022-04-11 14:59 by admin.

Messages (2)
msg415428 - (view) Author: Mikael Koli (Miksus) Date: 2022-03-17 16:48
The method smtplib.SMTP.send_message does not use the message's Policy if all of the from_addrs or to_addrs are not international. See: (unchanged in current main). The email.generator.BytesGenerator does not capture the email's policy as it was not passed to its init.

This has at least one notable setback: you cannot set the mangle_from to False meaning that the method will always turn "From ..." to ">From ..." in the plain text part (though often that is desirable). This is especially confusing as email library has the mangle_from as False by default for EmailMessages but smtplib.SMTP's send_message does not respect this by default.

The smtplib.SMTP.send_message has a mention about this in the docstring thus not entirely sure if intentional:

    ... Otherwise the generator is called without modifying the

If we changed this line:

from this:
    g = email.generator.BytesGenerator(bytesmsg)

to this:
    g = email.generator.BytesGenerator(bytesmsg, policy=msg.policy.clone()

smptlib's tests are passed but I suspect it's not that simple. The docstring mention indicates this is at some level intentional and I think the mangle_from needs to remain True as otherwise, it may cause security problems in existing code. Another option perhaps could be that the policy could be passed with the send_message and that is used if not None or we could have argument "msg_policy=False" that if True, the message's policy is used.

One could also think that this could be overcome by subclassing the SMTP. However, the logic is such deep in that it is not convenient.

So in short, the options I thought of:
- Have an argument "policy" in send_message to force usage of your own policy (safe option)
- Have an argument "msg_policy" (name debatable) in send_message and if True, the message's policy is always used (safe option)
- Use the message's policy always (unsafe, possibly breaking and causing security issues in existing code)
msg415436 - (view) Author: Mikael Koli (Miksus) Date: 2022-03-17 18:34
It seems the message's policy is actually used. However, the mangle_from_ is still always True as the policy is not passed in the initiation of the generator. It seems though that all the options I mentioned could still make the mangle_from_ to be changeable if one wishes so.
Date User Action Args
2022-04-11 14:59:57adminsetgithub: 91203
2022-03-17 18:34:59Miksussetmessages: + msg415436
2022-03-17 16:48:39Miksuscreate