Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check unread count of Gmail messages with Python

How can I check the number of unread Gmail message in my inbox with a short Python script? Bonus points for retrieving the password from a file.

like image 409
Steven Hepting Avatar asked Jun 04 '09 23:06

Steven Hepting


People also ask

How can you tell how many unread emails you have in Gmail?

Click on the “Advanced” tab. Scroll down to the “Unread message icon” option, click “Enable,” and then select “Save Changes.” Gmail will refresh, and from now on, the email icon in your Gmail tab will always have the number of unread messages displayed, no matter where you are in Gmail.

How do I get a count in Gmail?

Make the Gmail Unread Count More Visible in TabsSelect See all settings. Select the Advanced tab. In the Unread message icon section, select Enable, then select Save Changes. Now, your Gmail Chrome tab will display the number of unread messages you currently have.


2 Answers

import imaplib obj = imaplib.IMAP4_SSL('imap.gmail.com','993') obj.login('username','password') obj.select() obj.search(None,'UnSeen') 
like image 67
Avadhesh Avatar answered Oct 14 '22 23:10

Avadhesh


Well, I'm going to go ahead and spell out an imaplib solution as Cletus suggested. I don't see why people feel the need to use gmail.py or Atom for this. This kind of thing is what IMAP was designed for. Gmail.py is particularly egregious as it actually parses Gmail's HTML. That may be necessary for some things, but not to get a message count!

import imaplib, re conn = imaplib.IMAP4_SSL("imap.gmail.com", 993) conn.login(username, password) unreadCount = re.search("UNSEEN (\d+)", conn.status("INBOX", "(UNSEEN)")[1][0]).group(1) 

Pre-compiling the regex may improve performance slightly.

like image 35
Matthew Flaschen Avatar answered Oct 15 '22 01:10

Matthew Flaschen