Counting the number of messages in an e-mail account with python

Gabriele Cirulli picture Gabriele Cirulli · Jun 10, 2010 · Viewed 9.2k times · Source

is there any way, in Python, to have access to an e-mail account (I'll need this for gmail but better if any works) and be able to see the number of messages in the inbox (maybe even unread messages only)? Thank you.

Answer

Avadhesh picture Avadhesh · Jun 15, 2010

u Can Try This One

import imaplib  
obj = imaplib.IMAP4_SSL('imap.gmail.com', 993)  
obj.login('username', 'password')  
obj.select('Inbox')      <-- it will return total number of mail in Inbox i.e 
('OK', ['50'])  
obj.search(None,'UnSeen')  <-- it will return the list of uids for Unseen mails