[Email-SIG] How to extract attachment?

Lukasz Szybalski szybalski at gmail.com
Mon Jun 26 20:55:32 CEST 2006


Hello,
I received an email with attachment. The attachment can be binary or text.
How do I extract and save the message to database and attachment to some folder?

(f_name,f_email)=email.Utils.parseaddr(m['from'])
(t_name,t_email)=email.Utils.parseaddr(m['to'])
(r_name,r_email)=email.Utils.parseaddr(m['reply_to'])
subject = m['subject']
 body=m.get_payload()

Since payload is multipart now. get_payload will be a list of parts.

1. How can i do:

for i in m.get_payload():
    save text to db (or extract text as string)
    save attachment to file with correct extension [.xml .exe .doc
.pdf] (How can i extract file so i can do f.write(file) )

2.How can i distinguish what is what?  Will get_payload() display text
first and then attachment?


Thanks
Lukasz


More information about the Email-SIG mailing list