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_S SL(host='mail.e xample.com')
# or conn =imaplib.IMAP4( host='mail.exam ple.com') for no SSL
try:
(retcode, capabilities) = conn.login('use r', 'pass')
except:
print sys.exc_info()[1]
sys.exit(1)
conn.select(rea donly=1) # Select inbox or default namespace
(retcode, messages) = conn.search(Non e, '(UNSEEN)')
if retcode == 'OK':
for message in messages[0].split(' '):
print 'Processing :', message
(ret, mesginfo) = conn.fetch(mess age, '(BODY[HEADER.FIELDS (SUBJECT
FROM)])')
if ret == 'OK':
print mesginfo,'\n',3 0*'-'
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