Skip to content

Commit 63efb64

Browse files
author
Marko Mrdjenovic
committed
Merge branch 'master' of https://github.com/sparkpost/python-sparkpost into tornado
2 parents 5ee8845 + c4a194d commit 63efb64

File tree

4 files changed

+75
-17
lines changed

4 files changed

+75
-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: 24 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
@@ -34,12 +40,16 @@ def _translate_keys(self, **kwargs):
3440
model['options']['transactional'] = kwargs.get('transactional')
3541
model['options']['sandbox'] = kwargs.get('use_sandbox')
3642
model['options']['skip_suppression'] = kwargs.get('skip_suppression')
43+
model['options']['inline_css'] = kwargs.get('inline_css')
3744

3845
model['content']['use_draft_template'] = \
3946
kwargs.get('use_draft_template', False)
4047
model['content']['reply_to'] = kwargs.get('reply_to')
4148
model['content']['subject'] = kwargs.get('subject')
42-
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
4353
model['content']['html'] = kwargs.get('html')
4454
model['content']['text'] = kwargs.get('text')
4555
model['content']['template_id'] = kwargs.get('template')
@@ -96,24 +106,22 @@ def _get_base64_from_file(self, filename):
96106
encoded_string = base64.b64encode(a_file.read()).decode("ascii")
97107
return encoded_string
98108

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+
99118
def _extract_recipients(self, recipients):
100119
formatted_recipients = []
101120
for recip in recipients:
102-
try:
103-
string_types = basestring
104-
except NameError:
105-
string_types = str # Python 3 doesn't have basestring
106121
if isinstance(recip, string_types):
107-
name, email = parseaddr(recip)
108-
formatted_recip = {
109-
'address': {
110-
'name': name,
111-
'email': email
112-
}
113-
}
114-
if not name:
115-
del formatted_recip['address']['name']
116-
formatted_recipients.append(formatted_recip)
122+
formatted_recipients.append({
123+
'address': self._parse_address(recip)
124+
})
117125
else:
118126
formatted_recipients.append(recip)
119127
return formatted_recipients
@@ -184,6 +192,7 @@ def send(self, **kwargs):
184192
:param bool skip_suppression: Whether or not to ignore customer
185193
suppression rules, for this transmission only. Only applicable if
186194
your configuration supports this parameter. (SparkPost Elite only)
195+
:param bool inline_css: Whether or not to perform CSS inlining
187196
:param dict custom_headers: Used to set any headers associated with
188197
transmission
189198

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
]

test/test_transmissions.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ def test_translate_keys_with_bcc():
9090
]
9191

9292

93+
def test_translate_keys_with_inline_css():
94+
t = Transmissions('uri', 'key')
95+
results = t._translate_keys(inline_css=True)
96+
assert results['options'].get('inline_css') is True
97+
98+
9399
@responses.activate
94100
def test_success_send():
95101
responses.add(

0 commit comments

Comments
 (0)