reading only new messages in imaplib

R

Raghul

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.

Thanks in advance
 
K

Kartic

Raghul said the following on 2/22/2005 11:24 PM:
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.

Thanks in advance


import imaplib, sys

conn = imaplib.IMAP4_SSL(host='mail.example.com')
# or conn =imaplib.IMAP4(host='mail.example.com') for no SSL

try:
(retcode, capabilities) = conn.login('user', 'pass')
except:
print sys.exc_info()[1]
sys.exit(1)
conn.select(readonly=1) # Select inbox or default namespace
(retcode, messages) = conn.search(None, '(UNSEEN)')
if retcode == 'OK':
for message in messages[0].split(' '):
print 'Processing :', message
(ret, mesginfo) = conn.fetch(message, '(BODY[HEADER.FIELDS (SUBJECT
FROM)])')
if ret == 'OK':
print mesginfo,'\n',30*'-'
conn.close()


Please also go thru the IMAP RFC to learn more about the flags field and
the IMAP protocol in general. If you're developing something serious
using IMAP, it will be very beneficial to you to understand the protocol.

http://www.imap.org/papers/biblio.html

Thanks,
-Kartic
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top