I'm playing with Python imaplib (Python 2.6) to fetch emails from GMail. Everything I fetch an email with method http://docs.python.org/library/imaplib.html#imaplib.IMAP4.fetch I get whole email. I need only text part and also parse names of attachments, without downloading them. How this can be done? I see that emails returned by GMail follow the same format that browsers send to HTTP servers.
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
I'm afraid you're out of luck. According to this post, there are only two parts to the email - the header and the body. The body is where the attachments are if there are any and you have to download the whole body before extracting only the message text. The info about the FETCH command found here also supports this opinion. While it says you can extract partials of the body, these are specified in terms of octets which doesn't really help.
Take a look at this recipe: http://code.activestate.com/recipes/498189/
I adapted it slightly to print the From, Subject, Date, name of attachments, and message body (just plaintext for now -- its trivial to add html messages).
I used the Gmail pop3 server in this case, but it should work for IMAP as well.
This should be enough to get you heading in the right direction.
You can get only the plain text of the email by doing something like:
For the gmail messages I've seen, section 1 has the plaintext, including multipart junk. This may not be so robust.
I don't know how to get the name of the attachment without all of it. I haven't tried using partials.