Make sure binary MIME attachments will be base64 decoded

This commit is contained in:
Jonas Schnelli 2017-06-20 08:34:03 +02:00
parent ef0ba19a3b
commit a40bbdbecb
No known key found for this signature in database
GPG Key ID: 1EB776BB03C7922D

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