This is part 1 of my explanation of my gmail_imap (python) example library, please refer to parts 2, 3, 4.
As I said before mocking Google for the lack of a Gmail API, we’re forced to turn to IMAP. This being an exercise in python, I turned to the standard library docs, and got no help … worse than no help, confusing ‘help’. Next up, I think we should repent and turn to Google to help us find example code that will show us the proper way to deal with imaplib.
To summarize the results … crap. Some decent attempts, but mostly, as you can see in my references, crap. Don’t trust my opinion? Fine, I never liked you anyway … here’s a quote from the ActiveState IMAP MailWatcher recipe:
rough edges. The [example] code is organized in an awkward way. Presentation does not cleanly separate from the core code. This showed painfully when I wanted to deploy this recipe on a box where tcl/Tk was not available. Thus I wanted to replace Tkinter calls so that simply an external program gets called if there is a new mail. Ideally, this should be the matter of replacing a presentation class or function call with another one, leaving IMAP related code intact. But the Mailwatcher class just blends everything together with no concern, so I had to actually understand the IMAP code to achieve what I want, and alltogether [sic], work more on it.
Harsh, but fair. I hope that I acquit myself better. Code follows.
01/30/09 – Also found, webpymail which seems to be a similar idea. But also in a fairly beta format.
To start with, we need a manager class to abstract away everything but our logical thoughts of what a mailbox system should be:
a mailserver, a list of mailboxes, and a list of messages. Anything more than that, and well, I get *gosh darn* confused.
#imap_gmail.py
import imaplib
import gmail_mailboxes, gmail_messages, gmail_message
class gmail_imap:
def __init__ (self, username, password):
self.imap_server = imaplib.IMAP4_SSL("imap.gmail.com",993)
self.username = username
self.password = password
self.loggedIn = False
self.mailboxes = gmail_mailboxes.gmail_mailboxes(self);
self.messages = gmail_messages.gmail_messages(self);
def login (self):
self.imap_server.login(self.username,self.password)
self.loggedIn = True
def logout (self):
self.imap_server.close()
self.imap_server.logout()
self.loggedIn = False
All of the above is reasonably self explanatory, except for self.loggedIn which just holds the current state of our object so that we don’t need to do any crazy testing to know if we’re logged in or not.
Hi,
How can i use this library through a proxy server with/without username and password ?
Thanks
Hello,
I’m not quite sure what you are asking…you always need a username and password when working with imap? As I understand it, the proxy server should always behave the same to the original server ( a proxy) as far as the client is concerned.
This means that using this library you should only need to swap out the server address in gmail_imap.py / __init__ .
If you’re looking to understand how to work with Python imap, the rest of the articles should serve as a good example, but it’s not a ready to go ‘library’. You might checkout libpymail.
Let me know if you have further questions,
Verpa
For anyone who gets this far…note that Google has added OAuth as valid method of logging into IMAP now:
http://googlecode.blogspot.com/2010/03/oauth-access-to-imapsmtp-in-gmail.html
It’s not a web based API, but it’s a start!
Switched repo over to github as a few people have suggested new features.
Your Python scipts work like CHARM. Keep up the good work!!!
Thanks a lot.
[...] Python Gmail IMAP : part 1 January 2010 5 comments 4 [...]
# dude…
#!/usr/bin/env python
import imaplib
M=imaplib.IMAP4_SSL(‘imap.gmail.com’, 993)
M.login(‘myemailaddress@gmail.com’,'password’)
status, count = M.select(‘Inbox’)
status, data = M.fetch(count[0], ‘(UID BODY[TEXT])’)
print data[0][1]
M.close()
M.logout()
#this is all you need to write to get complete mail via IMAP :p
To delete email add:
File (gmail_messages.py):
def deleteMessage(self,uid):
status = self.server.imap_server.uid(‘STORE’,uid, ‘+FLAGS’, ‘(\Deleted)’)
self.server.imap_server.expunge()
File (gmail_imap.py):
for msg in gmail.messages:
message = gmail.messages.getMessage(msg.uid)
print message.Subject
print message.Body
gmail.messages.deleteMessage(msg.uid)
This will read every mail. Print it & then delete sed email
Hello Ryan,
Looks like I missed your comment by a day, but if you ever see this, please feel free to fork gmail_imap on github and add your improvements.
Hi,
Nice little library. I can’t find any licensing information, what license is it released under. If you don’t really mind then the apache license would be great from my point of view.
Thanks,
Francis
Hey Francis, help yourself. I claim no license on the code, which you can Apache license if you like. Be sure to check the licenses for the libraries I use however. None were particularly onerous.
Do let me know what you get up to with the template ( I think of the code as more a template than a library ) … I’d be curious to know.
Verpa
You, sir, are awesome. This code fits my needs perfectly and I thank you deeply.
Python, IMAP and Gmail – what fun!
For a ready-to-use Gmail backup solution, try GmailVault! We had fun building it and we hope you find it useful.