From 0063fde2e5867388bcd3632a01f4dc5fbdc5bb78 Mon Sep 17 00:00:00 2001 From: Patrick Kimber Date: Mon, 4 Aug 2014 15:32:24 +0100 Subject: [PATCH] if missing 'from_email', then 'log_message' will fail to build --- djrill/mail/backends/djrill.py | 8 ++++++-- djrill/tests/test_mandrill_send_template.py | 11 +++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/djrill/mail/backends/djrill.py b/djrill/mail/backends/djrill.py index 9e31917..c0ed3f3 100644 --- a/djrill/mail/backends/djrill.py +++ b/djrill/mail/backends/djrill.py @@ -112,11 +112,15 @@ class DjrillBackend(BaseEmailBackend): message.mandrill_response = None if not self.fail_silently: + from_email = msg_dict.get('from_email', None) + from_message = "" + if from_email: + from_message = ", from %s" % from_email raise MandrillAPIError( status_code=response.status_code, response=response, - log_message="Failed to send a message to %s, from %s" % - (msg_dict['to'], msg_dict['from_email'])) + log_message="Failed to send a message to %s%s" % + (msg_dict['to'], from_message)) return False # add the response from mandrill to the EmailMessage so callers can inspect it diff --git a/djrill/tests/test_mandrill_send_template.py b/djrill/tests/test_mandrill_send_template.py index 45b7416..c1d9569 100644 --- a/djrill/tests/test_mandrill_send_template.py +++ b/djrill/tests/test_mandrill_send_template.py @@ -1,5 +1,6 @@ from django.core import mail +from djrill import MandrillAPIError from djrill.tests.mock_backend import DjrillBackendMockAPITestCase @@ -39,6 +40,16 @@ class DjrillMandrillSendTemplateTests(DjrillBackendMockAPITestCase): self.assertFalse('from_email' in data['message']) self.assertFalse('from_name' in data['message']) + def test_send_template_without_from_field_api_failure(self): + self.mock_post.return_value = self.MockResponse(status_code=400) + msg = mail.EmailMessage('Subject', 'Text Body', + 'from@example.com', ['to@example.com']) + msg.template_name = "PERSONALIZED_SPECIALS" + msg.use_template_from = True + with self.assertRaises(MandrillAPIError): + msg.send() + self.assertEqual(sent, 0) + def test_send_template_without_subject_field(self): msg = mail.EmailMessage('Subject', 'Text Body', 'from@example.com', ['to@example.com'])