码迷,mamicode.com
首页 > 编程语言 > 详细

修改python原文件中的from、to字段

时间:2017-07-12 23:39:15      阅读:474      评论:0      收藏:0      [点我收藏+]

标签:python email from to

1down votefavorite

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 MIMEMultipart
msgRoot = MIMEMultipart(‘related‘)
msgRoot[‘To‘] = ‘email@email.com‘



You can use the 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.

For example,

if msgRoot.has_key(‘to‘):
    msgRoot.replace_header(‘to‘, someAdress)else:
    msgRoot[‘to‘] = ‘email@email.com‘


Thank you, that worked perfectly! – Dan Apr 24 ‘11 at 14:26




in Python 3.5 I had to use if ‘to‘ in message: because has_key has been deprecated. – Tom SitterJun 8 ‘16 at 21:17 



Python 3 changed dict syntax. See docs.python.org/3/whatsnew/3.0.html#builtins "Removed. dict.has_key() – use the in operator instead." – gimel Jun 9 ‘16 at 5:31


原文:

https://stackoverflow.com/questions/5770951/python-how-can-i-change-the-to-field-in-smtp-mime-script-rather-than-adding-a

修改python原文件中的from、to字段

标签:python email from to

原文地址:http://tenderrain.blog.51cto.com/9202912/1946852

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!