diff --git a/djrill/mail/backends/djrill.py b/djrill/mail/backends/djrill.py index a75c2bf..e20756c 100644 --- a/djrill/mail/backends/djrill.py +++ b/djrill/mail/backends/djrill.py @@ -49,6 +49,8 @@ class DjrillBackend(BaseEmailBackend): super(DjrillBackend, self).__init__(**kwargs) self.api_key = getattr(settings, "MANDRILL_API_KEY", None) self.api_url = MANDRILL_API_URL + + self.subaccount = getattr(settings, "MANDRILL_SUB_ACCOUNT", None) if not self.api_key: raise ImproperlyConfigured("You have not set your mandrill api key " @@ -187,6 +189,10 @@ class DjrillBackend(BaseEmailBackend): 'tags', 'preserve_recipients', 'view_content_link', 'subaccount', 'google_analytics_domains', 'google_analytics_campaign', 'metadata'] + + if self.subaccount: + msg_dict['subaccount'] = self.subaccount + for attr in mandrill_attrs: if hasattr(message, attr): msg_dict[attr] = getattr(message, attr) diff --git a/djrill/tests/__init__.py b/djrill/tests/__init__.py index b663f5b..f0af178 100644 --- a/djrill/tests/__init__.py +++ b/djrill/tests/__init__.py @@ -3,3 +3,4 @@ from djrill.tests.test_legacy import * from djrill.tests.test_mandrill_send import * from djrill.tests.test_mandrill_send_template import * from djrill.tests.test_mandrill_webhook import * +from djrill.tests.test_mandrill_subaccounts import * diff --git a/djrill/tests/test_mandrill_subaccounts.py b/djrill/tests/test_mandrill_subaccounts.py new file mode 100644 index 0000000..bfd647d --- /dev/null +++ b/djrill/tests/test_mandrill_subaccounts.py @@ -0,0 +1,38 @@ +from django.core import mail + +from djrill.tests.mock_backend import DjrillBackendMockAPITestCase + +from django.conf import settings + +class DjrillMandrillSubaccountTests(DjrillBackendMockAPITestCase): + """Test Djrill backend support for Mandrill subaccounts""" + + def test_send_basic(self): + mail.send_mail('Subject here', 'Here is the message.', + 'from@example.com', ['to@example.com'], fail_silently=False) + self.assert_mandrill_called("/messages/send.json") + data = self.get_api_call_data() + self.assertEqual(data['message']['subject'], "Subject here") + self.assertEqual(data['message']['text'], "Here is the message.") + self.assertFalse('from_name' in data['message']) + self.assertEqual(data['message']['from_email'], "from@example.com") + self.assertEqual(len(data['message']['to']), 1) + self.assertEqual(data['message']['to'][0]['email'], "to@example.com") + + + def test_send_from_subaccount(self): + settings.MANDRILL_SUB_ACCOUNT = "test_subaccount" + + mail.send_mail('Subject here', 'Here is the message.', + 'from@example.com', ['to@example.com'], fail_silently=False) + self.assert_mandrill_called("/messages/send.json") + data = self.get_api_call_data() + self.assertEqual(data['message']['subject'], "Subject here") + self.assertEqual(data['message']['text'], "Here is the message.") + self.assertFalse('from_name' in data['message']) + self.assertEqual(data['message']['from_email'], "from@example.com") + self.assertEqual(len(data['message']['to']), 1) + self.assertEqual(data['message']['to'][0]['email'], "to@example.com") + + + self.assertEqual(data['message']['subaccount'], settings.MANDRILL_SUB_ACCOUNT)