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
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 2019-03-15 15:10 by Fotoblysk. This issue is now closed.

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

from email.mime.text import MIMEText
msg = MIMEText('<div/>', 'html')
msg["To"] = ""
msg["To"] = ""

msg337988 - (view) Author: Rémi Lapeyre (remi.lapeyre) * Date: 2019-03-15 13:30
I think this is the expected behavior, see 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', "")
>>> msg.add_header('To', "")
>>> print(msg.get_all('To'))
['', '']
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.
Date User Action Args
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