You are not logged in.

#1 2009-03-19 14:21:50

r6
Member
From: Melbourne
Registered: 2008-07-02
Posts: 156

[Solved] Script to count new mail

This is a very simple problem that's confusing the hell out of me.

I want to show how many new (unread) emails I have, in my Awesome taskbar, and have it run every 10 minutes.

So I wrote a small perl script and it worked, showing 1 new mail.

The problem is that running the script again says there's no new mail. I figured the library calls were setting the new mail as read. So I tried again in python using imaplib, which lets you open the imap folder as readonly. But the same problem happens again, subsequent calls say there's no new mail.

I tried someone else's script found here but got the same result.

Can someone tell me what I'm missing here?

Last edited by r6 (2009-03-24 03:12:40)

Offline

#2 2009-03-19 17:46:08

byte
Member
From: Düsseldorf (DE)
Registered: 2006-05-01
Posts: 2,046

Re: [Solved] Script to count new mail

After a quick glance over the Python docs and the IMAP RFC, I'd say you should use a read-only SELECT of the mailbox (essentially the same as EXAMINE) after LOGIN and then use SEARCH RECENT (or UNSEEN).
Now get that into Python (or Perl). ;-]

Last edited by byte (2009-03-19 17:48:38)


1000

Offline

#3 2009-03-19 19:13:51

lswest
Member
From: Munich, Germany
Registered: 2008-06-14
Posts: 456
Website

Re: [Solved] Script to count new mail

I'm using a script I found in conky that pulls similar information out of the RSS/atom feed from gmail.

Here's the code:

## check-gmail.py -- A command line util to check GMail -*- Python -*-
## modified to display mailbox summary for conky

# ======================================================================
# Copyright (C) 2006 Baishampayan Ghose <b.ghose@ubuntu.com>
# Modified 2008 Hunter Loftis <hbloftis@uncc.edu>
# Time-stamp: Mon Jul 31, 2006 20:45+0530
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
# ======================================================================

import sys
import urllib             # For BasicHTTPAuthentication
import feedparser         # For parsing the feed
from textwrap import wrap

_URL = "https://mail.google.com/gmail/feed/atom"

uname = sys.argv[1]
password = sys.argv[2]
maxlen = sys.argv[3]

urllib.FancyURLopener.prompt_user_passwd = lambda self, host, realm: (uname, password)
    
def auth():
    '''The method to do HTTPBasicAuthentication'''
    opener = urllib.FancyURLopener()
    f = opener.open(_URL)
    feed = f.read()
    return feed

def readmail(feed, maxlen):
    '''Parse the Atom feed and print a summary'''
    atom = feedparser.parse(feed)
    print '%s@gmail.com (%s new)\n' % (uname, len(atom.entries))
#    for i in range(min(len(atom.entries), maxlen)):
#        print ' "%s"' % atom.entries[i].title
#        print '    %s' % atom.entries[i].author
#    if len(atom.entries) > maxlen:
#        print ' more...'

if __name__ == "__main__":
    f = auth()  # Do auth and then get the feed
    readmail(f, int(maxlen)) # Let the feed be chewed by feedparser

then you just call the script with python /path/to/script/ username password #ofEmailTitlesToDisplay.

Hope that helps?
Lswest

it requires the package python-feedparser from extra.

Last edited by lswest (2009-03-19 19:14:34)


Lswest <- the first letter of my username is a lowercase "L".
"...the Linux philosophy is "laugh in the face of danger". Oops. Wrong one. "Do it yourself". That's it." - Linus Torvalds

Offline

#4 2009-03-20 03:39:05

r6
Member
From: Melbourne
Registered: 2008-07-02
Posts: 156

Re: [Solved] Script to count new mail

@lswest: Unfortunately my mailbox has no rss feed, it's just pop3/imap on my ISP's mailserver.

@byte: The read-only SELECT isn't working for me.

typ, data = imap.select(foldername, 1)

The 1 is supposed to open it as readonly but it doesn't seem to work. There's another library that has an examine method which I'm trying now.

Answer found.

Last edited by r6 (2009-03-24 03:10:52)

Offline

Board footer

Powered by FluxBB