Getting/Saving email attachments w/ poplib and email modules

B

brettk

Hello All,

Here's what I'm trying to do:

I need to connect to a pop3 server, download all messages, and copy all
of the attachments into a specific directory. The actual email message
is unimportant. Now, I've found plenty of examples that strip the
attachments from an email message, but most (if not all) of them take a
file parameter. My question is this:

How can i retrieve an email message via poplib and pass it to
email.message_from_string()?

This is essentially what i'm doing now,

##############
import email
import poplib

mimes = ["image/tif","image/tiff","images/x-tif","image/x-tiff",
"application/tif","application/tiff","application/x-tif",
"application/x-tiff"]

def WriteAttachment(msg):
docs = [(part.get_filename(),part.get_payload(decode=True))
for part in msg.get_payload() if part.get_type() in mimes]
for name,data in docs:
f = file(name,'wb')
f.write(data)
f.close()

ms = poplib.POP3(server)
ms.user(uname)
ms.pass_(passw)

msgcount = len(ms.list()[1])
for i in range(msgcount):
for j in ms.retr(i+1)[1]:
msg = email.message_from_string(j)
WriteAttachment(msg)

##################################

I'm sure i'm missing something pretty simple, i just need a slap in the
right direction...

TIA
 
T

Tim Williams

----- Original Message -----
From: said:
Here's what I'm trying to do:

I need to connect to a pop3 server, download all messages, and copy all
of the attachments into a specific directory. The actual email message

##############
import email
import poplib

mimes = ["image/tif","image/tiff","images/x-tif","image/x-tiff",
"application/tif","application/tiff","application/x-tif",
"application/x-tiff"]

def WriteAttachment(msg):
for part in msg.walk():
if part.get_type() in mimes:
name = part.get_filename()
data = part.get_payload(decode=True)
f = file(name,'wb')
f.write(data)
f.close()

ms = poplib.POP3(server)
ms.user(user)
ms.pass_(passw)

msgcount = len(ms.list()[1])
for i in range(msgcount):
response, msg_as_list, size = ms.retr(i+1)
msg = email.message_from_string('\r\n'.join(msg_as_list))
WriteAttachment(msg)

##################################
 
M

Mike Meyer

Hello All,

Here's what I'm trying to do:

I need to connect to a pop3 server, download all messages, and copy all
of the attachments into a specific directory. The actual email message
is unimportant. Now, I've found plenty of examples that strip the
attachments from an email message, but most (if not all) of them take a
file parameter. My question is this:

How can i retrieve an email message via poplib and pass it to
email.message_from_string()?

A quick look at the poplib documentation shows the retr method, which gets
a message by number, and returns a list: ['response', ['line', ...], octets].

So to get a string, you'd do:

from poplib import POP3
from sys import exit

p = POP3('example.com')
# authentication, etc.
msg_list = p.list()
if not msg_list.startswith('+OK'):
# Handle error in listings
exit(1)

for msg in msg_list[1]:
msg_num, _ = msg.split()
resp = p.retr(msg_num)
if resp.startswith('+OK'):
email.message_from_string('\n'.join(resp[1]))
else:
# Deal with error retrieving message.

This is untested code, but the details of dealing with poplib should
be right.

<mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top