[issue33633] smtplib msg['To] = appends instead of assigning

R. David Murray report at bugs.python.org
Thu May 24 13:07:26 EDT 2018


R. David Murray <rdmurray at bitdance.com> added the comment:

smtplib doesn't define any behavior for messages.  I presume you are talking about the email library?

Vis the print behavior, dict-style lookup is defined to return the first matching header.  If you want to see all of them, you can use get_all.  For debugging, you should print the whole message, which would show the duplicate headers:

>>> from email.message import Message
>>> msg = Message()
>>> msg['To'] = 'abc at xyz.com'
>>> msg['To'] = 'xyz at abc.com'
>>> print(msg)
To: abc at xyz.com
To: xyz at abc.com



With the new API this is better, at least in terms of debugging:

>>> from email.message import EmailMessage
>>> msg = EmailMessage()
>>> msg['To'] = 'abc at xyz.com'
>>> print(msg)
To: abc at xyz.com


>>> msg['To'] = 'xyz at abc.com'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/rdmurray/python/p38/Lib/email/message.py", line 408, in __setitem__
    "in a message".format(max_count, name))
ValueError: There may be at most 1 To headers in a message


This can't be changed in the old API for backward compatibility reasons.

----------
nosy: +r.david.murray
resolution:  -> out of date
stage:  -> resolved
status: open -> closed

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue33633>
_______________________________________


More information about the Python-bugs-list mailing list