Blog available for sell
This blog is available for sale. Please 'contact us' if interested.
Advertise with us
email api   0   12719
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 admin@yourwebsite.com instead of something@gmail.com 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 smtp.gmail.com.

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: sandbox2bb5d65ecfXXXXXXa32fXXXXXX93XXXa.mailgun.org 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):
    try:
        url = "https://api.mailgun.net/v3/YOUR-DOMAIN-NAME/messages"
        status = requests.post(
            url,
            auth=("api", "key-23bhxxxxxxx"),
            data={"from": "FROM-NAME <mailgun@sandbox2bxxxxxxxxxxxxxxx67xx.mailgun.org>",
                  "to": "me@example.com"],
                  "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   12719
0 comments on 'How To Send Bulk Emails For Free Using Mailgun And Python Django'
Login to comment


Related Articles:
Accessing Gmail Inbox using Python imaplib module
In this article, we are accessing Gmail inbox using IMAP library of python, We covered how to generate the password of an App to access the gmail inbox, how to read inbox and different category emails, how to read promotional email, forum email and updates, How to search in email in Spam folder, how to search an email by subject line, how to get header values of an email, how to check DKIM, SPF, and DMARC headers of an email...
Sending email with attachments using Python built-in email module
Sending email with attachments using Python built-in email module, adding image as attachment in email while sending using Python, Automating email sending process using Python, Automating email attachment using python...
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...
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...
DigitalOcean Referral Badge

© 2024-2025 Python Circle   Contact   Sponsor   Archive   Sitemap