Skip to content

Commit 41ad43d

Browse files
tuck1sjgzamora
authored andcommitted
Handle email_rfc822 attribute (#181)
1 parent c55a461 commit 41ad43d

File tree

2 files changed

+58
-21
lines changed

2 files changed

+58
-21
lines changed

sparkpost/transmissions.py

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,31 @@ def _translate_keys(self, **kwargs):
4545
model['options']['ip_pool'] = kwargs.get('ip_pool')
4646
model['options']['inline_css'] = kwargs.get('inline_css')
4747

48-
model['content']['use_draft_template'] = \
49-
kwargs.get('use_draft_template', False)
50-
model['content']['reply_to'] = kwargs.get('reply_to')
51-
model['content']['subject'] = kwargs.get('subject')
52-
from_email = kwargs.get('from_email')
53-
if isinstance(from_email, string_types):
54-
from_email = self._parse_address(from_email)
55-
model['content']['from'] = from_email
56-
model['content']['html'] = kwargs.get('html')
57-
model['content']['text'] = kwargs.get('text')
58-
model['content']['template_id'] = kwargs.get('template')
59-
model['content']['headers'] = kwargs.get('custom_headers', {})
48+
rfc822 = kwargs.get('email_rfc822')
49+
if rfc822:
50+
model['content']['email_rfc822'] = rfc822
51+
else:
52+
model['content']['headers'] = kwargs.get('custom_headers', {})
53+
model['content']['use_draft_template'] = \
54+
kwargs.get('use_draft_template', False)
55+
model['content']['reply_to'] = kwargs.get('reply_to')
56+
model['content']['subject'] = kwargs.get('subject')
57+
from_email = kwargs.get('from_email')
58+
if isinstance(from_email, string_types):
59+
from_email = self._parse_address(from_email)
60+
model['content']['from'] = from_email
61+
model['content']['html'] = kwargs.get('html')
62+
model['content']['text'] = kwargs.get('text')
63+
model['content']['template_id'] = kwargs.get('template')
64+
65+
attachments = kwargs.get('attachments', [])
66+
model['content']['attachments'] = self._extract_attachments(
67+
attachments)
68+
69+
if 'inline_images' in kwargs:
70+
inline_images = kwargs['inline_images']
71+
model['content']['inline_images'] = self._extract_attachments(
72+
inline_images)
6073

6174
recipient_list = kwargs.get('recipient_list')
6275
if recipient_list:
@@ -77,15 +90,6 @@ def _translate_keys(self, **kwargs):
7790

7891
model['recipients'] = recipients
7992

80-
attachments = kwargs.get('attachments', [])
81-
model['content']['attachments'] = self._extract_attachments(
82-
attachments)
83-
84-
if 'inline_images' in kwargs:
85-
inline_images = kwargs['inline_images']
86-
model['content']['inline_images'] = self._extract_attachments(
87-
inline_images)
88-
8993
return model
9094

9195
def _format_copies(self, recipients, copies):

test/test_transmissions.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,39 @@ def test_translate_keys_with_inline_css():
150150
assert results['options'].get('inline_css') is True
151151

152152

153+
def test_translate_keys_with_email_rfc822():
154+
t = Transmissions('uri', 'key')
155+
156+
# Build data using implicit cat, as max line length is enforced
157+
eml = (
158+
'To: wilma <[email protected]>\n',
159+
'From: fred <[email protected]>\n',
160+
'Subject: Bedrock declaration\n',
161+
'MIME-Version: 1.0\n',
162+
'Content-Type: text/plain; charset=utf-8; format=flowed\n',
163+
'Content-Transfer-Encoding: 7bit\nContent-Language: en-GB\n',
164+
'\n',
165+
'When in the Course of human events we yell yabba dabba doo.',
166+
)
167+
168+
# Just a selection. Don't test attribs with irregular naming
169+
non_rfc822_attrs = {
170+
'headers': 'foo',
171+
'reply_to': 'foo',
172+
'subject': 'foo',
173+
'html': 'foo',
174+
'text': 'foo',
175+
}
176+
177+
# Demonstrate that email_rfc822 overrides other content attributes
178+
test_content = {'email_rfc822': eml}
179+
test_content.update(non_rfc822_attrs)
180+
results = t._translate_keys(**test_content)
181+
for i in non_rfc822_attrs:
182+
assert results['content'].get(i) is None
183+
assert results['content'].get('email_rfc822') is not None
184+
185+
153186
@responses.activate
154187
def test_success_send():
155188
responses.add(

0 commit comments

Comments
 (0)