本文共 1389 字,大约阅读时间需要 4 分钟。
1 1 | Here's an excerpt from the code I'm using. I'm looping through the part that adds the email; my problem is rather than changing the "to" field on each loop, it is appending the "to" data. Obviously this causes some issues, since the to field ends up getting longer and longer. I tried msgRoot.del_param('To') to no avail. I even tried setting the msgRoot['To'] to refer to the first index of a list so I could simply change the value of that list item (also didn't work). from email.MIMEMultipart import MIMEMultipartmsgRoot = MIMEMultipart('related')msgRoot['To'] = 'email@email.com' |
You can use the .
replace_header(_name, _value)
Replace a header. Replace the first header found in the message that matches _name, retaining header order and field name case. If no matching header was found, a KeyError is raised.
New in version 2.2.2.
For example,
if msgRoot.has_key('to'): msgRoot.replace_header('to', someAdress)else: msgRoot['to'] = 'email@email.com'
Thank you, that worked perfectly! – | |||
| in Python 3.5 I had to use | ||
| Python 3 changed dict syntax. See "Removed. dict.has_key() – use the in operator instead." – |
原文:
https://stackoverflow.com/questions/5770951/python-how-can-i-change-the-to-field-in-smtp-mime-script-rather-than-adding-a
本文转自Tenderrain 51CTO博客,原文链接:http://blog.51cto.com/tenderrain/1946852,如需转载请自行联系原作者