[Tutor] email & imaplib for beginners like me
Riumu Kuraku
cyresse at gmail.com
Thu Oct 14 10:42:59 CEST 2004
Hi all,
(This started off as a plea for help.)
Just tryng to understand the email package, and the docs are a
little... sparse? Maybe it's more I'm having trouble with OOP.
Anyway, here's a sequence I've learnt from frustrating trial and
error, I'm trying to log on to a mail server, and download a message,
and make it readable.
I thought I'd send it through in case anyone else gets stuck like me.
I'm doing it manually so I can understand how it works before I script it.
I'm having trouble with some stuff like -
a=test.fetch(1, 'RFC822' )... The first argument of fetch is fine.
It's the second that's got me. It's a name of a Internet standard. And
I used it on three different emails, and it got two headers, and one
header and full text. So I am quite confused as to how exactly to get
specific things, like headers/text/attachments only, so any light that
can be shed in that regard would be fantastic.
>>>import imaplib # IMAP protocol library
>>>import email.Parser # email parser library
>>>host="mail... ...com" # IMAP server name
>>>use="<username>" # Your login
>>>pas="<password>" # Your (plain text) password
>>>test=imaplib.IMAP4(host) # Create instance of imap protocol connection
>>>test.login(use, pas) # Send login and password to server
('OK', ['LOGIN Welcome']) # Server likes it
>>>test.select() # Selects INBOX by
default. Good enough for me...
('OK', ['3']) # I have 3 email messages
>>>x=test.fetch(3,'RFC822') #Try and get message 3...
>>> print x
('OK', [('3 (FLAGS (\\Seen hasnoatt) RFC822 {1200}', 'Return-Path:
<cyresse at gmail.com>\r\nReceived: from ....
......<cyresse at gmail.com>\r\nReply-To: cyresse at gmail.com,
cynos at safe-mail.net\r\nTo: protocol_test at allmail.net\r\nSubject:
Boo\r\nMime-Version: 1.0\r\nContent-Type: text/plain;
charset=US-ASCII\r\nContent-Transfer-Encoding:
7bit\r\n\r\nWhios\r\n'), ')'])
..Looks vaguely emailish to me (bit chopped out for brevity)
>>>j=Parser #I want to parse what I downloaded using Parser
>>>print j.parse(x)
TypeError: unbound method parse() must be called with Parser instance
as first argument (got tuple instance instead)
#What does that mean?
.....45 minutes later... oh....
>>>j=Parser() # REALLY IMPORTANT to include the (), I was running
around in circles
# for a looooong time trying to figure that out.
>>>print j.parse(x)
AttributeError: 'tuple' object has no attribute 'readline'
#OK, so x is a tuple, and it has no attr, readline...hmmm...
#Run off to docs, and open parser.py to have a look and:
>>>print j.parsestr(x) #Turns out, Parser.parse() is for file
objects... Parser.parsestr()
# is for string objects, as I am
about to learn.
TypeError: expected read buffer, tuple found
#Still not liking tuple. Hmmm...
>>>i=str(x)
>>>print j.parsestr(i)
>From nobody Thu Oct 14 21:10:42 2004
('OK', [('3 (FLAGS (\\Seen hasnoatt) RFC822 {1200}', 'Return-Path:
<cyresse at gmail.com>\r\nReceived: from ....
.
.....Reply-To: cyresse at gmail.com,
cynos at safe-mail.net\r\nTo:
protocol_test at allmail.net\r\nSubject: Boo\r\nMime-Version:
1.0\r\nContent-Type: text/plain;
charset=US-ASCII\r\nContent-Trarnsfer-Encoding: 7bit\r\n\r\nWhios\r\n'),
')'])
Huzzah! A header, that is easy to read! (Once again, snipped for brevity.)
Now, to reliably be able to get a whole email... I could write my own client : )
More information about the Tutor
mailing list