By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,100 Members | 2,846 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,100 IT Pros & Developers. It's quick & easy.

Getting/Saving email attachments w/ poplib and email modules

P: n/a
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

Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

----- Original Message -----
From: <br****@gmail.com>

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)

##################################
Jul 19 '05 #2

P: n/a
br****@gmail.com writes:
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
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.