reading only new messages in imaplib

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Raghul

    #1

    reading only new messages in imaplib

    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

  • Kartic

    #2
    Re: reading only new messages in imaplib

    Raghul said the following on 2/22/2005 11:24 PM:[color=blue]
    > 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
    >[/color]


    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.



    Thanks,
    -Kartic

    Comment

    Working...