Skip to content

Commit

Permalink
Fix Python 3 support and PEP8
Browse files Browse the repository at this point in the history
    - unicode() doesn't exist in Python 3, so move to using Django's
      smart_text since it abstracts away the Python 2 vs 3 differences
      for us and Django is already a dependency
    - PEP8 fixes
  • Loading branch information
frankwiles committed Sep 4, 2015
1 parent cc8c3ee commit c74b5d7
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions django_mailgun/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django.conf import settings
from django.core.mail.backends.base import BaseEmailBackend
from django.core.mail.message import sanitize_address
from django.utils.encoding import smart_text

try:
from io import StringIO
Expand All @@ -11,19 +12,21 @@
except ImportError:
from StringIO import StringIO


class MailgunAPIError(Exception):
pass


class MailgunBackend(BaseEmailBackend):
"""A Django Email backend that uses mailgun.
"""

def __init__(self, fail_silently=False, *args, **kwargs):
access_key, server_name = (kwargs.pop('access_key', None),
kwargs.pop('server_name', None))

super(MailgunBackend, self).__init__(
fail_silently=fail_silently,
fail_silently=fail_silently,
*args, **kwargs)

try:
Expand Down Expand Up @@ -55,6 +58,7 @@ def _send(self, email_message):
recipients = [sanitize_address(addr, email_message.encoding)
for addr in email_message.recipients()]

message = smart_text(email_message.message().as_string(), errors='ignore')
try:
r = requests.\
post(self._api_url + "messages.mime",
Expand All @@ -64,7 +68,7 @@ def _send(self, email_message):
"from": from_email,
},
files={
"message": StringIO(unicode(email_message.message().as_string(), errors="ignore")),
"message": StringIO(message),
}
)
except:
Expand Down

0 comments on commit c74b5d7

Please sign in to comment.