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
Django 1.7+
requests for Python
Installation
$ pip install django-mailgun-mime
Quick start
Create an account at Mailgun ;
Set up correctly Mailgun`s records at your DNS provider. (Just follow the Mailgun instructions);
Wait until Mailgun will check and approve your settings;
If you fail go back to point 2;
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'
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
Release files for django-mailgun-mime 0.1.7
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| django-mailgun-mime-0.1.7.tar.gz | 4.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_mailgun_mime-0.1.7-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details |
Total release size: 12.6 kB
Release files / django-mailgun-mime-0.1.7.tar.gz
| Download URL | django-mailgun-mime-0.1.7.tar.gz |
|---|---|
| Size | 4.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c4763e755ade78c0ecaa7fee8bc4656ae85bf9542a94c3bd8b558ccf8820bcd3
|
|
BLAKE2b-256 checksum How to use checksums |
39f6f282b55c7ef359a567a6c7eee9d66afff271b8f6ee9e19a75c12a3f9b437
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / django_mailgun_mime-0.1.7-py2.py3-none-any.whl
| Download URL | django_mailgun_mime-0.1.7-py2.py3-none-any.whl |
|---|---|
| Size | 8.1 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
cf77084fd42b36b72c1be3d5722c3191fff9909666a407465598124bdce1b337
|
|
BLAKE2b-256 checksum How to use checksums |
70950117f1741ea3be24ea7835158cd15224de6622bd56d9c4252d614258ba0b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |