how to retrieve attachment in a mail ?

Didier FRAISSE dfraisse at free.fr
Wed Oct 29 03:35:03 EST 2003


i need to retrieve every day an email and to save his attachments for later
processing.
i found some piece of code in the documentation like

    import poplib
    pop = poplib.POP3(Host)
    pop.user(User)
    pop.pass(Pwd)
    nbMsg, nbOctet = pop.stat()
    for n in range(nbMsg):
            response, lines, bytes = pop.retr(n+1)

which retrieve email from a pop3 account and

import email.Parser
import os
import sys

mailFile=open(File,"rb")
p=email.Parser.Parser()
msg=p.parse(mailFile)
mailFile.close()

partCounter=1
for part in msg.walk():
    if part.get_main_type()=="multipart":
         continue
    name=part.get_param("name")
    if name==None:
      name="part-%i" % partCounter
    partCounter+=1
    # In real life, make sure that name is a reasonable
    # filename on your OS.
    f=open(name,"wb")
    f.write(part.get_payload(decode=1))
    f.close()
    print name
wich extract every part of a mailFile

BUT i don't understand how to migrate from
            response, lines, bytes = pop.retr(n+1)
where lines is a list to a msg object
        for part in msg.walk():

thanks to help me and many pologize for my poor english
Didier







More information about the Python-list mailing list