Gmail is a project mainly written in Python, it's free.
A Python library for interacting with Gmail
gmail.py wraps imaplib and smtplib to make it easier to interact with your Gmail account through python.
Examples:
Login (this automatically connects to IMAP and SMTP) and then out:
import gmail conn = gmail.Gmail('username', 'password')
do stuff...
conn.logout()
Interact with labels:
conn.labels.switch('sent') 1567 # Number of emails in that label conn.labels.create('mylabel') conn.labels.delete('mylabel') conn.labels.exists('foo') False len(conn.labels.all()) 25
Search email:
Chainable queries
conn.search.subject('foo').to('[email protected]') conn.search.unread() msgs = conn.search.all()
msgs will be unread emails sent to '[email protected]'
with 'foo' in the subject
Download email:
msgs = conn.search.unread().all() msgs [<gmail.Message object...] msgs[0].subject() 'V1agra for Sale! #1 ONLINE PHARMACY!' msgs = conn.all()
Send email:
msg = gmail.Message().fill('[email protected]', '[email protected]', ... 'fwd: fwd: funny', 'pictures of cats') msg.to_addresses() ['[email protected]'] msg.subject() 'fwd: fwd: funny' conn.send(msg)
Notes:
Messages are not downloaded when they are created. They store their id and a reference to your IMAP connection. When you first try to read anything from them, they download.
Basically zero error handling is done right now.
API (no return unless indicated):
Gmail.init(username, password)
Gmail.logout(close_imap=True, close_smtp=True)
Gmail.fetch(id)
Gmail.send(message)
Gmail.search.clear()
Gmail.search.first()
Gmail.search.all()
Gmail.search.count()
Gmail.search.unread() Gmail.search.read() Gmail.search.to(query) Gmail.search.from_(query) Gmail.search.subject(query) Gmail.search.bcc(query) Gmail.search.cc(query) Gmail.search.body(query) Gmail.search.since(date) Gmail.search.before(date) Gmail.search.on(date)
Gmail.labels.exists(label)
Gmail.labels.switch(label)
Gmail.labels.create(label)
Gmail.labels.delete(label)
Gmail.labels.rename(old, new)
Gmail.labels.all()
Gmail.labels.current
Gmail.labels.count
Message.init(options)
Message.fill(to_addresses='', from_address='', subject='', body='')
Message.fetch()
Message.from_address(address)
Message.to_address(address)
Message.subject(subject)
Message.body(body)
Message.read() Message.unread()
Message.delete()
Message.archive()
Message.star() Message.unstar()
Message.spam()
Message.flag(flag) Message.unflag(flag)
Message.add_label(label)
Message.move_to(label)