GMail for Python
A Pythonic interface to Google's GMail, with all the tools you'll need. Search, read and send multipart emails, archive, mark as read/unread, delete emails, and manage labels.
This library is still under development, so please forgive some of the rough edges
Heavily inspired by Kriss "nu7hatch" Kowalik's GMail for Ruby library
Author
Installation
For now, installation is manual (pip support not yet implemented) and the only requirement is to use Python 2 (2.7+ to be precise):
git clone git://github.com/charlierguo/gmail.git
Features
- Search emails
- Read emails
- Emails: label, archive, delete, mark as read/unread/spam, star
- Manage labels
Basic usage
To start, import the gmail library.
import gmail
Authenticating gmail sessions
To easily get up and running:
import gmail
g = gmail.login(username, password)
Which will automatically log you into a GMail account. This is actually a shortcut for creating a new Gmail object:
from gmail import Gmail
g = Gmail()
g.login(username, password)
# play with your gmail...
g.logout()
You can also check if you are logged in at any time:
g = gmail.login(username, password)
g.logged_in # Should be True, AuthenticationError if login fails
OAuth authentication
If you have already received an OAuth2 access token from Google for a given user, you can easily log the user in. (Because OAuth 1.0 usage was deprecated in April 2012, this library does not currently support its usage)
gmail = gmail.authenticate(username, access_token)
Filtering emails
Get all messages in your inbox:
g.inbox().mail()
Get messages that fit some criteria:
g.inbox().mail(after=datetime.date(2013, 6, 18), before=datetime.date(2013, 8, 3))
g.inbox().mail(on=datetime.date(2009, 1, 1)
g.inbox().mail(sender="myfriend@gmail.com") # "from" is reserved, use "fr" or "sender"
g.inbox().mail(to="directlytome@gmail.com")
Combine flags and options:
g.inbox().mail(unread=True, sender="myboss@gmail.com")
Browsing labeled emails is similar to working with your inbox.
g.mailbox('Urgent').mail()