Skip to content

Commit 1f64b83

Browse files
committed
Decode base64 attachments before attaching their contents to email object
1 parent e1448ac commit 1f64b83

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

django_inbound_email/backends/mandrill.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
22
import logging
3+
import base64
34

45
from django.core.mail import EmailMultiAlternatives
56
from django.http import HttpRequest
@@ -20,9 +21,12 @@ class MandrillRequestParser(RequestParser):
2021

2122
def _process_attachments(self, email, attachments):
2223
for key, attachment in attachments.iteritems():
24+
is_base64 = attachment.get('base64')
2325
name = attachment.get('name')
2426
mimetype = attachment.get('type')
2527
content = attachment.get('content', u"")
28+
if is_base64:
29+
content = base64.b64decode(content)
2630
content = smart_bytes(content, strings_only=True)
2731

2832
if len(content) > self.max_file_size:

test_app/tests.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# -*- coding: utf-8 -*-
22
from os import path
33
import json
4+
import base64
45

56
from django.conf import settings
67
from django.core.mail import EmailMultiAlternatives
@@ -550,7 +551,13 @@ def _parse_from(name, email):
550551
if has_html:
551552
self.assertEqual(e.alternatives[0][0], msg['html'])
552553
for name, contents, mimetype in e.attachments:
553-
self.assertEqual(msg['attachments'][name]['content'], contents)
554+
# Check that base64 contents are decoded
555+
is_base64 = msg['attachments'][name].get('base64')
556+
req_contents = msg['attachments'][name]['content']
557+
if is_base64:
558+
req_contents = base64.b64decode(req_contents)
559+
560+
self.assertEqual(req_contents, contents)
554561
self.assertEqual(msg['attachments'][name]['type'], mimetype)
555562
self.assertEqual(e.body, msg['text'])
556563

0 commit comments

Comments
 (0)