mirror of
https://github.com/pacnpal/django-anymail.git
synced 2025-12-20 11:51:05 -05:00
Allow all extra message headers in send.
Mandrill has relaxed previous API restrictions on headers. Fixes #58.
This commit is contained in:
@@ -130,8 +130,7 @@ class DjrillBackend(BaseEmailBackend):
|
|||||||
still work through Mandrill.
|
still work through Mandrill.
|
||||||
|
|
||||||
Raises NotSupportedByMandrillError for any standard EmailMessage
|
Raises NotSupportedByMandrillError for any standard EmailMessage
|
||||||
features that cannot be accurately communicated to Mandrill
|
features that cannot be accurately communicated to Mandrill.
|
||||||
(e.g., prohibited headers).
|
|
||||||
"""
|
"""
|
||||||
sender = sanitize_address(message.from_email, message.encoding)
|
sender = sanitize_address(message.from_email, message.encoding)
|
||||||
from_name, from_email = parseaddr(sender)
|
from_name, from_email = parseaddr(sender)
|
||||||
@@ -162,11 +161,6 @@ class DjrillBackend(BaseEmailBackend):
|
|||||||
% len(message.bcc))
|
% len(message.bcc))
|
||||||
|
|
||||||
if message.extra_headers:
|
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
|
msg_dict["headers"] = message.extra_headers
|
||||||
|
|
||||||
return msg_dict
|
return msg_dict
|
||||||
|
|||||||
@@ -86,7 +86,8 @@ class DjrillBackendTests(DjrillBackendMockAPITestCase):
|
|||||||
bcc=['bcc@example.com'],
|
bcc=['bcc@example.com'],
|
||||||
cc=['cc1@example.com', 'Also CC <cc2@example.com>'],
|
cc=['cc1@example.com', 'Also CC <cc2@example.com>'],
|
||||||
headers={'Reply-To': 'another@example.com',
|
headers={'Reply-To': 'another@example.com',
|
||||||
'X-MyHeader': 'my value'})
|
'X-MyHeader': 'my value',
|
||||||
|
'Message-ID': 'mycustommsgid@example.com'})
|
||||||
email.send()
|
email.send()
|
||||||
self.assert_mandrill_called("/messages/send.json")
|
self.assert_mandrill_called("/messages/send.json")
|
||||||
data = self.get_api_call_data()
|
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']['text'], "Body goes here")
|
||||||
self.assertEqual(data['message']['from_email'], "from@example.com")
|
self.assertEqual(data['message']['from_email'], "from@example.com")
|
||||||
self.assertEqual(data['message']['headers'],
|
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.
|
# Mandrill doesn't have a notion of cc.
|
||||||
# Djrill just treats cc as additional "to" addresses,
|
# Djrill just treats cc as additional "to" addresses,
|
||||||
# which may or may not be what you want.
|
# 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:
|
# Make sure the image attachments are not treated as embedded:
|
||||||
self.assertFalse('images' in data['message'])
|
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):
|
def test_alternative_errors(self):
|
||||||
# Multiple alternatives not allowed
|
# Multiple alternatives not allowed
|
||||||
email = mail.EmailMultiAlternatives('Subject', 'Body',
|
email = mail.EmailMultiAlternatives('Subject', 'Body',
|
||||||
@@ -474,4 +461,4 @@ class DjrillMandrillFeatureTests(DjrillBackendMockAPITestCase):
|
|||||||
msg = mail.EmailMessage('Subject', 'Message', 'from@example.com', ['to1@example.com'],)
|
msg = mail.EmailMessage('Subject', 'Message', 'from@example.com', ['to1@example.com'],)
|
||||||
sent = msg.send(fail_silently=True)
|
sent = msg.send(fail_silently=True)
|
||||||
self.assertEqual(sent, 0)
|
self.assertEqual(sent, 0)
|
||||||
self.assertIsNone(msg.mandrill_response)
|
self.assertIsNone(msg.mandrill_response)
|
||||||
|
|||||||
@@ -3,6 +3,9 @@ Release Notes
|
|||||||
|
|
||||||
Version 0.9 (development):
|
Version 0.9 (development):
|
||||||
|
|
||||||
|
* Allow all extra message headers in send.
|
||||||
|
(Mandrill has relaxed previous API restrictions on headers.)
|
||||||
|
|
||||||
|
|
||||||
Version 0.8:
|
Version 0.8:
|
||||||
|
|
||||||
|
|||||||
@@ -89,10 +89,11 @@ Some notes and limitations:
|
|||||||
Special handling for embedded images
|
Special handling for embedded images
|
||||||
|
|
||||||
**Headers**
|
**Headers**
|
||||||
Djrill accepts additional headers, but only ``Reply-To`` and
|
Djrill accepts additional headers and passes them along to Mandrill.
|
||||||
``X-*`` (since that is all that Mandrill accepts). Any other extra headers
|
|
||||||
will raise :exc:`~djrill.NotSupportedByMandrillError` when you attempt to send the
|
.. versionchanged:: 0.9
|
||||||
message.
|
In earlier versions, Djrill only allowed ``Reply-To`` and ``X-*`` headers,
|
||||||
|
matching previous Mandrill API restrictions.
|
||||||
|
|
||||||
|
|
||||||
.. _mandrill-send-support:
|
.. _mandrill-send-support:
|
||||||
|
|||||||
Reference in New Issue
Block a user