The only thing strange in this code is the use of the imap_server.uid() function, which is a wrapper function for all the standard commands listed, and the use of a magic constant ‘RFC822’ which tells the IMAP server to send all the standard mail components.

The specifics of those steps are available at the urls provided and are fairly readable. Once again the response back from the fetch call is a tuple (status, email_string) which the python package email can interpret with email.message_from_string(email_string). The end result is pushed into a gmail_message object, and returned to the caller.

A quick note about gmail_message.py which encapulates the gmail_message structure:

The boiler plate __init__ / __repr__ of the gmail_message code is a bit too long to post. Needless to say, it’s all pretty readable, the only interesting part is that the gmail_message.Subject member defaults to ‘( no subject )’ instead of None. This may be a bad call, but it makes life simpler for the moment, as this means we don’t need to check if a subject exists when using a message later; every message is contracted to have a subject.

Phew…several posts later, what do we have? A half-way viable method to interact with gmail imap in python, without needing fragile libraries, without downloading every email in our gmail account, and without insulting the hard working people of the Python libraries or Google. Well, maybe not that last part.

If you download the module and collect all the files together, you will be able to make calls like:

18 Responses

By default, the snippet I pulled returns the HTML mime section of a message if one exists. If you’d rather always get the plain text version, do the following:
1. Grab a copy of my files.
2. Open file:
gmail_messages.py
3. Comment out ( add a # before ) lines 99,100,101:

That’s a bit different from where I was going with my code … you’d need some substantial changes. Unfortunately I wouldn’t have time to test these sort of changes out until the weekend…but if you ping me again on friday I’ll try to find time to give it a whirl.

What’d you need to do is:
(gmail_messages.py) line 51:
change ‘BODY.PEEK[HEADER.FIELDS (FROM SUBJECT DATE)]’ to either:
BODY.PEEK[] or
BODY.PEEK[HEADER.FIELDS]
not sure which … you’d need to look it up in the IMAP documentation. Good luck!

Then you’d need to add a new field to the object in ‘gmail_message.py’ probably would call it ‘headers’.
Then add a new line to the gmail_messages.py around line 58, saying message.headers = headers
Finally, you’d modify the gmail_message.py print routine to dump that headers dictionary when outputting a message, if you want. If you do that, be sure to remove the current headers being shown ‘From’,’Subject’,’Date’ to avoid duplication.

From then on, after you’d done the prelim work ( find a folder, load the messages, etc ) you should get a dump of the headers.

Like I said, not sure when I’d get around to it, since I’ve moved on a bit. But if you have further questions or really need someone else to try, remind me and I’ll try to make time.

I’ll change the links around these posts to point over there this weekend. This was in the failed_ideas branch, since *originally* before Google Wave was declared ka-poot, I wanted to make a wave robot like this project, Emaily . That got me into the poor python imap libraries, and it seemed like an interesting thing to kvetch about. Guess it was a bit, as this series is the only thing on the blog that gets any real traffic 😉