【问题标题】:Python: Modify Values in eml file (email header)Python:修改 eml 文件中的值(电子邮件标题)
【发布时间】:2016-09-29 07:55:22
【问题描述】:

我想使用 Python 更改“收件人”和“发件人”字段的电子邮件地址。到目前为止,我做了以下工作以阅读必填字段。请任何人提出建议,如何对它们进行更改。

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'])

【问题讨论】:

    标签: python parsing eml


    【解决方案1】:

    您应该将更改后的消息写回文件:

    with open('2.eml', 'w') as outfile:
        outfile.write(headers.as_string())
    

    请注意,您的姓名headers 并不完全准确,因为email.parser.Parser.parse 返回的值是email.message.Message。

    【讨论】:

      猜你喜欢
      • 2012-07-05
      • 2022-10-06
      • 2014-10-07
      • 2012-02-05
      • 2015-12-28
      • 1970-01-01
      • 1970-01-01
      • 2017-11-01
      • 2019-04-28
      相关资源
      最近更新 更多