diff --git a/post_office/models.py b/post_office/models.py index c56686d7..d0e3f158 100644 --- a/post_office/models.py +++ b/post_office/models.py @@ -104,7 +104,7 @@ def prepare_email_message(self): if get_override_recipients(): self.to = get_override_recipients() - if self.context is not None: + if self.template is not None and self.context is not None: engine = get_template_engine() subject = engine.from_string(self.template.subject).render(self.context) plaintext_message = engine.from_string(self.template.content).render(self.context) diff --git a/post_office/tests/test_models.py b/post_office/tests/test_models.py index 77ec5202..a50ba975 100644 --- a/post_office/tests/test_models.py +++ b/post_office/tests/test_models.py @@ -65,6 +65,20 @@ def test_email_message_render(self): self.assertEqual(message.body, 'Content test') self.assertEqual(message.alternatives[0][0], 'HTML test') + def test_email_message_prepare_without_template_and_with_context(self): + """ + Ensure Email instance without template but with context is properly prepared. + """ + context = {'name': 'test'} + email = Email.objects.create(to=['to@example.com'], template=None, + subject='Subject test', message='Content test', + html_message='HTML test', + from_email='from@e.com', context=context) + message = email.email_message() + self.assertEqual(message.subject, 'Subject test') + self.assertEqual(message.body, 'Content test') + self.assertEqual(message.alternatives[0][0], 'HTML test') + def test_dispatch(self): """ Ensure that email.dispatch() actually sends out the email