Using poplib to parse headers - Thank You All!

Jean-Claude Neveu jcn-france1972 at
Mon Jul 7 03:10:20 CEST 2008

Tim Roberts wrote:
>You've received some very confusing advice in this thread.  Alex had the
>right answer, but I want to expand it a bit.
>poplib.retr gives you a string.  You need to hand that string to the email
>module, and you do that using "email.message_from_string".

This is just to thank Tim, Alex, and everyone who posted for their 
help in getting this to work. It's been more than a month since you 
answered my question and I've been having to focus on other things. 
Last week, I finally got back to this task and your answers helped me 
get it working.

To save time for future people who might have the same question, I'm 
posting a basic script for reading mail from a POP server and passing 
the email message to the Python email library for parsing. I'm new to 
Python (but experienced in other languages) so please feel free to 
tell me if any of my syntax is clumsy, or if I did something the 
difficult way when Python has an easier or more efficient way to do 
it. I hope this will be useful for the archives.

import getpass, poplib, email

# Set up the connection to the POP server
popconnection = poplib.POP3('')

# Find out how many messages are waiting
numMessages = len(popconnection.list()[1])

# Iterate through the messages
for i in range(numMessages):

# retr will be called three times for each email message in the j loop below.
# The first call will return a string telling you how many bytes (i.e., octets)
# are in the message. The string will look like this: OK <byte count> octets
# (where <byte count> is the total number of bytes in the message).
# The second call will return a list containing the elements of the message.
# The third call will return an integer containing the total number of
# bytes in the message

     for j in popconnection.retr(i+1):

# We are interested only in the contents of the list. And we need to put the
# list contents into a string, because that is what 
message_from_string expects.
# We must also be sure to put a line break at the end of the substring that
# represents each list element, as message_from_string relies on line breaks
# to parse the email message. The k loop below builds the string from the list.
         if type(j) == list:
             numElements = len(j)
             outString = ""
            for k in range(numElements):
                 outString += j[k]
                 outString += '\n'
             message = email.message_from_string(outString)

# Now that we have got the contents of the email into an email object, we can
# access the logical elements of the email at will, in any order. The call to
# get_payload() is to retrieve the body of the message.
             print message['Subject']
             print message['From']
             print message.get_payload()

# Strictly speaking, all we need to do on quitting is to disconnect, 
but in some
# applications it would be a good idea to delete the email from the server.

More information about the Python-list mailing list