DjrillMessage which wraps Django's EmailMultiAlternatives. Mail backend now works with standard email and multipart through the DjrillMessage object.

This commit is contained in:
Chris Jones
2012-02-28 13:03:26 -08:00
parent d6ff089060
commit 91b846fea3
2 changed files with 81 additions and 10 deletions

View File

@@ -0,0 +1,37 @@
from django.core.exceptions import ImproperlyConfigured
from django.core.mail import EmailMultiAlternatives
class DjrillMessage(EmailMultiAlternatives):
alternative_subtype = "mandrill"
def __init__(self, subject='', body='', from_email=None, to=None, bcc=None,
connection=None, attachments=None, headers=None, alternatives=None,
cc=None, tags=None, track_opens=True, track_clicks=True):
super(DjrillMessage, self).__init__(subject, body, from_email, to, bcc,
connection, attachments, headers, alternatives, cc)
self.tags = self._set_mandrill_tags(tags)
self.track_opens = track_opens
self.track_clicks = track_clicks
def _set_mandrill_tags(self, tags):
"""
Check that all tags are below 50 chars and that they do not start
with an underscore.
Raise ImproperlyConfigured if an underscore tag is passed in to
alert the user. Any tag over 50 chars is left out of the list.
"""
tag_list = []
for tag in tags:
if len(tag) <= 50 and not tag.startswith("_"):
tag_list.append(tag)
elif tag.startswith("_"):
raise ImproperlyConfigured(
"Tags starting with an underscore are reserved for "
"internal use and will cause errors with Mandill's API")
return tag_list