Issue 36303: Trying to change values (fe. "To", "From") of email.mime.text.MIMEText after initial assigment silently doesn't change them. (original) (raw)

Issue36303

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('
', '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('
', '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:12 admin set github: 80484
2019-03-15 15:10:43 Fotoblysk set status: open -> closedresolution: not a bugmessages: + stage: resolved
2019-03-15 13:34:53 remi.lapeyre set messages: + versions: + Python 3.8
2019-03-15 13:30:38 remi.lapeyre set nosy: + remi.lapeyremessages: +
2019-03-15 13🔞20 Fotoblysk set messages: +
2019-03-15 13:15:00 Fotoblysk create