Thanks in advance
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
it prints something like this. what it actually display.And pls tell me
what it actually does? I can't understand
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]
>> 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