Permalink
Browse files

Merge branch 'master' of github.com:themartorana/python-postmark

  • Loading branch information...
2 parents acba8bd + c8de853 commit a0bbd91eb8e34bfbf41b1b241164b0e255d9b708 @themartorana committed May 4, 2011
Showing with 45 additions and 9 deletions.
  1. +20 −7 postmark/core.py
  2. +25 −2 postmark/django_backend.py
View
@@ -8,7 +8,11 @@
#
# Imports (JSON library based on import try)
-import email.mime.base
+try:
+ from email.mime.base import MIMEBase
+except ImportError, e:
+ from email import MIMEBase
+
import sys
import urllib
import urllib2
@@ -98,9 +102,10 @@ def __init__(self, **kwargs):
try:
from django import VERSION
from django.conf import settings as django_settings
- self.__api_key = django_settings.POSTMARK_API_KEY
self.__user_agent = '%s (Django %s)' % (self.__user_agent, '_'.join([str(var) for var in VERSION]))
- if not self.__sender:
+ if not self.__api_key and hasattr(django_settings, 'POSTMARK_API_KEY'):
+ self.__api_key = django_settings.POSTMARK_API_KEY
+ if not self.__sender and hasattr(django_settings, 'POSTMARK_SENDER'):
self.__sender = django_settings.POSTMARK_SENDER
except ImportError:
pass
@@ -296,7 +301,7 @@ def _check_values(self):
raise PMMailMissingValueException('Cannot send an e-mail without either an HTML or text version of your e-mail body')
- def send(self, test=False):
+ def send(self, test=None):
'''
Send the email through the Postmark system.
Pass test=True to just print out the resulting
@@ -348,7 +353,7 @@ def send(self, test=False):
"Content": attachment[1],
"ContentType": attachment[2],
})
- elif isinstance(attachment, email.mime.base.MIMEBase):
+ elif isinstance(attachment, MIMEBase):
attachments.append({
"Name": attachment.get_filename(),
"Content": attachment.get_payload(),
@@ -360,6 +365,14 @@ def send(self, test=False):
# # TODO: Set up regex to strip html
# pass
+ # If test is not specified, attempt to read the Django setting
+ if test is None:
+ try:
+ from django.conf import settings as django_settings
+ test = getattr(django_settings, "POSTMARK_TEST_MODE", None)
+ except ImportError:
+ pass
+
# If this is a test, just print the message
if test:
print 'JSON message is:\n%s' % json.dumps(json_message)
@@ -438,9 +451,9 @@ def __init__(self, **kwargs):
try:
from django import VERSION
from django.conf import settings as django_settings
- self.__api_key = django_settings.POSTMARK_API_KEY
+ if not self.__api_key and hasattr(django_settings, 'POSTMARK_API_KEY'):
+ self.__api_key = django_settings.POSTMARK_API_KEY
self.__user_agent = '%s (Django %s)' % (self.__user_agent, '_'.join([str(var) for var in VERSION]))
- self.__sender = django_settings.POSTMARK_SENDER
except ImportError:
pass
View
@@ -1,9 +1,30 @@
from django.conf import settings
from django.core.mail.backends.base import BaseEmailBackend
from django.core.exceptions import ImproperlyConfigured
+from django.core.mail import EmailMessage, EmailMultiAlternatives
from core import PMMail
+class PMEmailMessage(EmailMessage):
+ def __init__(self, *args, **kwargs):
+ if 'tag' in kwargs:
+ self.tag = kwargs['tag']
+ del kwargs['tag']
+ else:
+ self.tag = None
+
+ super(PMEmailMessage, self).__init__(*args, **kwargs)
+
+class PMEmailMultiAlternatives(EmailMultiAlternatives):
+ def __init__(self, *args, **kwargs):
+ if 'tag' in kwargs:
+ self.tag = kwargs['tag']
+ del kwargs['tag']
+ else:
+ self.tag = None
+
+ super(PMEmailMultiAlternatives, self).__init__(*args, **kwargs)
+
class EmailBackend(BaseEmailBackend):
def __init__(self, api_key=None, default_sender=None, **kwargs):
@@ -40,7 +61,7 @@ def _send(self, message):
recipients = ','.join(message.to)
html_body = None
- if message.__class__.__name__ == 'EmailMultiAlternatives':
+ if isinstance(message, EmailMultiAlternatives):
for alt in message.alternatives:
if alt[1] == "text/html":
html_body=alt[0]
@@ -67,7 +88,9 @@ def _send(self, message):
reply_to=reply_to,
custom_headers=custom_headers,
attachments=attachments)
-
+
+ postmark_message.tag = getattr(message, 'tag', None)
+
postmark_message.send(test=self.test_mode)
except:
if self.fail_silently:

0 comments on commit a0bbd91

Please sign in to comment.