Python:修改eml文件(电子邮件标头)中的值 [英] Python: Modify Values in eml file (email header)
本文介绍了Python:修改eml文件(电子邮件标头)中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用Python更改收件人"和发件人"字段的电子邮件地址.到目前为止,我已经完成了以下工作,以读取必填字段.任何人都可以提出建议,如何在其中做出改变.
I would like to have a changes of "To" and "From" field's email addresses using Python. So far now I did following which is working for reading required field. Any one please suggest, How to make change in them.
from email.parser import Parser
fp = open('2.eml', 'r+')
headers = Parser().parse(fp)
# Make changes only within a code, Not in to the file. I would like to save given changes for from in to my 2.eml file
headers.replace_header('from', 'newEmail@domain.com')
print ('To: %s' % headers['to'])
print ('From: %s' % headers['from'])
print ('Subject: %s' % headers['subject'])
推荐答案
您应将更改后的消息写回到文件中:
You should write the changed message back to the file:
with open('2.eml', 'w') as outfile:
outfile.write(headers.as_string())
请注意,您的姓名headers
并不完全准确,因为 email.message.Message
.
Note that your name headers
is not completely accurate as the value returned by email.parser.Parser.parse
is email.message.Message
.
这篇关于Python:修改eml文件(电子邮件标头)中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文