From 1e44392b13bc79694a1f4f6db5ddde0e3cc40adf Mon Sep 17 00:00:00 2001 From: medmunds Date: Sat, 25 Jan 2014 11:58:12 -0800 Subject: [PATCH] Allow all extra message headers in send. Mandrill has relaxed previous API restrictions on headers. Fixes #58. --- djrill/mail/backends/djrill.py | 8 +------- djrill/tests/test_mandrill_send.py | 25 ++++++------------------- docs/history.rst | 3 +++ docs/usage/sending_mail.rst | 9 +++++---- 4 files changed, 15 insertions(+), 30 deletions(-) diff --git a/djrill/mail/backends/djrill.py b/djrill/mail/backends/djrill.py index 0be5e15..52266cc 100644 --- a/djrill/mail/backends/djrill.py +++ b/djrill/mail/backends/djrill.py @@ -130,8 +130,7 @@ class DjrillBackend(BaseEmailBackend): still work through Mandrill. Raises NotSupportedByMandrillError for any standard EmailMessage - features that cannot be accurately communicated to Mandrill - (e.g., prohibited headers). + features that cannot be accurately communicated to Mandrill. """ sender = sanitize_address(message.from_email, message.encoding) from_name, from_email = parseaddr(sender) @@ -162,11 +161,6 @@ class DjrillBackend(BaseEmailBackend): % len(message.bcc)) if message.extra_headers: - for k in message.extra_headers.keys(): - if k != "Reply-To" and not k.startswith("X-"): - raise NotSupportedByMandrillError( - "Invalid message header '%s' - Mandrill " - "only allows Reply-To and X-* headers" % k) msg_dict["headers"] = message.extra_headers return msg_dict diff --git a/djrill/tests/test_mandrill_send.py b/djrill/tests/test_mandrill_send.py index cf0ff9f..453aabf 100644 --- a/djrill/tests/test_mandrill_send.py +++ b/djrill/tests/test_mandrill_send.py @@ -86,7 +86,8 @@ class DjrillBackendTests(DjrillBackendMockAPITestCase): bcc=['bcc@example.com'], cc=['cc1@example.com', 'Also CC '], headers={'Reply-To': 'another@example.com', - 'X-MyHeader': 'my value'}) + 'X-MyHeader': 'my value', + 'Message-ID': 'mycustommsgid@example.com'}) email.send() self.assert_mandrill_called("/messages/send.json") data = self.get_api_call_data() @@ -94,7 +95,9 @@ class DjrillBackendTests(DjrillBackendMockAPITestCase): self.assertEqual(data['message']['text'], "Body goes here") self.assertEqual(data['message']['from_email'], "from@example.com") self.assertEqual(data['message']['headers'], - { 'Reply-To': 'another@example.com', 'X-MyHeader': 'my value' }) + {'Reply-To': 'another@example.com', + 'X-MyHeader': 'my value', + 'Message-ID': 'mycustommsgid@example.com'}) # Mandrill doesn't have a notion of cc. # Djrill just treats cc as additional "to" addresses, # which may or may not be what you want. @@ -217,22 +220,6 @@ class DjrillBackendTests(DjrillBackendMockAPITestCase): # Make sure the image attachments are not treated as embedded: self.assertFalse('images' in data['message']) - def test_extra_header_errors(self): - email = mail.EmailMessage('Subject', 'Body', 'from@example.com', - ['to@example.com'], - headers={'Non-X-Non-Reply-To-Header': 'not permitted'}) - with self.assertRaises(NotSupportedByMandrillError): - email.send() - - # Make sure fail_silently is respected - email = mail.EmailMessage('Subject', 'Body', 'from@example.com', - ['to@example.com'], - headers={'Non-X-Non-Reply-To-Header': 'not permitted'}) - sent = email.send(fail_silently=True) - self.assertFalse(self.mock_post.called, - msg="Mandrill API should not be called when send fails silently") - self.assertEqual(sent, 0) - def test_alternative_errors(self): # Multiple alternatives not allowed email = mail.EmailMultiAlternatives('Subject', 'Body', @@ -474,4 +461,4 @@ class DjrillMandrillFeatureTests(DjrillBackendMockAPITestCase): msg = mail.EmailMessage('Subject', 'Message', 'from@example.com', ['to1@example.com'],) sent = msg.send(fail_silently=True) self.assertEqual(sent, 0) - self.assertIsNone(msg.mandrill_response) \ No newline at end of file + self.assertIsNone(msg.mandrill_response) diff --git a/docs/history.rst b/docs/history.rst index e45fdb3..ab485d2 100644 --- a/docs/history.rst +++ b/docs/history.rst @@ -3,6 +3,9 @@ Release Notes Version 0.9 (development): +* Allow all extra message headers in send. + (Mandrill has relaxed previous API restrictions on headers.) + Version 0.8: diff --git a/docs/usage/sending_mail.rst b/docs/usage/sending_mail.rst index 536bba6..b4e7b90 100644 --- a/docs/usage/sending_mail.rst +++ b/docs/usage/sending_mail.rst @@ -89,10 +89,11 @@ Some notes and limitations: Special handling for embedded images **Headers** - Djrill accepts additional headers, but only ``Reply-To`` and - ``X-*`` (since that is all that Mandrill accepts). Any other extra headers - will raise :exc:`~djrill.NotSupportedByMandrillError` when you attempt to send the - message. + Djrill accepts additional headers and passes them along to Mandrill. + + .. versionchanged:: 0.9 + In earlier versions, Djrill only allowed ``Reply-To`` and ``X-*`` headers, + matching previous Mandrill API restrictions. .. _mandrill-send-support: