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: email's use of __setitem__ is highly counterintuitive
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.3
Status: closed Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: r.david.murray, tonimueller
Priority: normal Keywords:

Created on 2011-05-19 11:37 by tonimueller, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (2)
msg136276 - (view) Author: Toni Mueller (tonimueller) Date: 2011-05-19 11:37
email's usage of __setitem__ is highly counterintuitive to the point of being dangerous. The documented behaviour is (quote):

__setitem__(name, val)

    Add a header to the message with field name name and value val. The field is appended to the end of the message’s existing fields.

    Note that this does not overwrite or delete any existing header with the same name. If you want to ensure that the new header is the only one present in the message with field name name, delete the field first, e.g.:

(taken from )

The use case of *appending* a header of the same type (eg. a "Received:" header) should be performed by the add_header() method, or an extend_header() method, or something similar, and not by abusing the __setitem__ method. The current behaviour imho deviates extremely from the behaviour of similar libraries in all other programming languages that I'm aware of, and from the standard dict functionality, too. It makes it much too easy to have duplicate headers, esp., duplicate "To:" headers, resulting in mailbombing and information leakage. For the potential damage, this property of the library is highly under-advertised.

A side effect appears to be that trying to have your message headers set up in a unique fashion, probably the most frequent use case, one has to make sure to use each operator only once, or "decorate" everything with a del msg[myheader], as the operation is not idempotent.
msg136279 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2011-05-19 11:49
This is a long-standing design choice in the email package.  If you want to advocate for changing it, please join the email-sig mailing list (see  We are in the process of developing a new version, which will at least reject things like duplicate To headers.

I'm closing this issue since as things stand this is not something that is likely to change.  If you carry the day in a discussion on the email-sig, we can reopen the issue.  In any case it is a feature request, not a bug.
Date User Action Args
2022-04-11 14:57:17adminsetgithub: 56320
2011-05-19 11:49:06r.david.murraysetstatus: open -> closed

type: behavior -> enhancement
components: + Library (Lib)
versions: + Python 3.3, - Python 2.6
nosy: + r.david.murray

messages: + msg136279
2011-05-19 11:37:22tonimuellercreate