mirror of
https://github.com/pacnpal/django-anymail.git
synced 2025-12-20 11:51:05 -05:00
Handle extra headers accepted by Mandrill, also extra option of from_name. Tests to cover checking of extra headers and from_name.
This commit is contained in:
@@ -80,6 +80,7 @@ class DjrillBackend(BaseEmailBackend):
|
|||||||
|
|
||||||
if getattr(message, "alternative_subtype", None):
|
if getattr(message, "alternative_subtype", None):
|
||||||
if message.alternative_subtype == "mandrill":
|
if message.alternative_subtype == "mandrill":
|
||||||
|
self._build_advanced_message_dict(message)
|
||||||
if message.alternatives:
|
if message.alternatives:
|
||||||
self._add_alternatives(message)
|
self._add_alternatives(message)
|
||||||
|
|
||||||
@@ -109,6 +110,25 @@ class DjrillBackend(BaseEmailBackend):
|
|||||||
"to": self.recipients
|
"to": self.recipients
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def _build_advanced_message_dict(self, message):
|
||||||
|
"""
|
||||||
|
Builds advanced message dict and attaches any accepted extra headers.
|
||||||
|
"""
|
||||||
|
self.msg_dict.update({
|
||||||
|
"from_name": message.from_name,
|
||||||
|
"tags": message.tags,
|
||||||
|
"track_opens": message.track_opens,
|
||||||
|
})
|
||||||
|
|
||||||
|
if message.extra_headers:
|
||||||
|
accepted_headers = {}
|
||||||
|
|
||||||
|
for k in message.extra_headers.keys():
|
||||||
|
if k.startswith("X-") or k == "Reply-To":
|
||||||
|
accepted_headers.update(
|
||||||
|
{"%s" % k: message.extra_headers[k]})
|
||||||
|
self.msg_dict.update({"headers": accepted_headers})
|
||||||
|
|
||||||
def _add_alternatives(self, message):
|
def _add_alternatives(self, message):
|
||||||
"""
|
"""
|
||||||
There can be only one! ... alternative attachment.
|
There can be only one! ... alternative attachment.
|
||||||
@@ -123,9 +143,6 @@ class DjrillBackend(BaseEmailBackend):
|
|||||||
"check the alternatives you have attached to your message.")
|
"check the alternatives you have attached to your message.")
|
||||||
|
|
||||||
self.msg_dict.update({
|
self.msg_dict.update({
|
||||||
"from_name": message.from_name,
|
|
||||||
"html": message.alternatives[0][0],
|
"html": message.alternatives[0][0],
|
||||||
"tags": message.tags,
|
|
||||||
"track_opens": message.track_opens,
|
|
||||||
"track_clicks": message.track_clicks
|
"track_clicks": message.track_clicks
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -8,21 +8,26 @@ from djrill.mail import DjrillMessage
|
|||||||
class DjrillMessageTests(TestCase):
|
class DjrillMessageTests(TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.subject = "Djrill baby djrill."
|
self.subject = "Djrill baby djrill."
|
||||||
|
self.from_name = "Tarzan"
|
||||||
self.from_email = "test@example"
|
self.from_email = "test@example"
|
||||||
self.to = ["King Kong <kingkong@example.com>",
|
self.to = ["King Kong <kingkong@example.com>",
|
||||||
"Cheetah <cheetah@example.com", "bubbles@example.com"]
|
"Cheetah <cheetah@example.com", "bubbles@example.com"]
|
||||||
self.text_content = "Wonderful fallback text content."
|
self.text_content = "Wonderful fallback text content."
|
||||||
self.html_content = "<h1>That's a nice HTML email right there.</h1>"
|
self.html_content = "<h1>That's a nice HTML email right there.</h1>"
|
||||||
|
self.headers = {"Reply-To": "tarzan@example.com"}
|
||||||
self.tags = ["track", "this"]
|
self.tags = ["track", "this"]
|
||||||
|
|
||||||
def test_djrill_message_success(self):
|
def test_djrill_message_success(self):
|
||||||
msg = DjrillMessage(self.subject, self.text_content, self.from_email,
|
msg = DjrillMessage(self.subject, self.text_content, self.from_email,
|
||||||
self.to, tags=self.tags)
|
self.to, tags=self.tags, headers=self.headers,
|
||||||
|
from_name=self.from_name)
|
||||||
|
|
||||||
self.assertIsInstance(msg, DjrillMessage)
|
self.assertIsInstance(msg, DjrillMessage)
|
||||||
self.assertEqual(msg.body, self.text_content)
|
self.assertEqual(msg.body, self.text_content)
|
||||||
self.assertEqual(msg.recipients(), self.to)
|
self.assertEqual(msg.recipients(), self.to)
|
||||||
self.assertEqual(msg.tags, self.tags)
|
self.assertEqual(msg.tags, self.tags)
|
||||||
|
self.assertEqual(msg.extra_headers, self.headers)
|
||||||
|
self.assertEqual(msg.from_name, self.from_name)
|
||||||
|
|
||||||
def test_djrill_message_html_success(self):
|
def test_djrill_message_html_success(self):
|
||||||
msg = DjrillMessage(self.subject, self.text_content, self.from_email,
|
msg = DjrillMessage(self.subject, self.text_content, self.from_email,
|
||||||
|
|||||||
Reference in New Issue
Block a user