From 8b870ca5528e4b0104aea9bb77804c81451d6381 Mon Sep 17 00:00:00 2001 From: tuck1s Date: Thu, 4 Oct 2018 16:54:25 +0100 Subject: [PATCH 1/2] Handle email_rfc822 attribute --- sparkpost/transmissions.py | 46 +++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/sparkpost/transmissions.py b/sparkpost/transmissions.py index 39d20fc..9a28555 100644 --- a/sparkpost/transmissions.py +++ b/sparkpost/transmissions.py @@ -45,18 +45,31 @@ def _translate_keys(self, **kwargs): model['options']['ip_pool'] = kwargs.get('ip_pool') model['options']['inline_css'] = kwargs.get('inline_css') - model['content']['use_draft_template'] = \ - kwargs.get('use_draft_template', False) - model['content']['reply_to'] = kwargs.get('reply_to') - model['content']['subject'] = kwargs.get('subject') - from_email = kwargs.get('from_email') - if isinstance(from_email, string_types): - from_email = self._parse_address(from_email) - model['content']['from'] = from_email - model['content']['html'] = kwargs.get('html') - model['content']['text'] = kwargs.get('text') - model['content']['template_id'] = kwargs.get('template') - model['content']['headers'] = kwargs.get('custom_headers', {}) + rfc822 = kwargs.get('email_rfc822') + if rfc822: + model['content']['email_rfc822'] = rfc822 + else: + model['content']['headers'] = kwargs.get('custom_headers', {}) + model['content']['use_draft_template'] = \ + kwargs.get('use_draft_template', False) + model['content']['reply_to'] = kwargs.get('reply_to') + model['content']['subject'] = kwargs.get('subject') + from_email = kwargs.get('from_email') + if isinstance(from_email, string_types): + from_email = self._parse_address(from_email) + model['content']['from'] = from_email + model['content']['html'] = kwargs.get('html') + model['content']['text'] = kwargs.get('text') + model['content']['template_id'] = kwargs.get('template') + + attachments = kwargs.get('attachments', []) + model['content']['attachments'] = self._extract_attachments( + attachments) + + if 'inline_images' in kwargs: + inline_images = kwargs['inline_images'] + model['content']['inline_images'] = self._extract_attachments( + inline_images) recipient_list = kwargs.get('recipient_list') if recipient_list: @@ -77,15 +90,6 @@ def _translate_keys(self, **kwargs): model['recipients'] = recipients - attachments = kwargs.get('attachments', []) - model['content']['attachments'] = self._extract_attachments( - attachments) - - if 'inline_images' in kwargs: - inline_images = kwargs['inline_images'] - model['content']['inline_images'] = self._extract_attachments( - inline_images) - return model def _format_copies(self, recipients, copies): From 2680178e6753cfb2c852ce17588b18b2ddc0dc22 Mon Sep 17 00:00:00 2001 From: tuck1s Date: Fri, 2 Nov 2018 20:38:39 +0000 Subject: [PATCH 2/2] Added tests for email_rfc822. --- test/test_transmissions.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/test/test_transmissions.py b/test/test_transmissions.py index 39f511b..ebe363d 100644 --- a/test/test_transmissions.py +++ b/test/test_transmissions.py @@ -150,6 +150,39 @@ def test_translate_keys_with_inline_css(): assert results['options'].get('inline_css') is True +def test_translate_keys_with_email_rfc822(): + t = Transmissions('uri', 'key') + + # Build data using implicit cat, as max line length is enforced + eml = ( + 'To: wilma \n', + 'From: fred \n', + 'Subject: Bedrock declaration\n', + 'MIME-Version: 1.0\n', + 'Content-Type: text/plain; charset=utf-8; format=flowed\n', + 'Content-Transfer-Encoding: 7bit\nContent-Language: en-GB\n', + '\n', + 'When in the Course of human events we yell yabba dabba doo.', + ) + + # Just a selection. Don't test attribs with irregular naming + non_rfc822_attrs = { + 'headers': 'foo', + 'reply_to': 'foo', + 'subject': 'foo', + 'html': 'foo', + 'text': 'foo', + } + + # Demonstrate that email_rfc822 overrides other content attributes + test_content = {'email_rfc822': eml} + test_content.update(non_rfc822_attrs) + results = t._translate_keys(**test_content) + for i in non_rfc822_attrs: + assert results['content'].get(i) is None + assert results['content'].get('email_rfc822') is not None + + @responses.activate def test_success_send(): responses.add(