Save email.Message to file

I am trying to modify emails stored as text files. I first import the message as follows:

import email
f = open('filename')
msg = email.message_from_file(f)

Then I make all the changes I want using the functions of the email module.

The last step is to save the message object (msg) in a file. What part of the code does this? It seems that there is no simple function like message_to_file () "...

Many thanks.

+3
source share
1 answer

The Messsage.as_string method should give you a smoothed version of the message, which you can write just like any other line:

msg.as_string ()

, , email.generator? , - :

generator = email.generator.Generator(out_file)
generator.flatten(msg)

, out_file , msg - .

+5

Source: https://habr.com/ru/post/1754093/


All Articles