Skip to content
This project is deprecated in favor of
Python Makefile
Branch: master
Clone or download

Latest commit

Fetching latest commit…
Cannot retrieve the latest commit at this time.


Type Name Latest commit message Commit time
Failed to load latest commit information.
.coveragerc Increased coverage to 50% Sep 7, 2015
.editorconfig Many things for the 0.3.0 release. Sep 5, 2015
.gitignore Many things for the 0.3.0 release. Sep 5, 2015
.travis.yml Many things for the 0.3.0 release. Sep 5, 2015 Fix year for recent CHANGELOG notes May 4, 2016
CONTRIBUTING.rst Update CONTRIBUTING.rst May 4, 2016
CONTRIBUTORS.rst Bump for 0.9.0 release May 3, 2016
LICENSE License info Mar 16, 2011 License info Mar 16, 2011
Makefile Corrected versioning Oct 9, 2015
README.rst Update README.rst May 4, 2016 Replace missing paranthesis. May 3, 2016
setup.cfg Added @ghinch to contributors and various flake8 and cleanup tasks Sep 9, 2015 Bump for 0.9.1 May 3, 2016 Prep for 0.7.0 release Sep 22, 2015


This project is unsupported. Please use instead.


A Django email backend for use with Mailgun


Django-Mailgun is a drop-in mail backend for Django.

Getting going

Install django-mailgun:

pip install django-mailgun

Add the following to your

EMAIL_BACKEND = 'django_mailgun.MailgunBackend'

Replace ACCESS-KEY with the "API-KEY" value from your Mailgun account details and SERVER-NAME with the last part of your "API Base URL" (eg.<your_server_name>), also found in your Mailgun account details.

Now, when you use django.core.mail.send_mail, Mailgun will send the messages.

Extra features

Passing user-specific data

Mailgun also includes the ability to send emails to a group of recipients via a single API call ( To make use of this, you need to pass Recipient Variables along with your API call. To do so with Django-Mailgun, add a valid JSON string to the extra_headers attribute of EmailMessage and Django-Mailgun will remove the string from the headers and send it appropriately. For example:

email = EmailMessage('Hi!', 'Cool message for %recipient.first_name%', '', [,])
email.extra_headers['recipient_variables'] = '{"":{"first_name":"Joe"}, "":{"first_name":"Jane"}}'

When Jane receives her email, its body should read 'Cool message for Jane', and Joe will see 'Cool message for Joe'.

Analytics and other tracking features

Mailgun provides the ability to track certain events that concern your emails. The API exposes these options (see These options can also be passed to Mailgun's SMTP server (see "Passing Sending Options" under If you add any of the SMTP options to the extra_headers attribute of EmailMessage, Django-Mailgun will map those values over to the appropriate API parameter. For example:

email = EmailMessage('Hi!', 'Cool message for Joe', '', [])
email.extra_headers['X-Mailgun-Tag'] = ['Tag 1', 'Tag 2']

When the email is sent, it will be tagged with 'Tag 1' and 'Tag 2'. You can provide a string for any value, or a list or tuple that contains strings for options that can take multiple values.

Attaching data to messages

When sending, you can attach data to your messages by passing custom data to X-Mailgun-Variables header (see Data should be formatted as JSON, and it will be included in any webhook event releated to the email containing the custom data. For example:

email = EmailMessage('Hi!', 'Cool message for Joe', '', [])
email.extra_headers['X-Mailgun-Variables'] = {'my-id': 'email_id', 'my-variable':'variable'}

Later, you can read this data in your Mailgun webhook handler. For example:

def mailgun_webhook(request):
    email_id ='my-id')
    my_variable ='my-variable')

    # Do something with your variables

    return Response(status=status.HTTP_200_OK)

NOTE: Django-Mailgun does NOT validate your data for compliance with Mailgun's API; it merely maps over whatever values you provide. For example, Mailgun's API states that no more than 3 tags are allowed per email, and each tag must be no greater than 128 characters ( If you provide 4 tags, or a tag longer than 128 characters, Django-Mailgun will attempt to send such (potentially) invalid data. You must ensure what you send is appropriate.

Django Email Backend Reference

You can’t perform that action at this time.