reading only new messages in imaplib
Kartic
removethis.kartic.krishnamurthy at gmail.com
Wed Feb 23 13:41:51 EST 2005
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
More information about the Python-list
mailing list