views:

2493

answers:

7

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.

+4  A: 

Well it isn't a code snippet but I imagine using imaplib and the Gmail IMAP instructions get you most of the way there.

cletus
+2  A: 

Use Gmail.py

file = open("filename","r")
usr = file.readline()
pwd = file.readline()
gmail = GmailClient()
gmail.login(usr, pwd)
unreadMail = gmail.get_inbox_conversations(is_unread=True)
print unreadMail

Gets login information from a text file assuming the login name and password are on separate lines.

Hawker
I checked the module source, what it does it open gmail in html mode and parse the page. This is bad! http://www.holovaty.com/code/gmail.py
Nadia Alramli
readline includes a trailing newline, which you don't want here.
Matthew Flaschen
Nadia is right. This will waste significant time and bandwidth.
Matthew Flaschen
measure before you cut
Dustin Getz
+14  A: 

I advise you to use Gmail atom feed

It is as simple as this:

import urllib

url = 'https://mail.google.com/mail/feed/atom/'
opener = urllib.FancyURLopener()
f = opener.open(url)
feed = f.read()

You can then use the feed parse function in this nice article: Check Gmail the pythonic way

Nadia Alramli
+1 nice solution that avoid all the imap/pop business!
Jarret Hardie
What's so bad about IMAP? Note that this snippet is not complete, despite the "simple as this".
Matthew Flaschen
Also note that this solution will prompt the user for username, password on the TTY. However, you can subclass the opener to handle this.
Matthew Flaschen
+1  A: 

Once you are logged in (do this manually or with gmail.py) you should use the feed.

It is located here: http://mail.google.com/mail/feed/atom

It is the way Google does it. Here is a link to their js chrome extension: http://dev.chromium.org/developers/design-documents/extensions/samples/gmail.zip

You will then be able to parse xml that looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<feed version="0.3" xmlns="http://purl.org/atom/ns#"&gt;
<title>Gmail - Inbox for [email protected]</title>
<tagline>New messages in your Gmail Inbox</tagline>
<fullcount>142</fullcount>
Unknown
+11  A: 

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.

Matthew Flaschen
While I happen to agree IMAP is the way to go here, there are some portability issues that could occur. Since the other methods are using HTTP connections, they could conceivably work when IMAP wouldn't (in restrictive environments like Google App Engine or on networks where non-HTTP traffic is restricted).
Tom
Tom, that's true. But the OP didn't say they were dealing with those limitations, so we should not invent them prematurely.
Matthew Flaschen
+1  A: 
import imaplib
obj = imaplib.IMAP4_SSL('imap.gmail.com','993')
obj.login('username','password')
obj.select()
obj.search(None,'UnSeen')
Avadhesh
I really like this solution. Generally clean with only one import. To get the number you'd just: len(obj.search(None, 'UnSeen')[1][0].split())
Steven Hepting
A: 

what about hotmail??

Ahmet Yıldırım