reading only new messages in imaplib
Tony Meyer
t-meyer at ihug.co.nz
Tue Feb 22 23:45:37 EST 2005
> Is it posssible to read only the new messages or unread
> messages using imaplib in python? If it is possible pls
> specify the module or give a sample code.
This will print out the first 20 chars of each undeleted message. You
should be able to figure out how to do what you want from it.
>>> import imaplib
>>> i = imaplib.IMAP4("mail.example.com")
>>> i.login("username", "password")
('OK', ['[CAPABILITY IMAP4REV1 IDLE NAMESPACE MAILBOX-REFERRALS BINARY
UNSELECT SCAN SORT THREAD=REFERENCES THREAD=ORDEREDSUBJECT MULTIAPPEND] User
username authenticated'])
>>> i.select()
('OK', ['12'])
>>> for msg_num in i.search(None, "UNDELETED")[1][0].split():
... msg = i.fetch(str(msg_num), "RFC822")
... print msg[1][0][1][:20]
...
Return-Path: <kelly_
Return-Path: <J.M.La
Return-Path: <H.L.Sc
Return-Path: <elenat
[etc]
Simply playing around with an imaplib.IMAP4 class in an interactive prompt,
with imaplib.debug set to 4 and a copy of the RFC handy is the best way to
learn.
=Tony.Meyer
More information about the Python-list
mailing list