2010-11-04 64 views
4

一個郵件可能包含不同的像塊:蟒蛇IMAP:如何解析多郵件內容

--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 

我怎樣才能得到與Python每個塊的內容?
還有如何獲得每個塊的屬性? (內容類型,等等。)

回答

8

爲了解析的郵件我已經使用Message.walk()方法是這樣的:

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

對於內容你可以嘗試:part.get_payload()。對於內容類型有:part.get_content_type()

您將在這裏找到documetation:http://docs.python.org/library/email.message.html

您也可以嘗試email模塊,其迭代器。

+0

謝謝!我沒有讀過get_payload()返回消息列表! – Sergey 2010-11-04 09:54:36