extractAttachments.py

#! /usr/bin/python
 
""" Extract attachments from emails and store them as files.
    Command-line argument: target directory
    Stdin: email text (e.g., through procmail)
 
    This script performs no error checking and assumes that anything with
    a filename will be base64-encoded. This is really only meant to collect
    data that is emailed automatically. Test well and use at your own risk.
"""
 
import sys
import email.parser
import base64
 
BASEDIR = sys.argv[1] + '/'
 
parser = email.parser.Parser()
mail = parser.parse(sys.stdin)
if mail.is_multipart():
	for m in mail.get_payload():
		if (m.get_filename() != None):
			file = open(BASEDIR + m.get_filename(), 'w')
			file.write(base64.b64decode(m.get_payload()))
			file.close()
---