Archive for August 2012
Show subject for unseen IMAP mails
This python script handles MIME encodings and outputs everything in utf-8.
I use it with conky in spectrwm bar.
import imaplib from email.header import decode_header mail = imaplib.IMAP4_SSL('imap.gmail.com') # your imap server goes here mail.login('example@gmail.com', 'password') mail.select('INBOX', readonly=True) typ, new = mail.search(None, "UNSEEN") new = new[0].split() if (len(new) > 0): res = list() for msg in new: typ, data = mail.fetch(msg, '(BODY[HEADER.FIELDS (SUBJECT)])') # FROM, DATE, etc data = data[0][1].strip() decoded = decode_header(data[9:])[0] # "Subject: " is 9 characters long text = decoded[0].replace("\r\n", "") if (decoded[1] != None): text = unicode(text, decoded[1]) text = text[:20].strip() # Cut off first 20 characters from subject res.append(text.encode('utf-8')) print ', '.join(res)