Are you looking for my non-technical blog?

This is now my technical-only blog, my non-technical blog is here.

16 December 2005

Python - libgmail Tutorial

This is a short tutorla about libgmail. Which is a Library to provide access to Gmail via Python. Tabs are important in python as they indicate the different code blocks, since they do not have "{" and "}" like those in C and C++. So I'll replace the tabs here with [TAB], since it's hard to write the proper format in HTML.

First of all lets see how can we get the libgmail help, a.k.a man pages:
>>>import libgmail
>>>help(libgmail)
Then lets see how can we retrieve the messages in our the inbox:
>>>import libgmail
>>>ga = libgmail.GmailAccount("username@gmail.com", "password")
>>>ga.login()
>>>folder = ga.getMessagesByFolder("inbox", True)
>>>for thread in folder:
>>>[TAB]print "Thread:", thread.id, "Subject:", thread.subject
>>>[TAB]for msg in thread:
>>>[TAB][TAB]print "Msg:", msg.id, ",Author", msg.author, ",Subject:", msg.subject
Now lets say we want to print all the attached files in all the messages labled by "projectx"
>>>import libgmail
>>>ga = libgmail.GmailAccount("username@gmail.com", "password")
>>>ga.login()
>>>folder = ga.getMessagesByLabel("projectx", True)
>>>for tread in folder:
>>>[TAB]for msg in thread:
>>>[TAB][TAB]for attach in msg.attachments:
>>>[TAB][TAB][TAB]print attach._getContent()
You may also send a message this way:
>>>import libgmail
>>>ga = libgmail.GmailAccount("username@gmail.com", "password")
>>>ga.login()
>>>msg=libgmail.GmailComposedMessage("friend@gmail.com", "SubjectHere", "BodyHere")
>>>ga.sendMessage(msg)
To send attachmented files into the message:
>>>import libgmail
>>>ga = libgmail.GmailAccount("username@gmail.com", "password")
>>>ga.login()
>>>myFiles = ["/file1", "/file2", "/file3"]
>>>msg=libgmail.GmailComposedMessage("friend@gmail.com", "SubjectHere", "BodyHere", filenames=myFiles)
>>>ga.sendMessage(msg)
Finally I've noticed that sometimes the sendMessage() function fails - returns "HTTP Error 404: Not Found", and hope that they are going fix it soon. Actually the function fails on the first time but when you call it again it works, so you may use a "try-except" statement like the one below:
>>>try:
>>>[TAB]ga.sendMessage(msg)
>>>except:
>>>[TAB]ga.sendMessage(msg)

Tags: , ,

13 comments:

  1. Thanks a lot for the tutorial! Just what I needed! Really saved me a lot of time and effort.

    ReplyDelete
  2. Thats just great !! Thanks Tarek.

    I need one help though. How do I extract or print the e-mail address of the sender in a particular thread [msg.author - doesn't give me the authors mail address]?

    ReplyDelete
  3. Hey got it.. it's "msg.sender" to print the senders name.

    ReplyDelete
  4. sorry confused....

    msg.author -> prints name
    msg.sender -> senders mail address

    Thanks again man.. Is there a place to see these member names [help(libgmail) doesn't have them]

    ReplyDelete
  5. How to remove details while retrieving mail body ?

    ReplyDelete
  6. I haven't any idea for this service and i heard this for the first time in your blog. Thanks for sharing this informative tutorial with us.

    ReplyDelete
  7. hey i've installed the package tried to write your basic code:

    import libgmail

    ga = libgmail.GmailAccount('username@gmail.com','password')

    ga.login()

    and i get this execption:
    gmailloginfailure: login failed' (wrong username/password)'

    i am running py 2.6 and i just cant login.

    any ideas?

    ReplyDelete
  8. I have no idea, why this happens to you. Unfortunately, I haven't used that library since ages ago

    ReplyDelete
  9. For the record: the "login failed" is fixed in the CVS version, which for some reason the team hasn't updated.

    If you don't want to install CVS (I didn't), visit the Sourceforge site, go to the CVS page, click "Browse source" near the top, and click through the folders for libgmail. Once you reach the main source directory, you'll see that only two files have version numbers greater than 0.1.11; get the file that ends in "py" (you can get the CHANGES file if you want, but it doesn't matter) and save it over your copy.

    This made everything work for me.

    Good luck.

    ReplyDelete
  10. You can just put code within html pre tags to keep it formatted instead of using [TAB]

    ReplyDelete
  11. hello there, any idea about
    >>> ga=libgmail.GmailAccount("user@gmail.com","user123")
    >>> ga.login()
    Traceback (most recent call last):
    File "", line 1, in
    File "libgmail.py", line 317, in login
    pageData = self._retrievePage(req)
    File "libgmail.py", line 358, in _retrievePage
    req = ClientCookie.Request(urlOrRequest)
    File "build\bdist.win32\egg\mechanize\_request.py", line 31, in __init__
    File "build\bdist.win32\egg\mechanize\_rfc3986.py", line 62, in is_clean_uri
    TypeError: expected string or buffer

    ReplyDelete
  12. I'm reading in other forum "libgmail is not compatible with the new interface of Gmail".
    And the project appears stopped of new versions 2 years ago.
    =(

    ReplyDelete