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.
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.
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.
import imaplib obj = imaplib.IMAP4_SSL('imap.gmail.com','993') obj.login('username','password') obj.select() obj.search(None,'UnSeen')
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With