First of all lets see how can we get the libgmail help, a.k.a man pages:
>>>import libgmailThen lets see how can we retrieve the messages in our the inbox:
>>>help(libgmail)
>>>import libgmailNow lets say we want to print all the attached files in all the messages labled by "projectx"
>>>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
>>>import libgmailYou may also send a message this way:
>>>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()
>>>import libgmailTo send attachmented files into the message:
>>>ga = libgmail.GmailAccount("username@gmail.com", "password")
>>>ga.login()
>>>msg=libgmail.GmailComposedMessage("friend@gmail.com", "SubjectHere", "BodyHere")
>>>ga.sendMessage(msg)
>>>import libgmailFinally 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:
>>>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)
>>>try:
>>>[TAB]ga.sendMessage(msg)
>>>except:
>>>[TAB]ga.sendMessage(msg)
Tags: Python, Gmail, Gr33n Data
Thanks a lot for the tutorial! Just what I needed! Really saved me a lot of time and effort.
ReplyDeleteYou're welcome man
ReplyDeleteThats just great !! Thanks Tarek.
ReplyDeleteI 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]?
Hey got it.. it's "msg.sender" to print the senders name.
ReplyDeletesorry confused....
ReplyDeletemsg.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]
How to remove details while retrieving mail body ?
ReplyDeleteI 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.
ReplyDeletehey i've installed the package tried to write your basic code:
ReplyDeleteimport 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?
I have no idea, why this happens to you. Unfortunately, I haven't used that library since ages ago
ReplyDeleteFor the record: the "login failed" is fixed in the CVS version, which for some reason the team hasn't updated.
ReplyDeleteIf 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.
You can just put code within html pre tags to keep it formatted instead of using [TAB]
ReplyDeletehello there, any idea about
ReplyDelete>>> 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
I'm reading in other forum "libgmail is not compatible with the new interface of Gmail".
ReplyDeleteAnd the project appears stopped of new versions 2 years ago.
=(