Bug in email.generator.BytesGenerator() [was: Why does SMTP.send_message() do from mangling?]

Grant Edwards grant.b.edwards at gmail.com
Mon Sep 27 00:36:28 EDT 2021


On 2021-09-27, Grant Edwards <grant.b.edwards at gmail.com> wrote:

> According to
> https://docs.python.org/3/library/email.generator.html#email.generator.BytesGenerator
> the default from mangling behavior is _supposed_ to obey the message
> policy if no policy or mangle_from_ value was
> provided to the call to BytesGenerator().

Nope, I think both the author of smtplib.py and I were misled by the
documentation for email.generator.BytesGenerator. After rereading it,
it does say in one place that if no mangle_from_, value is passed to
BytesGenerator(), it is supposed to default to the mangle_from_
setting in the policy **passed to BytesGenerator()**:

    If optional mangle_from_ is True, put a > character in front of
    any line in the body that starts with the exact string "From ",
    that is From followed by a space at the beginning of a
    line. mangle_from_ defaults to the value of the mangle_from_
    setting of the policy.

Where "the policy" refers to the one passed to BytesGenerator().

However, later on it also says

    If policy is None (the default), use the policy associated with
    the Message or EmailMessage object passed to flatten to control
    the message generation.

That's misleading and only partially true. If you don't pass a policy
to BytesGenerator(), only _some_ of the settings from the message's
policy will be used. Some policy settings (e.g. mangle_from_) are
obeyed when passed to BytesGenerator(), but ignored in the message's
policy even if there was no policy passed to BytesGenerator().  I
think that last sentence above needs to be changed, and smtplib.py
needs to be fixed as shown in my previous post.

--
Grant


More information about the Python-list mailing list