Mercurial > hg > emaildispatcher
view emaildispatcher/handlers.py @ 1:2c76525ce80b
demo works: can be used to extract images to a directory
author | k0s <k0scist@gmail.com> |
---|---|
date | Thu, 31 Dec 2009 00:26:33 -0500 |
parents | bbcc528508f1 |
children |
line wrap: on
line source
from datetime import datetime import base64 import os timestamp = '%Y%m%d%H%M%s' class Images(object): "extract images from an email message" def __init__(self, directory): self.directory = directory def __call__(self, message): payload = message.get_payload() if isinstance(payload, basestring): if message.get_content_maintype() == 'image': if message.get('Content-Transfer-Encoding') == 'base64': payload = base64.b64decode(payload) filename = message.get_filename() if filename is None: filename = datetime.now().strftime(timestamp) if os.path.exists(os.path.join(self.directory, filename)): datestamp = datetime.now().strftime(timestamp) if '.' in filename: base, ext = filename.rsplit('.', 1) filename = '%s.%s.%s' % (base.rstrip('.'), datestamp, ext) else: filename = '%s.%s' % (filename, datestamp) f = file(os.path.join(self.directory, filename), 'wb') f.write(payload) f.close() else: # assume a list of Messages for part in payload: self(part)