add a work-around the bug in encoding of attachement filename by FileMakerPro

This commit is contained in:
Benjamin Dauvergne 2012-05-10 11:16:42 +02:00
parent 5d8ea1861f
commit ca57f02ee5
1 changed files with 13 additions and 1 deletions

View File

@ -3,6 +3,7 @@ import sys
import email
import email.errors
import email.utils
import email.header
import logging
from django.core.management.base import BaseCommand, CommandError
@ -63,6 +64,17 @@ In case of failure the following return value is returned:
if exit_code:
sys.exit(exit_code)
def decode_filename(self, filename):
'''See if the filename contains encoded-word work around bugs in FileMakerPro'''
m = re.match(r'=\?(.*)\?(.*)\?(.*)\?=', s)
if m:
result = []
for content, encoding in email.header.decode_header(filename):
result.append(unicode(content, encoding or 'ascii'))
return ''.join(result)
else:
return filename
def handle_mail(self, mail, mail_sender, mail_recipients, **options):
content_errors = []
attachments = []
@ -90,7 +102,7 @@ In case of failure the following return value is returned:
except models.FileType.DoesNotExist:
content_errors.append('The subject "%s" does not match any known file type' % subject)
for part in mail.walk():
filename = part.get_filename(None)
filename = self.decode_filename(part.get_filename(None))
if part.get_content_type() == 'text/plain' and \
('Content-Disposition' not in part or 'inline' in part['Content-Disposition']):
charset = part.get_content_charset('us-ascii')