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:
Note that your name
headers
is not completely accurate as the value returned byemail.parser.Parser.parse
isemail.message.Message
.