mirror of
https://github.com/pacnpal/django-anymail.git
synced 2025-12-20 20:01:05 -05:00
Allow extra headers from any EmailMessage (not just DjrillMessage)
Also adds test case for basic functionality on django.core.mail.EmailMessage. Cherry-picked from: medmunds/Djrill@f0503783f
This commit is contained in:
@@ -96,16 +96,26 @@ class DjrillBackend(BaseEmailBackend):
|
|||||||
use by default. Standard text email messages sent through Django will
|
use by default. Standard text email messages sent through Django will
|
||||||
still work through Mandrill.
|
still work through Mandrill.
|
||||||
"""
|
"""
|
||||||
return {
|
msg_dict = {
|
||||||
"text": message.body,
|
"text": message.body,
|
||||||
"subject": message.subject,
|
"subject": message.subject,
|
||||||
"from_email": self.sender,
|
"from_email": self.sender,
|
||||||
"to": self.recipients
|
"to": self.recipients
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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]})
|
||||||
|
msg_dict.update({"headers": accepted_headers})
|
||||||
|
|
||||||
|
return msg_dict
|
||||||
|
|
||||||
def _build_advanced_message_dict(self, message):
|
def _build_advanced_message_dict(self, message):
|
||||||
"""
|
"""
|
||||||
Builds advanced message dict and attaches any accepted extra headers.
|
Builds advanced message dict
|
||||||
"""
|
"""
|
||||||
self.msg_dict.update({
|
self.msg_dict.update({
|
||||||
"from_name": message.from_name,
|
"from_name": message.from_name,
|
||||||
@@ -113,14 +123,6 @@ class DjrillBackend(BaseEmailBackend):
|
|||||||
"track_opens": message.track_opens,
|
"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):
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -65,6 +65,33 @@ class DjrillBackendTests(DjrillBackendMockAPITestCase):
|
|||||||
mail.send_mail('Subject', 'Message', 'from@example.com',
|
mail.send_mail('Subject', 'Message', 'from@example.com',
|
||||||
['to@example.com'])
|
['to@example.com'])
|
||||||
|
|
||||||
|
def test_email_message(self):
|
||||||
|
email = mail.EmailMessage('Subject', 'Body goes here',
|
||||||
|
'from@example.com',
|
||||||
|
['to1@example.com', 'Also To <to2@example.com>'],
|
||||||
|
bcc=['bcc1@example.com', 'Also BCC <bcc2@example.com>'],
|
||||||
|
cc=['cc1@example.com', 'Also CC <cc2@example.com>'],
|
||||||
|
headers={'Reply-To': 'another@example.com',
|
||||||
|
'X-MyHeader': 'my value',
|
||||||
|
'Errors-To': 'silently stripped'})
|
||||||
|
email.send()
|
||||||
|
data = self.get_api_call_data()
|
||||||
|
self.assertEqual(data['message']['subject'], "Subject")
|
||||||
|
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' })
|
||||||
|
# Mandrill doesn't have a notion of cc, and only allows a single bcc.
|
||||||
|
# Djrill just treats cc and bcc as though they were "to" addresses,
|
||||||
|
# which may or may not be what you want.
|
||||||
|
self.assertEqual(len(data['message']['to']), 6)
|
||||||
|
self.assertEqual(data['message']['to'][0]['email'], "to1@example.com")
|
||||||
|
self.assertEqual(data['message']['to'][1]['email'], "to2@example.com")
|
||||||
|
self.assertEqual(data['message']['to'][2]['email'], "cc1@example.com")
|
||||||
|
self.assertEqual(data['message']['to'][3]['email'], "cc2@example.com")
|
||||||
|
self.assertEqual(data['message']['to'][4]['email'], "bcc1@example.com")
|
||||||
|
self.assertEqual(data['message']['to'][5]['email'], "bcc2@example.com")
|
||||||
|
|
||||||
|
|
||||||
class DjrillMessageTests(TestCase):
|
class DjrillMessageTests(TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
|||||||
Reference in New Issue
Block a user