Reputation: 359
I am trying to access POP3 email server. I will be polling messages and downloading attachments for each one of them. I can successfully login and get the messages but cannot figure out how to actually get the attachment, which I would need to parse later. I'm thinking I could save to tmp dir until I process it.
Here's what I got so far:
pop = poplib.POP3_SSL(server)
pop.user(usr)
pop.pass_(pwd)
f = open(file_dir, 'w')
num_msgs = len(pop.list()[1])
for msg_list in range(num_msgs):
for msg in pop.retr(msg_list+1)[1]:
mail = email.message_from_string(msg)
for part in mail.walk():
f.write(part.get_payload(decode=True))
f.close()
This is code I pieced together from the examples I found online but no solid example of actually getting the attachment. The file I'm writing to is empty. What am I missing here?
Upvotes: 9
Views: 8736
Reputation: 81
Please see a complete example below.
Import poplib and parser
import poplib from email import parser
A function to return a connection to the pop server:
def mail_connection(server='pop.mymailserver.com'):
pop_conn = poplib.POP3(server)
pop_conn.user('someuser@server')
pop_conn.pass_('password')
return pop_conn
A function to fetch the mail:
def fetch_mail(delete_after=False):
pop_conn = mail_connection()
messages = [pop_conn.retr(i) for i in range(1, len(pop_conn.list()[1]) + 1)]
messages = ["\n".join(mssg[1]) for mssg in messages]
messages = [parser.Parser().parsestr(mssg) for mssg in messages]
if delete_after == True:
delete_messages = [pop_conn.dele(i) for i in range(1, len(pop_conn.list()[1]) + 1)]
pop_conn.quit()
return messages
Then a function to save the attachments as files. NB, the allowed mimetypes
; you could have a list of them, such as:
allowed_mimetypes = ["application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"]
and so on.
def get_attachments():
messages = fetch_mail()
attachments = []
for msg in messages:
for part in msg.walk():
if part.get_content_type() in allowed_mimetypes:
name = part.get_filename()
data = part.get_payload(decode=True)
f = open(name,'wb')
f.write(data)
f.close()
attachments.append(name)
return attachments
Upvotes: 6
Reputation: 2039
I know this is an old question, but just in case: The value you're passing to email.message_from_string is actually a list of the contents of the email, where each element is a line. You need to join it up to get a string representation of that email:
mail = email.message_from_string("".join(msg))
Upvotes: 3