Permalink
Browse files

Support for proper bulk sending from Django's send_mass_mail function

Constructs an actual PMBatchMail object to send instead of many
individual calls to PMMail.send()
  • Loading branch information...
mattrobenolt committed Feb 25, 2012
1 parent a38a261 commit 5ce4029cda442f7df166185527f22a16ade7b70b
Showing with 51 additions and 46 deletions.
  1. +51 −46 postmark/django_backend.py
View
@@ -3,7 +3,7 @@
from django.core.exceptions import ImproperlyConfigured
from django.core.mail import EmailMessage, EmailMultiAlternatives
-from core import PMMail
+from core import PMMail, PMBatchMail
class PMEmailMessage(EmailMessage):
def __init__(self, *args, **kwargs):
@@ -45,58 +45,63 @@ def send_messages(self, email_messages):
"""
if not email_messages:
return
- num_sent = 0
- for message in email_messages:
- sent = self._send(message)
- if sent:
- num_sent += 1
- return num_sent
+ sent = self._send(email_messages)
+ if sent:
+ return len(email_messages)
+ return 0
- def _send(self, message):
- """A helper method that does the actual sending."""
+ def _build_message(self, message):
+ """A helper method to convert a PMEmailMessage to a PMMail"""
if not message.recipients():
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
+
+ reply_to = None
+ custom_headers = {}
+ if message.extra_headers and isinstance(message.extra_headers, dict):
+ if message.extra_headers.has_key('Reply-To'):
+ reply_to = message.extra_headers.pop('Reply-To')
+ if len(message.extra_headers):
+ custom_headers = message.extra_headers
+ attachments = []
+ if message.attachments and isinstance(message.attachments, list):
+ if len(message.attachments):
+ attachments = message.attachments
+
+ postmark_message = PMMail(api_key=self.api_key,
+ subject=message.subject,
+ sender=message.from_email,
+ to=recipients,
+ bcc=recipients_bcc,
+ text_body=message.body,
+ html_body=html_body,
+ reply_to=reply_to,
+ custom_headers=custom_headers,
+ attachments=attachments)
+
+ postmark_message.tag = getattr(message, 'tag', None)
+ return postmark_message
+
+ def _send(self, messages):
+ """A helper method that does the actual sending."""
+ if len(messages) == 1:
+ to_send = self._build_message(messages[0])
+ else:
+ postmark_messages = map(self._build_message, messages)
+ to_send = PMBatchMail(messages=postmark_messages)
try:
- 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
-
- reply_to = None
- custom_headers = {}
- if message.extra_headers and isinstance(message.extra_headers, dict):
- if message.extra_headers.has_key('Reply-To'):
- reply_to = message.extra_headers.pop('Reply-To')
- if len(message.extra_headers):
- custom_headers = message.extra_headers
- attachments = []
- if message.attachments and isinstance(message.attachments, list):
- if len(message.attachments):
- attachments = message.attachments
-
- postmark_message = PMMail(api_key=self.api_key,
- subject=message.subject,
- sender=message.from_email,
- to=recipients,
- bcc=recipients_bcc,
- text_body=message.body,
- html_body=html_body,
- reply_to=reply_to,
- custom_headers=custom_headers,
- attachments=attachments)
-
- postmark_message.tag = getattr(message, 'tag', None)
-
- postmark_message.send(test=self.test_mode)
+ to_send.send(test=self.test_mode)
except:
if self.fail_silently:
return False
raise
return True
-

0 comments on commit 5ce4029

Please sign in to comment.