Retrieve Email Attachments

Lutz Horn lho at gmx.de
Fri May 14 02:28:24 EDT 2004


> I need to access a POP or mayb IMAP based email account and extract
> attachments (probably images) from the email messages.  How can this
> be done with the current two module libraries?

Use poplib or imaplib to get a message as a string.

 >>> import poplib
 >>> connection = poplib.POP3("servername")
 >>> connection.user("username")
 >>> connection.pass_("password")

Then use the email module (new in 2.2) to parse this string into an 
instance of email.Messsage using

 >>> import email
 >>> msg = email.message_from_string(msg_as_string)

Use the methods of the resulting email.Message instance to get the 
contents of the email.

Lutz



More information about the Python-list mailing list