Python imap: how to parse multi-user email content

Mail may contain various blocks, for example:

--0016e68deb06b58acf04897c624e Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable content_1 ... --0016e68deb06b58acf04897c624e Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable content_2 ... and so on 

How can I get the contents of each block using python?
And also how to get the properties of each block? (content type, etc.)

+4
source share
2 answers

To parse email, I used the Message.walk() method as follows:

 if msg.is_multipart(): for part in msg.walk(): ... 

For content, you can try: part.get_payload() . For the content type there is: part.get_content_type()

You will find documentation here: http://docs.python.org/library/email.message.html

You can also try email with its iterators.

+8
source

http://docs.python.org/library/email.html

A very simple example (msg_as_str contains raw bytes received from the imap server):

 import email msg = email.message_from_string(msg_as_str) print msg["Subject"] 
+2
source

All Articles