how-to email api   0   811
How to send bulk emails for free using Mailgun and Python Django

Consider these scenario in your newly developed Django Application:

  • You need to send promotional emails to all the registered users.
  • You need to send email confirmation link or password reset link.
  • You need to send weekly newsletter to you readers.

What do you choose? Free Gmail Account to send email using python code? That is a nice idea provided you want to look unprofessional and you need not to send more than 500 emails per day.

But if you want professional look in your emails [from address something like instead of and do not want the limit on per day email count, then this article is for you.

Mailgun Account - Domain, Login and API Key:

We will be using Mailgun API to send emails instead of

First create a free account with Mailgun. You will get to choose the payment information while signing up. If you do not add payment info then you can send emails to verified recipients only.

After you are done with signup process, you can see your api key and other details in dashboard.

By default you are assigned a domain name to use while sending email which should look like as below: You will be able to see your domain information by clicking on domain link.

how to send bulk emails for free using mailgun and python django

We will be using above details in our Django code to send sample email. Remember you can send email to verified recipients only from this default mailgun domain.

Add one of your email Id to verified recipients list. Similarly you can add your own domain in mailgun and verify that by following the process given in mailgun documentation. With a verified custom domain you can send email to anyone.

Python-Django Code to send email using mailgun API:

Use below code to send email using mailgun api:

import requests

def send_email(data):
        url = ""
        status =
            auth=("api", "key-23bhxxxxxxx"),
            data={"from": "FROM-NAME <>",
                  "to": ""],
                  "subject": "Test Email",
                  "text": PLAIN-TEXT,              
                  "html": HTML-TEXT}
        return status
    except Exception as e:
        raise e

Replace the variables in CAPITAL LETTERS with actual value. PLAIN-TEXT  and HTML-TEXT can be generated from template as below.

from django.utils.html import strip_tags
from django.template import Context, Template
from django.template.loader import get_template

template = get_template("myapp/sample_template.html")
context = Context(context_data)
body_html = template.render(context_data) #HTML-TEXT			
body_text = strip_tags(body_html) #PLAIN-TEXT

If mail is accepted by Mailgun to send, you will receive 200 as status code.

if status.status_code == 200:
    # do something. update DB

So build Django app and send your first email.

Please comment if something is not working for you.

how-to email api   0   811

Related Articles:
Python Script 3: Validate, format and Beautify JSON string Using Python
Validating json using python code, format and beautify json file using python, validate json file using python, how to validate, format and beautify json...
Encryption-Decryption in Python Django
How to encrypt and decrypt the content in DJango. Encrypting the critical information in Django App. Encrypting username, email and password in Django...
Adding Robots.txt file to Django Application
Adding robots.txt file in your Django application. Easiest way to add robots.txt file. Django application robots.txt file. Why should you add robots.txt file in your Django Application....
Scraping 10000 tweets in 60 seconds using celery, RabbitMQ and Docker cluster with rotating proxy
Scrapping large amount of tweets within minutes using celery, RabbitMQ and docker cluster. Scraping huge data quickly using docker cluster with TOR....

0 thoughts on 'How To Send Bulk Emails For Free Using Mailgun And Python Django'
Leave a comment:

*All Fields are mandatory. **Email Id will not be published publicly.

Please subscribe to get the latest articles in your mailbox.

Recent Posts:

© 2018-2019
Contact Us: code108labs [at]
Address: 3747 Smithfield Avenue, Houston, Texas