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.

classification
Title: Trying to change values (fe. "To", "From") of email.mime.text.MIMEText after initial assigment silently doesn't change them.
Type: behavior Stage: resolved
Components: email Versions: Python 3.8, Python 3.7, Python 3.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Fotoblysk, barry, r.david.murray, remi.lapeyre
Priority: normal Keywords:

Created on 2019-03-15 13:15 by Fotoblysk, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (4)
msg337986 - (view) Author: Łukasz (Fotoblysk) Date: 2019-03-15 13:18
Script reproducing this behavior:

```python3
from email.mime.text import MIMEText
msg = MIMEText('<div/>', 'html')
msg["To"] = "this.email.shouldnt.be.printed@nokia.com"
msg["To"] = "valid.email@nokia.com"
print(msg["To"])
```

stdout:
```
this.email.shouldnt.be.printed@nokia.com
```
msg337988 - (view) Author: Rémi Lapeyre (remi.lapeyre) * Date: 2019-03-15 13:30
I think this is the expected behavior, see https://docs.python.org/3.4/library/email.message.html#email.message.Message.__getitem__ for details:

> Note that if the named field appears more than once in the message’s headers, exactly which of those field values will be returned is undefined. Use the get_all() method to get the values of all the extant named headers.

This has been already discussed elsewhere, it may not be the best API but for compatibility reasons, we cannot change it anyway.
msg337989 - (view) Author: Rémi Lapeyre (remi.lapeyre) * Date: 2019-03-15 13:34
Here's how you can rewrite your code so it is more explicit:

Python 3.7.2 (default, Feb 12 2019, 08:15:36)
[Clang 10.0.0 (clang-1000.11.45.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from email.mime.text import MIMEText
>>> msg = MIMEText('<div/>', 'html')
>>> msg.add_header('To', "this.email.shouldnt.be.printed@nokia.com")
>>> msg.add_header('To', "valid.email@nokia.com")
>>> print(msg.get_all('To'))
['this.email.shouldnt.be.printed@nokia.com', 'valid.email@nokia.com']
msg337997 - (view) Author: Łukasz (Fotoblysk) Date: 2019-03-15 15:10
Ok, so this is not a bug, I must missed this information in documentation.

In my defense this usage of `__getitem__` and `__setitem__` feels a little bit counter-intuitive.

Sorry bothering.
History
Date User Action Args
2022-04-11 14:59:12adminsetgithub: 80484
2019-03-15 15:10:43Fotoblysksetstatus: open -> closed
resolution: not a bug
messages: + msg337997

stage: resolved
2019-03-15 13:34:53remi.lapeyresetmessages: + msg337989
versions: + Python 3.8
2019-03-15 13:30:38remi.lapeyresetnosy: + remi.lapeyre
messages: + msg337988
2019-03-15 13:18:20Fotoblysksetmessages: + msg337986
2019-03-15 13:15:00Fotoblyskcreate