Skip to content

Commit c4a194d

Browse files
committed
Merge pull request #91 from pegler/master
fix Django backend to correctly base64 encode attachments
2 parents 95c7679 + d9945e0 commit c4a194d

File tree

3 files changed

+67
-17
lines changed

3 files changed

+67
-17
lines changed

sparkpost/django/message.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
from base64 import b64encode
2+
13
from django.core.mail import EmailMultiAlternatives
4+
from django.conf import settings
25

36
from .exceptions import UnsupportedContent
47

@@ -46,11 +49,19 @@ def __init__(self, message):
4649

4750
if message.attachments:
4851
formatted['attachments'] = []
52+
str_encoding = settings.DEFAULT_CHARSET
4953
for attachment in message.attachments:
5054
filename, content, mimetype = attachment
55+
try:
56+
if isinstance(content, unicode):
57+
content = content.encode(str_encoding)
58+
except NameError:
59+
if isinstance(content, str):
60+
content = content.encode(str_encoding)
61+
base64_encoded_content = b64encode(content)
5162
formatted['attachments'].append({
5263
'name': filename,
53-
'data': content,
64+
'data': base64_encoded_content.decode('ascii'),
5465
'type': mimetype
5566
})
5667

sparkpost/transmissions.py

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
from .base import Resource
66

77

8+
try:
9+
string_types = basestring
10+
except NameError:
11+
string_types = str # Python 3 doesn't have basestring
12+
13+
814
class Transmissions(Resource):
915
"""
1016
Transmission class used to send, list and get transmissions. For detailed
@@ -40,7 +46,10 @@ def _translate_keys(self, **kwargs):
4046
kwargs.get('use_draft_template', False)
4147
model['content']['reply_to'] = kwargs.get('reply_to')
4248
model['content']['subject'] = kwargs.get('subject')
43-
model['content']['from'] = kwargs.get('from_email')
49+
from_email = kwargs.get('from_email')
50+
if isinstance(from_email, string_types):
51+
from_email = self._parse_address(from_email)
52+
model['content']['from'] = from_email
4453
model['content']['html'] = kwargs.get('html')
4554
model['content']['text'] = kwargs.get('text')
4655
model['content']['template_id'] = kwargs.get('template')
@@ -97,24 +106,22 @@ def _get_base64_from_file(self, filename):
97106
encoded_string = base64.b64encode(a_file.read()).decode("ascii")
98107
return encoded_string
99108

109+
def _parse_address(self, address):
110+
name, email = parseaddr(address)
111+
parsed_address = {
112+
'email': email
113+
}
114+
if name:
115+
parsed_address['name'] = name
116+
return parsed_address
117+
100118
def _extract_recipients(self, recipients):
101119
formatted_recipients = []
102120
for recip in recipients:
103-
try:
104-
string_types = basestring
105-
except NameError:
106-
string_types = str # Python 3 doesn't have basestring
107121
if isinstance(recip, string_types):
108-
name, email = parseaddr(recip)
109-
formatted_recip = {
110-
'address': {
111-
'name': name,
112-
'email': email
113-
}
114-
}
115-
if not name:
116-
del formatted_recip['address']['name']
117-
formatted_recipients.append(formatted_recip)
122+
formatted_recipients.append({
123+
'address': self._parse_address(recip)
124+
})
118125
else:
119126
formatted_recipients.append(recip)
120127
return formatted_recipients

test/django/test_message.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
1+
from django.conf import settings
12
from django.core.mail import EmailMultiAlternatives
23
from django.core.mail.message import EmailMessage
4+
from django.utils.functional import empty
35

46
from sparkpost.django.message import SparkPostMessage
57
from .utils import at_least_version
68

79

10+
def reconfigure_settings(**new_settings):
11+
old_settings = settings._wrapped
12+
13+
settings._wrapped = empty
14+
settings.configure(default_settings=old_settings, **new_settings)
15+
16+
17+
reconfigure_settings(
18+
DEFAULT_CHARSET='utf-8'
19+
)
20+
21+
822
base_options = dict(
923
subject='Test',
1024
body='Testing',
@@ -67,7 +81,25 @@ def test_attachment():
6781
attachments=[
6882
{
6983
'name': 'file.txt',
70-
'data': 'test content',
84+
'data': 'dGVzdCBjb250ZW50',
85+
'type': 'text/plain'
86+
}
87+
]
88+
)
89+
expected.update(base_expected)
90+
assert actual == expected
91+
92+
93+
def test_attachment_unicode():
94+
email_message = EmailMessage(**base_options)
95+
email_message.attach('file.txt', u'test content', 'text/plain')
96+
97+
actual = SparkPostMessage(email_message)
98+
expected = dict(
99+
attachments=[
100+
{
101+
'name': 'file.txt',
102+
'data': 'dGVzdCBjb250ZW50',
71103
'type': 'text/plain'
72104
}
73105
]

0 commit comments

Comments
 (0)