Lightweight Python Gmail mail management library. Search, read, archive, mark as read/unread, delete emails, and manage labels.
To start, import the gmail
library.
import gmail
im = gmail.connect($email, $pwd)
mail_ids = gmail.read_email_from_gmail(im, "INBOX", '(FROM "test@adrii.com" UNSEEN)')
mail_ids = gmail.read_email_from_gmail(im, "INBOX", '(FROM "test@adrii.com" UNSEEN)')
for uid in mail_ids[0].split():
mail_data = gmail.fetch(im, uid)
for response_part in mail_data:
arr = response_part[0]
if isinstance(arr, tuple):
msg = gmail.getContent(arr)
email_subject = gmail.decode_mime_words(msg['subject'])
email_from = gmail.decode_mime_words(msg['from'])
email_to = gmail.decode_mime_words(msg['to'])
email_body = gmail.parseBody(msg)
body_txt = str(email_body['text'])
body_html = str(email_body['html'])
print("Subject: "+ email_subject + "\n")
print("From: "+ email_from + "\n")
print("To: "+ to + "\n")
print("Body HTML: "+ body_html + "\n")
print("Body TXT: "+ body_txt + "\n")
gmail.read(im, uid)
email.unread(im, uid)
folder = 'Processed'
gmail.move_to(im, uid, folder)
gmail.archive(im, uid)
gmail.delete(im, uid)
flags = ["Processed", "foo", "var"]
gmail.add_label(im, uid, flags)
flags = ["Processed", "foo", "var"]
gmail.rm_label(im, uid, flags)
flags = gmail.getFlags(im, uid)
gmail.disconnect(im)
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.