Skip to content

Commit

Permalink
Merge pull request #36 from danxshap/content_subtype_issue
Browse files Browse the repository at this point in the history
Don't require content_subtype to be set to "html"
  • Loading branch information
themartorana committed Jun 10, 2014
2 parents 6e0cb88 + 832fa79 commit d01242b
Showing 1 changed file with 17 additions and 19 deletions.
36 changes: 17 additions & 19 deletions postmark/django_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def __init__(self, *args, **kwargs):
self.track_opens = getattr(settings, 'POSTMARK_TRACK_OPENS', False)

super(PMEmailMessage, self).__init__(*args, **kwargs)

class PMEmailMultiAlternatives(EmailMultiAlternatives):
def __init__(self, *args, **kwargs):
if 'tag' in kwargs:
Expand All @@ -36,52 +36,49 @@ def __init__(self, *args, **kwargs):
self.track_opens = getattr(settings, 'POSTMARK_TRACK_OPENS', False)

super(PMEmailMultiAlternatives, self).__init__(*args, **kwargs)

class EmailBackend(BaseEmailBackend):

def __init__(self, api_key=None, default_sender=None, **kwargs):
"""
Initialize the backend.
"""
super(EmailBackend, self).__init__(**kwargs)
self.api_key = api_key if api_key is not None else getattr(settings, 'POSTMARK_API_KEY', None)
if self.api_key is None:
raise ImproperlyConfigured('POSTMARK API key must be set in Django settings file or passed to backend constructor.')
raise ImproperlyConfigured('POSTMARK API key must be set in Django settings file or passed to backend constructor.')
self.default_sender = getattr(settings, 'POSTMARK_SENDER', default_sender)
self.test_mode = getattr(settings, 'POSTMARK_TEST_MODE', False)
self.test_mode = getattr(settings, 'POSTMARK_TEST_MODE', False)

def send_messages(self, email_messages):
"""
Sends one or more EmailMessage objects and returns the number of email
messages sent.
"""
if not email_messages:
return
return
sent = self._send(email_messages)
if sent:
return len(email_messages)
return 0


def _build_message(self, message):
"""A helper method to convert a PMEmailMessage to a PMMail"""
if not message.recipients():
return False
return False
recipients = ','.join(message.to)
recipients_bcc = ','.join(message.bcc)

html_body = None
if isinstance(message, EmailMultiAlternatives):
for alt in message.alternatives:
if alt[1] == "text/html":
html_body=alt[0]
break

if getattr(message, 'content_subtype', None) == 'html':
html_body=message.body


reply_to = None
custom_headers = {}
custom_headers = {}
if message.extra_headers and isinstance(message.extra_headers, dict):
if 'Reply-To' in message.extra_headers:
reply_to = message.extra_headers.pop('Reply-To')
Expand All @@ -91,8 +88,8 @@ def _build_message(self, message):
if message.attachments and isinstance(message.attachments, list):
if len(message.attachments):
attachments = message.attachments
postmark_message = PMMail(api_key=self.api_key,

postmark_message = PMMail(api_key=self.api_key,
subject=message.subject,
sender=message.from_email,
to=recipients,
Expand All @@ -102,7 +99,7 @@ def _build_message(self, message):
reply_to=reply_to,
custom_headers=custom_headers,
attachments=attachments)

postmark_message.tag = getattr(message, 'tag', None)
postmark_message.track_opens = getattr(message, 'track_opens', False)

Expand Down Expand Up @@ -131,3 +128,4 @@ def _send(self, messages):
return False
raise
return True

0 comments on commit d01242b

Please sign in to comment.