Coder Social home page Coder Social logo

django-mailgun's Introduction

Django-Mailgun

A Django email backend for use with Mailgun

Overview

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

Getting going

Install django-mailgun:

pip install django-mailgun

Add the following to your settings.py:

EMAIL_BACKEND = 'django_mailgun.MailgunBackend'
MAILGUN_ACCESS_KEY = 'ACCESS-KEY'
MAILGUN_SERVER_NAME = 'SERVER-NAME'

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 (https://documentation.mailgun.com/user_manual.html#batch-sending). 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 protected]', [[email protected], [email protected]])
email.extra_headers['recipient_variables'] = '{"[email protected]":{"first_name":"Joe"}, "[email protected]":{"first_name":"Jane"}}'
email.send()

When Jane receives her email, its body should read 'Cool message for Jane', and Joe will see 'Cool messagae 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 https://documentation.mailgun.com/api-sending.html#sending). These options can also be passed to Mailgun's SMTP server (see "Passing Sending Options" under https://documentation.mailgun.com/user_manual.html#sending-via-smtp). 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 protected]', [[email protected]])
email.extra_headers['X-Mailgun-Tag'] = ['Tag 1', 'Tag 2']
email.send()

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.

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 (https://documentation.mailgun.com/user_manual.html#tagging). 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

django-mailgun's People

Contributors

alex avatar bradwhittington avatar bryanhelmig avatar frankwiles avatar ghinch avatar omidraha avatar pydanny avatar ryneeverett avatar sausaw avatar sonthonaxrk avatar vstoykov avatar wmsmith avatar zakdoek avatar zdavisk avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.