mirror of
https://github.com/pacnpal/django-anymail.git
synced 2025-12-20 11:51:05 -05:00
use_template_subject and use_template_from
Change "clear_subject" and "clear_from" to clarify expected usage. Update docs. Acknowledge contribution.
This commit is contained in:
@@ -15,3 +15,4 @@ Eric Hennings
|
|||||||
Michael Hobbs
|
Michael Hobbs
|
||||||
Sameer Al-Sakran
|
Sameer Al-Sakran
|
||||||
Kyle Gibson
|
Kyle Gibson
|
||||||
|
nikolay-saskovets
|
||||||
|
|||||||
@@ -87,7 +87,6 @@ class DjrillBackend(BaseEmailBackend):
|
|||||||
if getattr(message, 'alternatives', None):
|
if getattr(message, 'alternatives', None):
|
||||||
self._add_alternatives(message, msg_dict)
|
self._add_alternatives(message, msg_dict)
|
||||||
self._add_attachments(message, msg_dict)
|
self._add_attachments(message, msg_dict)
|
||||||
self._filter_msg_dict(message, msg_dict)
|
|
||||||
api_params['message'] = msg_dict
|
api_params['message'] = msg_dict
|
||||||
|
|
||||||
# check if template is set in message to send it via
|
# check if template is set in message to send it via
|
||||||
@@ -145,13 +144,17 @@ class DjrillBackend(BaseEmailBackend):
|
|||||||
content = "html" if message.content_subtype == "html" else "text"
|
content = "html" if message.content_subtype == "html" else "text"
|
||||||
msg_dict = {
|
msg_dict = {
|
||||||
content: message.body,
|
content: message.body,
|
||||||
"subject": message.subject,
|
|
||||||
"from_email": from_email,
|
|
||||||
"to": to_list
|
"to": to_list
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if not getattr(message, 'use_template_from', False):
|
||||||
|
msg_dict["from_email"] = from_email
|
||||||
if from_name:
|
if from_name:
|
||||||
msg_dict["from_name"] = from_name
|
msg_dict["from_name"] = from_name
|
||||||
|
|
||||||
|
if not getattr(message, 'use_template_subject', False):
|
||||||
|
msg_dict["subject"] = message.subject
|
||||||
|
|
||||||
if message.extra_headers:
|
if message.extra_headers:
|
||||||
msg_dict["headers"] = message.extra_headers
|
msg_dict["headers"] = message.extra_headers
|
||||||
|
|
||||||
@@ -314,11 +317,3 @@ class DjrillBackend(BaseEmailBackend):
|
|||||||
'content': content_b64.decode('ascii'),
|
'content': content_b64.decode('ascii'),
|
||||||
}
|
}
|
||||||
return mandrill_attachment, is_embedded_image
|
return mandrill_attachment, is_embedded_image
|
||||||
|
|
||||||
def _filter_msg_dict(self, message, msg_dict):
|
|
||||||
"""Filter message data (e.g. clear subject field, or from field)"""
|
|
||||||
if hasattr(message, 'clear_from') and message.clear_from:
|
|
||||||
msg_dict['from_name'] = ''
|
|
||||||
msg_dict['from_email'] = ''
|
|
||||||
if hasattr(message, 'clear_subject') and message.clear_subject:
|
|
||||||
msg_dict['subject'] = ''
|
|
||||||
|
|||||||
@@ -31,15 +31,24 @@ class DjrillMandrillSendTemplateTests(DjrillBackendMockAPITestCase):
|
|||||||
msg = mail.EmailMessage('Subject', 'Text Body',
|
msg = mail.EmailMessage('Subject', 'Text Body',
|
||||||
'from@example.com', ['to@example.com'])
|
'from@example.com', ['to@example.com'])
|
||||||
msg.template_name = "PERSONALIZED_SPECIALS"
|
msg.template_name = "PERSONALIZED_SPECIALS"
|
||||||
msg.clear_from = True
|
msg.use_template_from = True
|
||||||
msg.clear_subject = True
|
|
||||||
msg.send()
|
msg.send()
|
||||||
self.assert_mandrill_called("/messages/send-template.json")
|
self.assert_mandrill_called("/messages/send-template.json")
|
||||||
data = self.get_api_call_data()
|
data = self.get_api_call_data()
|
||||||
self.assertEqual(data['template_name'], "PERSONALIZED_SPECIALS")
|
self.assertEqual(data['template_name'], "PERSONALIZED_SPECIALS")
|
||||||
self.assertEqual(data['message']['subject'], "")
|
self.assertFalse('from_email' in data['message'])
|
||||||
self.assertEqual(data['message']['from_email'], "")
|
self.assertFalse('from_name' in data['message'])
|
||||||
self.assertEqual(data['message']['from_name'], "")
|
|
||||||
|
def test_send_template_without_subject_field(self):
|
||||||
|
msg = mail.EmailMessage('Subject', 'Text Body',
|
||||||
|
'from@example.com', ['to@example.com'])
|
||||||
|
msg.template_name = "PERSONALIZED_SPECIALS"
|
||||||
|
msg.use_template_subject = True
|
||||||
|
msg.send()
|
||||||
|
self.assert_mandrill_called("/messages/send-template.json")
|
||||||
|
data = self.get_api_call_data()
|
||||||
|
self.assertEqual(data['template_name'], "PERSONALIZED_SPECIALS")
|
||||||
|
self.assertFalse('subject' in data['message'])
|
||||||
|
|
||||||
def test_no_template_content(self):
|
def test_no_template_content(self):
|
||||||
# Just a template, without any template_content to be merged
|
# Just a template, without any template_content to be merged
|
||||||
|
|||||||
@@ -40,20 +40,29 @@ can be used with templates.
|
|||||||
How To Use Default Mandrill Subject and From fields
|
How To Use Default Mandrill Subject and From fields
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
To use default *Mandril* subject or default from field you need send message
|
To use default Mandrill "subject" or "from" field from your template definition
|
||||||
to *Mandril* with empty subject or empty from field. This can be done using
|
(overriding your EmailMessage and Django defaults), set the following attrs:
|
||||||
the following attrs: :attr:`clear_subject` and :attr:`clear_from` on
|
:attr:`use_template_subject` and/or :attr:`use_template_from` on
|
||||||
your :class:`~django.core.mail.EmailMessage` object::
|
your :class:`~django.core.mail.EmailMessage` object::
|
||||||
# ...
|
# ...
|
||||||
msg.clear_subject = True
|
msg.use_template_subject = True
|
||||||
msg.clear_from = True
|
msg.use_template_from = True
|
||||||
msg.send()
|
msg.send()
|
||||||
|
|
||||||
If :attr:`clear_subject` is set, Djrill will send message without subject and
|
.. attribute:: use_template_subject
|
||||||
Mandrill will use default subject.
|
|
||||||
|
If `True`, Djrill will omit the subject, and Mandrill will
|
||||||
|
use the default subject from the template.
|
||||||
|
|
||||||
|
.. versionadded:: 1.1
|
||||||
|
|
||||||
|
.. attribute:: use_template_from
|
||||||
|
|
||||||
|
If `True`, Djrill will omit the "from" field, and Mandrill will
|
||||||
|
use the default "from" from the template.
|
||||||
|
|
||||||
|
.. versionadded:: 1.1
|
||||||
|
|
||||||
If :attr:`clear_from` is set, Djrill will send message without from field and
|
|
||||||
Mandrill will use default from field.
|
|
||||||
|
|
||||||
|
|
||||||
.. _django-templates:
|
.. _django-templates:
|
||||||
|
|||||||
Reference in New Issue
Block a user