Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python poplib get attachment

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?

like image 401
t0x13 Avatar asked Jun 30 '11 21:06

t0x13


People also ask

How do you check if an email has an attachment in Python?

In general, you can try analyzing content-type header. If it's multipart/mixed, most likely the message contains an attachment.

How do I read email attachments in python?

imaplib is the package that installs IMAP a standard email protocol that stores email messages on a mail server, but allows the end user to view and manipulate the messages as though they were stored locally on the end user's computing device(s).

How do I download an email attachment in Python?

Create an empty python file download_attachment.py. Add the following lines to it. print 'Proceeding' import email import getpass import imaplib import os import sys userName = '[email protected]' passwd = 'yourpassword' directory = '/full/path/to/the/directory' detach_dir = '.

What is Poplib?

Source code: Lib/poplib.py. This module defines a class, POP3 , which encapsulates a connection to a POP3 server and implements the protocol as defined in RFC 1939. The POP3 class supports both the minimal and optional command sets from RFC 1939.


1 Answers

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
like image 60
torrange Avatar answered Sep 23 '22 01:09

torrange