Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

reading only new messages in imaplib

4,042 views
Skip to first unread message

Raghul

unread,
Feb 22, 2005, 11:24:02 PM2/22/05
to
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

Tony Meyer

unread,
Feb 22, 2005, 11:45:37 PM2/22/05
to Raghul, pytho...@python.org
> 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

Raghul

unread,
Feb 23, 2005, 12:02:44 AM2/23/05
to
eceived: from web50
Received: by twmail.
Received: from mail.
Received: from mail.
Received: by twmail.
Received: from adams
Received: by twmail.
Received: by twmail.
Received: by twmail.
Received: from ns2.T
Received: by twmail.
Received: by twmail.
Received: from chat3
Received: by twmail.
Received: by twmail.
Received: from CRM (
Received: by twmail.
Received: by twmail.
Received: by twmail.
Received: by twmail.
thread-index: AcUTaP
Received: by twmail.
Received: from web60
Received: from web60
Received: by twmail.
Received: by twmail.
Received: by twmail.
Received: by twmail.
Received: by twmail.
Received: by twmail.
Received: from ablis
Received: by twmail.
thread-index: AcUXP/
Received: from web50
Received: from mta.n
Received: from ns2.T
Received: by twmail.
Received: by twmail.
Received: by twmail.
Received: by twmail.
Received: from ns2.T
Received: by twmail.


it prints something like this. what it actually display.And pls tell me
what it actually does? I can't understand

Kartic

unread,
Feb 23, 2005, 1:41:51 PM2/23/05
to
Raghul said the following on 2/22/2005 11:24 PM:


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

Tony Meyer

unread,
Feb 23, 2005, 6:45:32 PM2/23/05
to Raghul, pytho...@python.org
[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.

[Tony Meyer]


>> 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.

[...]

[Raghul]


> Received: by twmail.
> Received: from mail.

[...]


> it prints something like this. what it actually display.

Did you read what I wrote, or just run the code? As I said, it displays the
first 20 characters of each undeleted message. If you can't see where the
'first 20 characters' bit is in the code, then you really need to forget
about IMAP for the moment and start with the Python tutorial.

> And pls tell me what it actually does? I can't understand

>>> import imaplib

Imports the imaplib module.

>>> i = imaplib.IMAP4("mail.example.com")

Creates an imaplib.IMAP4 instance to be connected to the mail.example.com
server.

>>> i.login("username", "password")

Logs into the server with the username "username" and the password
"password".

>>> i.select()

Selects the inbox.

>>> for msg_num in i.search(None, "UNDELETED")[1][0].split():

Does an IMAP search for UNDELETED messages. Assumes that [0] of the
response is "OK" and that [1] of the response is a tuple of the string of
the ids. Splits the string on whitespace and iterates through it.

... msg = i.fetch(str(msg_num), "RFC822")

Fetches the message (header and body in RFC822 format) for the given message
number.

... print msg[1][0][1][:20]

Digs down through the response (use imaplib.debug = 4 to see it) to get the
message as a string. Prints out 20 characters of it.

As basically everyone has said, you must read the IMAP RFC and understand it
in order to use imaplib. Please do so.

=Tony.Meyer

Raghul

unread,
Feb 23, 2005, 9:51:50 PM2/23/05
to
Thanks ya.It helped me a lot ............

0 new messages