Skip to content

Commit bb763f3

Browse files
Add SENDGRID_RAISE_UNHANDLED setting
1 parent 7a8ffd4 commit bb763f3

3 files changed

Lines changed: 30 additions & 6 deletions

File tree

README.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Add the following to your project’s **settings.py**:
2424
EMAIL_BACKEND = "sgbackend.SendGridBackend"
2525
SENDGRID_USER = "Your SendGrid Username"
2626
SENDGRID_PASSWORD = "Your SendGrid Password"
27+
SENDGRID_RAISE_UNHANDLED = True
2728
2829
**Done!**
2930

sgbackend/exceptions.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# -*- encoding: utf-8 -*-
2+
3+
from __future__ import absolute_import, unicode_literals
4+
5+
from sendgrid import exceptions
6+
7+
8+
class SendGridUnhandledContentTypeError(exceptions.SendGridError):
9+
pass

sgbackend/mail.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,30 @@
33
import sendgrid
44
from django.conf import settings
55
from django.core.exceptions import ImproperlyConfigured
6-
from django.core.mail import EmailMultiAlternatives
76
from django.core.mail.backends.base import BaseEmailBackend
87
from email.mime.base import MIMEBase
98
try:
109
import rfc822
1110
except Exception as e:
1211
import email.utils as rfc822
1312

13+
from . import exceptions
14+
15+
__all__ = ('HANDLED_CONTENT_TYPES', 'SendGridBackend')
16+
17+
HANDLED_CONTENT_TYPES = {'text/html', 'text/plain'}
18+
1419

1520
class SendGridBackend(BaseEmailBackend):
1621
"""
1722
Email back-end using SendGrid Web API
1823
"""
24+
1925
def __init__(self, fail_silently=False, **kwargs):
2026
super(SendGridBackend, self).__init__(fail_silently=fail_silently, **kwargs)
2127
self.api_user = getattr(settings, 'SENDGRID_USER', None)
2228
self.api_key = getattr(settings, 'SENDGRID_PASSWORD', None)
29+
self.raise_unhandled = getattr(settings, 'SENDGRID_RAISE_UNHANDLED', False)
2330
if self.api_user is None or self.api_key is None:
2431
raise ImproperlyConfigured('Either SENDGRID_USER or SENDGRID_PASSWORD was not declared in settings.py')
2532
self.sendgrid = sendgrid.SendGridClient(self.api_user, self.api_key, raise_errors=not fail_silently)
@@ -52,11 +59,18 @@ def _build_sengrid_mail(self, email):
5259
mail.set_from(email.from_email)
5360

5461
text, html = '', email.body if email.content_subtype == 'html' else email.body, ''
55-
if not html and isinstance(email, EmailMultiAlternatives):
56-
try:
57-
html = next(c for c, t in email.alternatives if t == 'text/html')
58-
except StopIteration:
59-
pass
62+
if hasattr(email, 'alternatives'):
63+
if self.raise_unhandled:
64+
unhandled_types = [t for c, t in email.alternatives if t not in HANDLED_CONTENT_TYPES]
65+
if unhandled_types:
66+
raise exceptions.SendGridUnhandledContentTypeError(
67+
"SendGrid API don't handle content of type(s) {0}".format(unhandled_types)
68+
)
69+
if not html:
70+
try:
71+
html = next(c for c, t in email.alternatives if t == 'text/html')
72+
except StopIteration:
73+
pass
6074
mail.set_text(text)
6175
mail.set_html(html)
6276

0 commit comments

Comments
 (0)