Script to fetch IMAP unread / unseen message counts?

Donn Cave donn at drizzle.com
Mon Feb 17 23:48:24 EST 2003


Quoth Gerhard Hring <gerhard.haering at gmx.de>:
| * 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")

You can also get these particular facts with

    imap.status(foldername, '(UNSEEN RECENT)')
        -> ('OK', ['folder (RECENT 5 UNSEEN 0)'])

parsing left as an exercise, as usual.

	Donn Cave, donn at drizzle.com




More information about the Python-list mailing list