This space is available for advertisement. Please contact us if interested.
Advertise with us
email api   0   8384
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.

email api   0   8384

Related Articles:
Adding Email Subscription Feature in Django Application
email subscription feature in django, sending email subscription confirmation mail in django, sending email unsubscribe email in Django, Add subscription feature in Django...
How to Track Email Opens Sent From Django App
How to track email opens. Tracking email sent from django app. Finding the email open rate in Python Django. Email behaviour of users in Python Django. Finding when email is opened by user in python-django....
Sending Emails Using Python and Gmail
Python code to send free emails using Gmail credentials, Sending automated emails using python and Gmail, Using Google SMTP server to send emails using python. Python script to automate gmail sending emails, automating email sending using gmail...
Python Script 2 : Crawling all emails from a website
Website crawling for email address, web scraping for emails, data scraping and fetching email adress, python code to scrape all emails froma websites, automating the email id scraping using python script, collect emails using python script...

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.

DigitalOcean Referral Badge

Get a .COM for just $5.98!

© 2021-2022 Python Circle   Contact   Sponsor   Archive   Sitemap   Partner Sites: PythonSnippets.Dev  99Dev.Tools