how to retrieve attachment in a mail ?

John Roth newsgroups at jhrothjr.com
Wed Oct 29 07:34:17 EST 2003


"Didier FRAISSE" <dfraisse at free.fr> wrote in message
news:3f9f7b15$0$235$636a55ce at news.free.fr...
> i need to retrieve every day an email and to save his attachments for
later
> processing.
> i found some piece of code in the documentation like
>
>     import poplib
>     pop = poplib.POP3(Host)
>     pop.user(User)
>     pop.pass(Pwd)
>     nbMsg, nbOctet = pop.stat()
>     for n in range(nbMsg):
>             response, lines, bytes = pop.retr(n+1)
>
> which retrieve email from a pop3 account and
>
> import email.Parser
> import os
> import sys
>
> mailFile=open(File,"rb")
> p=email.Parser.Parser()
> msg=p.parse(mailFile)
> mailFile.close()
>
> partCounter=1
> for part in msg.walk():
>     if part.get_main_type()=="multipart":
>          continue
>     name=part.get_param("name")
>     if name==None:
>       name="part-%i" % partCounter
>     partCounter+=1
>     # In real life, make sure that name is a reasonable
>     # filename on your OS.
>     f=open(name,"wb")
>     f.write(part.get_payload(decode=1))
>     f.close()
>     print name
> wich extract every part of a mailFile
>
> BUT i don't understand how to migrate from
>             response, lines, bytes = pop.retr(n+1)
> where lines is a list to a msg object
>         for part in msg.walk():
>
> thanks to help me and many pologize for my poor english
> Didier

I found that the last example in the documentation for the email
module (12.2.13 in the Python 2.2.3 Library doc) did exactly
what I wanted. I think I've got slightly different requirements:
I keep the antivirus settings on Outlook Express set high enough
that I can't save attachements directly, so I save the entire
e-mail to a processing directory, and then use the code in the
example to unpack it. After it's unpacked, I do whatever I need to
with it.

This is the code; I don't remember if I modified the example
or not...

-------------------------------------------------------------

#!/usr/bin/env python

"""Unpack a MIME message into a directory of files.

Usage: unpackmail [options] msgfile

Options:
    -h / --help
        Print this message and exit.

    -d directory
    --directory=directory
        Unpack the MIME message into the named directory, which will be
        created if it doesn't already exist.

msgfile is the path to the file containing the MIME message.
"""

import sys
import os
import getopt
import errno
import mimetypes
import email


def usage(code, msg=''):
    print >> sys.stderr, __doc__
    if msg:
        print >> sys.stderr, msg
    sys.exit(code)


def main():
    try:
        opts, args = getopt.getopt(sys.argv[1:], 'hd:', ['help',
'directory='])
    except getopt.error, msg:
        usage(1, msg)

    dir = os.curdir
    for opt, arg in opts:
        if opt in ('-h', '--help'):
            usage(0)
        elif opt in ('-d', '--directory'):
            dir = arg

    try:
        msgfile = args[0]
    except IndexError:
        usage(1)

    try:
        os.mkdir(dir)
    except OSError, e:
        # Ignore directory exists error
        if e.errno <> errno.EEXIST: raise

    fp = open(msgfile)
    msg = email.message_from_file(fp)
    fp.close()

    counter = 1
    for part in msg.walk():
        # multipart/* are just containers
        if part.get_content_maintype() == 'multipart':
            continue
        # Applications should really sanitize the given filename so that an
        # email message can't be used to overwrite important files
        filename = part.get_filename()
        if not filename:
            ext = mimetypes.guess_extension(part.get_type())
            if not ext:
                # Use a generic bag-of-bits extension
                ext = '.bin'
            filename = 'part-%03d%s' % (counter, ext)
        counter += 1
        fp = open(os.path.join(dir, filename), 'wb')
        fp.write(part.get_payload(decode=1))
        fp.close()


if __name__ == '__main__':
    main()

-------------------------------------------------------------

I also use the following command to process each email
I want to unpack:

----------------------------

python unpackmail.py -d outdir %1

---------------------------------

There are obviously lots of ways this could be improved, but
it suits my needs.

John Roth
>
>
>






More information about the Python-list mailing list