Merge pull request #526 from jonasschnelli/2017/07/base64_fix

Make sure binary MIME attachments will be base64 decoded
This commit is contained in:
Garret Wassermann 2017-06-25 20:44:30 -04:00 committed by GitHub
commit 0a0f0d3e5d

14
helpdesk/management/commands/get_email.py Normal file → Executable file
View File

@ -21,6 +21,8 @@ from os.path import isfile, join
import poplib
import re
import socket
import base64
import binascii
from time import ctime
from email_reply_parser import EmailReplyParser
@ -332,8 +334,16 @@ def ticket_from_message(message, queue, logger):
if not name:
ext = mimetypes.guess_extension(part.get_content_type())
name = "part-%i%s" % (counter, ext)
files.append(SimpleUploadedFile(name, encoding.smart_bytes(part.get_payload()), part.get_content_type()))
logger.debug("Found MIME attachment %s" % name)
payload = part.get_payload()
payloadToWrite = payload
try:
logger.debug("Try to base64 decode the attachment payload")
payloadToWrite = base64.decodestring(payload)
except binascii.Error:
logger.debug("Payload was not base64 encoded, using raw bytes")
payloadToWrite = payload
files.append(SimpleUploadedFile(name, encoding.smart_bytes(payloadToWrite), part.get_content_type()))
logger.info("Found MIME attachment %s" % name)
counter += 1