Script to fetch IMAP unread / unseen message counts?

Gerhard =?unknown-8bit?Q?H=E4ring?= gerhard.haering at gmx.de
Mon Feb 17 16:39:21 EST 2003


* John J Lee <jjl at pobox.com> [2003-02-17 20:51 +0000]:
> Anybody have one?
> 
> Google found a couple on a newsgroup, but the URLs given are broken.

Here's a one that I used on my home intranet ;-)

#v+
#!/usr/bin/env python

import imaplib

class ImapCheckResult:
    def __init__(self):
	self.unseen_messages= 0
	self.new_messages = 0
    
    def __repr__(self):
	return "Unseen: %i, New: %i" % (self.unseen_messages, self.new_messages)

def check_imap_folder(host, user, passwd, foldername):
    result = ImapCheckResult()
    imap = imaplib.IMAP4(host)
    imap.login(user, passwd)
    typ, data = imap.select(foldername, 1)
    typ, data = imap.search(None, 'UNSEEN')
    result.unseen_messages = len(data[0].split())
    typ, data = imap.search(None, 'NEW')
    result.new_messages = len(data[0].split())
    imap.logout()
    return result

def main():
    print check_imap_folder("myhost", "myuser", "mypass", "INBOX")

if __name__ == '__main__':
    main()
#v-

Gerhard
-- 
Favourite database:             http://www.postgresql.org/
Favourite programming language: http://www.python.org/
Combine the two:                http://pypgsql.sf.net/
Embedded database for Python:   http://pysqlite.sf.net/





More information about the Python-list mailing list