POP3 and email

pythonhda pythonhda at yahoo.com.replacepythonwithlinux
Sun Jun 6 15:07:35 EDT 2004


On Sun, 06 Jun 2004 10:25:04 -0400
Paul Schmidt <wogsterca at yahoo.ca> wrote:

> 
> There is some stuff that isn't clear here, can I pass the POP3 message 
> directly to email for processing, or does it need to be saved to a 
> temporary file somewhere first? The emails being received will consist 
> of headers, a tiny amount of text, and one or more MIME attachments.
> 

Yes you can pass the POP3 message directly. IIRC, when you download a message
you'll get a tuple back that looks something like (response_code, message, size).
The "message" is a list of lines in the message.

To have the email package process the message, you can do something like:

>>> import email.Parser
>>> myemail = email.Parser.Parser().parsestr('\n'.join(mesg[1]))

Where mesg is the full tuple that you downloaded. You can use the "get" methods
on "myemail" to retrieve the parts you want.

This should give you the text of the message (if it isn't a multipart message):

>>> mymessage = myemail.get_payload()

If it is multipart, use the "walk" method to iterate through each part and the 
get the payload.

>>> for part in myemail.walk():
...	mypart = part.get_payload()
...	# do something

Check out Text Processing in Python: http://gnosis.cx/TPiP/ 
It's a great book to buy, chapter 5 is about the email module among other things.




More information about the Python-list mailing list