[Tutor] OT - email & imaplib modules

mailing list ml.cyresse at gmail.com
Wed Sep 7 13:34:33 CEST 2005

Hi all, 

Just musing. 

Even though I'm familiar with imaplib & IMAP, and most of the email
modules, I still find them somewhat... obtuse to work with.

The IMAP protocol is rather exposed in imaplib, and you have to read
RFCs and fiddle to sort the format required for IMAP commands.

For an example, I asked an acquaintance who's overjoyed at finding
Ruby after working in PHP to show me how Ruby handled IMAP.

Here's a Ruby line/Python line breakdown of how they compare.

imap = Net::IMAP.new('mail.example.com')
imap = imaplib.IMAP4('mail.example.com')

imap.authenticate('LOGIN', 'joe_user', 'joes_password') 
imap.login('joe_user', 'joes_password')

imap.select('INBOX') #switch to the inbox
imap.select() #In Python it defaults to INBOX

imap.search(["FROM","person at diddlywink.com"]).each do |message_id| #for each
for message_id in imap.search(None, '(FROM
"person at diddlywink.com")')[1][0].split(" "):

response = imap.fetch(message_id, "RFC822.TEXT") #fetch the text
response = imap.fetch(message_ID, "RFC822.TEXT")

puts response.attr['RFC822.TEXT'] #print the text
print response[1][0][1] #print the text

imap.store(message_id, "+FLAGS", [:Deleted]) #flag to delete
imap.store(message_id, "+FLAGS", "\Deleted")

#No Python equivalent

imap.close #close mailbox, delete all flagged as delete as well (no
need to imap.expunge)

imap.disconnect #close session

I also find the nesting horrible to deal with. An imap fetch will
return a tuple, where the last item is ')'. I know that this line  -
for message_id in imap.search(None, '(FROM
"person at diddlywink.com")')[1][0].split(" ")

isn't the clearest way to do it, but I was going for line for line. :)

The email.Message object is also quite complex to learn.

Now, I'm aware the both modules allow you to do incredibly complex
things. But I'm of the opinion that simple things should be.

I'm not sure what's being worked on for Py3K, but I really hope
imaplib and email are.

Would adding a layer over each to simplify things be bad or good? For
instance, instead of IMAP using and returning IMAP specific strings,
why not do stuff like imap.search("FROM", "x at y.com") and then let the
object handle the brackets and quotes etc, and accept ranges of
messages as a list, and why not have ranges returned as something

Ditto with email.Message, how about methods to access email parts, as
per the various RFC's? So calling msg.attachments() lists all
attachments, or msg.format() shows what formatting the email is (text,
RTF, HTML) , or msg.body() returns the BODY/TEXT portion?

It's like Pythoncard, a layer of abstraction makes 90% of what you do
easier, and the other 10% you can still do the manual way.

I don't know, I'm also tempted to try and write those layers, if
anyone would benefit from them. But yeah, would it be better or worse
for people to not have to learn the niceties of RFC3501 formal syntax
section to use imap?

/end idle ponderings


Liam Clarke

More information about the Tutor mailing list