Skip to content

Commit

Permalink
added stubs for transforming smtp headers to api data
Browse files Browse the repository at this point in the history
  • Loading branch information
wsmith committed Sep 18, 2015
1 parent b2569a9 commit 76f0373
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions django_mailgun.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,23 @@
version = '0.6.0'


# A mapping of smtp headers to API key names
# https://documentation.mailgun.com/user_manual.html#sending-via-smtp
# https://documentation.mailgun.com/api-sending.html#sending
#
# structure is SMTP_HEADER: (api_name, data_transform_function)
HEADERS_MAP = {
'X-Mailgun-Tag': ('o:tag', lambda x: x),
'X-Mailgun-Campain-Id': ('o:campain', lambda x: x),
'X-Mailgun-Dkim': ('o:dkim', lambda x: x),
'X-Mailgun-Deliver-By': ('o:deliverytime', lambda x: x),
'X-Mailgun-Drop-Message': ('o:testmode', lambda x: x),
'X-Mailgun-Track': ('o:tracking', lambda x: x),
'X-Mailgun-Track-Clicks': ('o:tracking-clicks', lambda x: x),
'X-Mailgun-Track-Opens': ('o:tracking-opens', lambda x: x),
'X-Mailgun-Variables': ('v:my-var', lambda x: x),
}

class MailgunAPIError(Exception):
pass

Expand All @@ -37,6 +54,7 @@ def __init__(self, fail_silently=False, *args, **kwargs):
raise

self._api_url = "https://api.mailgun.net/v3/%s/" % self._server_name
self._headers_map = HEADERS_MAP

def open(self):
"""Stub for open connection, all sends are done over HTTP POSTs
Expand All @@ -48,6 +66,19 @@ def close(self):
"""
pass

def _map_smtp_headers_to_api_parameters(self, email_message):
"""
Map the values passed in SMTP headers to API-ready
2-item tuples present in HEADERS_MAP
:return: 2-item tuples of the form (api_name, api_values)
"""
api_data = []
for smtp_key, api_transformer in self._headers_map.iteritems():
data_to_transform = email_message.extra_headers.pop(smtp_key, None)
if data_to_transform is not None:
api_data.append(api_transformer[0], api_transformer[1](data_to_transform))
return api_data

def _send(self, email_message):
"""A helper method that does the actual sending."""
if not email_message.recipients():
Expand All @@ -68,6 +99,9 @@ def _send(self, email_message):
if recipient_variables is not None:
post_data.append(('recipient-variables', recipient_variables, ))

for name, value in self._map_smtp_headers_to_api_parameters(email_message):
post_data.append((name, value, ))

if hasattr(email_message, 'alternatives') and email_message.alternatives:
for alt in email_message.alternatives:
if alt[1] == 'text/html':
Expand Down

0 comments on commit 76f0373

Please sign in to comment.