这是我正在使用的代码的摘录。我正在遍历添加电子邮件的部分;我的问题不是在每个循环中更改“to”字段,而是附加“to”数据。显然这会导致一些问题,因为 to 字段最终变得越来越长。我试过 msgRoot.del_param('To') 无济于事。我什至尝试将 msgRoot['To'] 设置为引用列表的第一个索引,这样我就可以简单地更改该列表项的值(也没有用)。
from email.MIMEMultipart import MIMEMultipart
msgRoot = MIMEMultipart('related')
msgRoot['To'] = 'email@email.com'
最佳答案
您可以使用 replace_header
method .
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.
例如,
if msgRoot.has_key('to'):
msgRoot.replace_header('to', someAdress)
else:
msgRoot['to'] = 'email@email.com'
关于Python:如何更改 smtp/MIME 脚本中的 "to"字段而不是添加新字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5770951/