Skip to content

Commit 9cdfbf2

Browse files
committed
Merge pull request #57 from rajumsys/django-backend
Django email backend
2 parents 7b133a5 + eb590c5 commit 9cdfbf2

File tree

10 files changed

+374
-0
lines changed

10 files changed

+374
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,4 @@ venv/
6262

6363
# Mac OSX
6464
.DS_Store
65+
.idea

.travis.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@ language: python
22
python:
33
- "2.7"
44
- "3.4"
5+
env:
6+
- DJANGO_VERSION=1.7
7+
- DJANGO_VERSION=1.8
8+
- DJANGO_VERSION=1.9b1
59
install:
610
- pip install -r dev-requirements.txt
11+
- pip uninstall django --yes
12+
- pip install -q django==$DJANGO_VERSION
713
- pip install coveralls
814
- pip install -e .
915
before_script:

README.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,16 @@ Here at SparkPost, our messages are known as transmissions. Let's use the underl
8888
8989
.. _transmissions API: https://www.sparkpost.com/api#/reference/transmissions
9090

91+
Django Integration
92+
------------------
93+
The SparkPost python library comes with an email backend for Django. Put the following configuration in `settings.py` file.
94+
95+
.. code-block:: python
96+
97+
SPARKPOST_API_KEY = 'API_KEY'
98+
EMAIL_BACKEND = 'sparkpost.django.email_backend.SparkPostEmailBackend'
99+
100+
Replace *API_KEY* with an actual API key that you've generated in `Get a Key`_ section.
91101

92102
Documentation
93103
-------------

dev-requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ requests==2.5.1
55
responses==0.3.0
66
wheel
77
twine
8+
Django>=1.8,<1.9

docs/django/backend.rst

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
Django Email Backend
2+
====================
3+
4+
The SparkPost python library comes with an email backend for Django.
5+
6+
Configure Django
7+
----------------
8+
9+
To configure Django to use SparkPost, put the following configuration in `settings.py` file.
10+
11+
.. code-block:: python
12+
13+
SPARKPOST_API_KEY = 'API_KEY'
14+
EMAIL_BACKEND = 'sparkpost.django.email_backend.SparkPostEmailBackend'
15+
16+
Replace *API_KEY* with an actual API key.
17+
18+
19+
Sending an email
20+
----------------
21+
22+
Django is now configured to use the SparkPost email backend. You can now send mail using Django's `send_mail` method:
23+
24+
.. code-block:: python
25+
26+
from django.core.mail import send_mail
27+
28+
send_mail(
29+
subject='hello from sparkpost',
30+
message='Hello Rock stars!'
31+
from_email='[email protected]',
32+
recipient_list=['[email protected]'],
33+
html_message='<p>Hello Rock stars!</p>',
34+
)
35+
36+
37+
Supported version
38+
-----------------
39+
SparkPost will support all versions of Django that are within extended support period. Refer to `Django Supported_Version`_.
40+
41+
Current supported versions are:
42+
* 1.7
43+
* 1.8
44+
* 1.9b1
45+
46+
47+
.. _Django Supported_Version: https://www.djangoproject.com/download/#supported-versions
48+
49+
50+
Additional documentation
51+
------------------------
52+
53+
See our `Using SparkPost with Django`_ in support article.
54+
55+
.. _Using SparkPost with Django: https://support.sparkpost.com/customer/en/portal/articles/2169630-using-sparkpost-with-django?b_id=7411
56+

docs/index.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@ Auto-generated API reference for python-sparkpost:
5858

5959
api
6060

61+
Using in Django
62+
---------------
63+
64+
Configure Django to use SparkPost email backend
65+
66+
.. toctree::
67+
:maxdepth: 2
68+
69+
django/backend
70+
6171

6272
Additional documentation
6373
------------------------

sparkpost/django/__init__.py

Whitespace-only changes.

sparkpost/django/email_backend.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
from django.conf import settings
2+
from django.core.mail.backends.base import BaseEmailBackend
3+
4+
from sparkpost import SparkPost
5+
6+
from .exceptions import UnsupportedContent
7+
from .exceptions import UnsupportedParam
8+
9+
10+
class SparkPostEmailBackend(BaseEmailBackend):
11+
"""
12+
SparkPost wrapper for Django email backend
13+
"""
14+
15+
def __init__(self, fail_silently=False, **kwargs):
16+
super(SparkPostEmailBackend, self)\
17+
.__init__(fail_silently=fail_silently, **kwargs)
18+
19+
sp_api_key = getattr(settings, 'SPARKPOST_API_KEY', None)
20+
21+
self.client = SparkPost(sp_api_key)
22+
23+
def send_messages(self, email_messages):
24+
"""
25+
Send emails, returns integer representing number of successful emails
26+
"""
27+
success = 0
28+
for message in email_messages:
29+
try:
30+
response = self._send(message)
31+
success += response['total_accepted_recipients']
32+
except Exception:
33+
if not self.fail_silently:
34+
raise
35+
return success
36+
37+
def _send(self, message):
38+
self.check_unsupported(message)
39+
self.check_attachments(message)
40+
41+
params = dict(
42+
recipients=message.to,
43+
text=message.body,
44+
from_email=message.from_email,
45+
subject=message.subject
46+
)
47+
48+
if hasattr(message, 'alternatives') and len(message.alternatives) > 0:
49+
for alternative in message.alternatives:
50+
51+
if alternative[1] == 'text/html':
52+
params['html'] = alternative[0]
53+
else:
54+
raise UnsupportedContent(
55+
'Content type %s is not supported' % alternative[1]
56+
)
57+
58+
return self.client.transmissions.send(**params)
59+
60+
@staticmethod
61+
def check_attachments(message):
62+
if len(message.attachments):
63+
raise UnsupportedContent(
64+
'The SparkPost Django email backend does not '
65+
'currently support attachment.'
66+
)
67+
68+
@staticmethod
69+
def check_unsupported(message):
70+
unsupported_params = ['cc', 'bcc', 'reply_to']
71+
for param in unsupported_params:
72+
if len(getattr(message, param, [])):
73+
raise UnsupportedParam(
74+
'The SparkPost Django email backend does not currently '
75+
'support %s.' % param
76+
)

sparkpost/django/exceptions.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class UnsupportedContent(Exception):
2+
pass
3+
4+
5+
class UnsupportedParam(Exception):
6+
pass

0 commit comments

Comments
 (0)