Skip to main content

It is a tiny wrapper for Django that allows to send mail via Mailgun`s API.

Project description

It is a tiny wrapper for Django that allows to send mail via Mailgun`s API. For the reference of Mailgun`s API, please visit api for sending .

For the Django`s email implementation, please visit sending email .

This wrapper sends request to Mailgun`s mime API to efficiently use Django`s EmailMessage instance. In this case we just put mime message, prepared by Django`s EmailMessage instance, and attach it as a file. After receiving your message Mailgun`s API will do the rest of job.

This wrapper also supports Mailgun`s extra headers, which can be very useful. After wrapper finds such header it places one to request’s data. Please notice that wrapper does not validate Mailgun’s extra headers. If does so it won’t be simple and tiny anymore. :-)

Requirements

  1. Django 1.7+

  2. requests for Python

Installation

$ pip install django-mailgun-mime

Quick start

  1. Create an account at Mailgun ;

  2. Set up correctly Mailgun`s records at your DNS provider. (Just follow the Mailgun instructions);

  3. Wait until Mailgun will check and approve your settings;

  4. If you fail go back to point 2;

  5. Set up following variables at yours project settings:

EMAIL_BACKEND = 'django_mailgun_mime.backends.MailgunMIMEBackend'
MAILGUN_API_KEY = 'your api key'
MAILGUN_DOMAIN_NAME = 'your.domain'
# optionally:
DEFAULT_FROM_EMAIL = 'postmaster@your.domain'
SERVER_EMAIL = 'admin@your.domain'
  1. Now you can send email via Mailgun API. Also you can send email through Mailgun SMTP service. In this case just use Django`s SMTP backend.

Examples

Firstly, we need to import some dependencies:

from django.template.loader import render_to_string
from django.conf import settings
from django.core.mail import send_mail, EmailMultiAlternatives
from django.core.mail import get_connection

A simple email sending

# We will use these paths ahead
path_to_txt = 'path/to/template.txt'
path_to_html = 'path/to/template.html'

s = 'Simple test for Mailgun awesomeness'
m = 'Congratulations! Now you know Mailgun awesomeness!'
context = {'title': s, 'message': m}

txt = render_to_string(path_to_txt, context)
html = render_to_string(path_to_html, context)
send_mail(s, txt, settings.DEFAULT_FROM_EMAIL, ['to@example.com'], html_message=html)

Mailgun extra headers and file attachment

s = 'Testing Mailgun awesomeness!'
m = 'Message content for email with tracking.'
context = {'title': s, 'message': m}

txt = render_to_string(path_to_txt, context)
html = render_to_string(path_to_html, context)

msg = EmailMultiAlternatives(s, txt, settings.DEFAULT_FROM_EMAIL,
                             ['to@example.com'])
msg.attach_alternative(html, 'text/html')
msg.attach_file('path/to/file')
msg.extra_headers['o:tracking-opens'] = 'yes'
msg.extra_headers['h:Reply-To'] = 'from@example.com'
msg.send()

Specifying connection

api_key = 'API_KEY_FROM_MAILGUN'
domain = 'yours.domain.name.checked.and.set.at.mailgun'
connection = get_connection('django_mailgun_mime.backends.MailgunMIMEBackend',
                            api_key=api_key,
                            domain=domain)

s = 'Testing specific connection!'
m = 'Well... You receive it. What now?'
context = {'title': s, 'message': m}

txt = render_to_string(path_to_txt, context)
html = render_to_string(path_to_html, context)

send_mail(s, txt, settings.DEFAULT_FROM_EMAIL, ['to@example.com'],
                 connection=connection, html_message=html)

License

ImLink

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-mailgun-mime-0.1.5.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

django_mailgun_mime-0.1.5-py2.py3-none-any.whl (8.2 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django-mailgun-mime-0.1.5.tar.gz.

File metadata

File hashes

Hashes for django-mailgun-mime-0.1.5.tar.gz
Algorithm Hash digest
SHA256 c69384857ec7d43afb168252968783d67d132c2ee20abeb6ac4da4bd035a9d27
MD5 f0b8fa1029b7c295a9ee467970589bb3
BLAKE2b-256 c09d0913ee4639d6e37b9ca3929087879df9d8665e266e0cf042a6b246e6df42

See more details on using hashes here.

File details

Details for the file django_mailgun_mime-0.1.5-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_mailgun_mime-0.1.5-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 c7bff41fe31a804503319e19120cded921f311f5cde95e58f485882ea4d9e4b5
MD5 00b634eeea9242c61129ce23ac28dec6
BLAKE2b-256 1bd424b3115106bb4630f98faeb537bef6ff9d5a6d276ef0feabd35cee7a7cd1

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page