Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > gmail/poplib: quickly detecting new mail

Thread Tools

gmail/poplib: quickly detecting new mail

Posts: n/a

I'm trying to monitor my gmail account to know when I have obtained a
new email. It seems that once I have logged in, I should be able to
call the stat() function repeatedly to see how many messages are in my
inbox. The problem is that this number does not seem to update until I
have logged out, and logged back in. In other words, I run the code
below, send myself an email, and observe that the count does not
change. If I kill the program and restart (hence logging back in),
then the total count is now updated. The other function calls seem to
work the same way (eg "list" just shows the same list, even when I know
new mail has arrived).

1. is this a standard behavior of pop protocol? (to give me the same
results for any API call on a single login?)
2. OR is this a peculiarity of gmail
3. is there a more efficient and correct way to see know when I have
received a new mail? Currently my "working" method is to log out and
log back in. With this method, I can get about 17 refreshes per minute
but anything faster and Gmail blocks me for a few minutes. (yes it is
important to my application that I have very frequent refreshes).

(see code sample below)



import poplib
import time
from email.Parser import Parser

parser = Parser()
server = poplib.POP3_SSL("", 995)
print server.user("XXX-MY_EMAIL")
print server.pass_("XXX-MY_PW")

def getMsgCount():
# check message count by stat() and list() functions
numMsgs = server.stat()[0]
print "Num msg by stat():", numMsgs
print "Num msg by list():", len(server.list()[1])
print "Most recent:", numMsgs, getSubj(numMsgs)

def getSubj(which):
# return subject of message with id 'which'
msg = "\n".join(, 1)[1])
email = parser.parsestr(msg)
return email.get("Subject")

while True:
print "--"

Reply With Quote
Alex Martelli
Posts: n/a
LJ <(E-Mail Removed)> wrote:
> 1. is this a standard behavior of pop protocol? (to give me the same
> results for any API call on a single login?)
> 2. OR is this a peculiarity of gmail

Definitely POP3 standard behavior, as a simple reading of the relevant
RFC will show (e.g.,
summarizing... on entering Transaction state, a POP3 server acquires an
exclusive lock on the mailbox, to ensure that no modifications occur to
it throughout the session.

I'm not sure about the refresh frequency, but you may want to try the
Atom feed, .

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
New Python book, "Learn Python Quickly" John Rowland Python 0 03-16-2013 03:28 PM
How Quickly Can You Spot A New Woger Nym? Lawrence D'Oliveiro NZ Computing 2 02-28-2010 12:01 AM
New to Java (a C++ migrant) want to quickly get on to language Amol Java 4 01-19-2007 02:21 PM
New mail pop up! No new mail, still pops up! Moo Firefox 5 09-17-2006 06:49 PM
Detecting new mail? Robb Meade ASP General 3 08-29-2003 05:54 PM