way to extract only the message from pop3

hlubenow hlubenow2 at gmx.net
Thu Apr 5 11:58:50 EDT 2007


flit wrote:

> Hello All,
> 
> Using poplib in python I can extract only the headers using the .top,
> there is a way to extract only the message text without the headers?

As mentioned before, you should use module "email":

------------------------------------

#!/usr/bin/env python

import poplib
import email
import os
import sys
import string


PROVIDER = "pop.YourMailProvider.de"
USER = "YourUserName"
PASSWORD = "YourPassword"

try:
    client = poplib.POP3(PROVIDER)
except:
    print "Error: Provider not found."
    sys.exit(1)

client.user(USER)
client.pass_(PASSWORD)

nrof_mails = len(client.list()[1])

for i in range(nrof_mails):
    lines = client.retr(i + 1)[1]
    mailstring = string.join(lines, "\n")
    blockit = 0 

    msg = email.message_from_string(mailstring)

    for part in msg.walk():

        if part.get_content_maintype() == "text" and blockit == 0: 
            blockit = 1 
            mycontent = part.get_payload() 
            mycontent = mycontent.decode("quopri_codec") 
            print mycontent 
            print

client.quit()

------------------------------------

See You

H.



More information about the Python-list mailing list