Get body text of an email using python imap and email package

后端 未结 3 1176
慢半拍i
慢半拍i 2020-12-03 19:42

I want to retrieve body (only text) of emails using python imap and email package.

As per this SO thread, I\'m using the following code:

mail = email         


        
3条回答
  •  情深已故
    2020-12-03 20:04

    You are assuming that messages have a uniform structure, with one well-defined "main part". That is not the case; there can be messages with a single part which is not a text part (just an "attachment" of a binary file, and nothing else) or it can be a multipart with multiple textual parts (or, again, none at all) and even if there is only one, it need not be the first part. Furthermore, there are nested multiparts (one or more parts is another MIME message, recursively).

    In so many words, you must inspect the MIME structure, then decide which part(s) are relevant for your application. If you only receive messages from a fairly static, small set of clients, you may be able to cut some corners (at least until the next upgrade of Microsoft Plague hits) but in general, there simply isn't a hierarchy of any kind, just a collection of (not necessarily always directly related) equally important parts.

提交回复
热议问题