How to send bulk emails for free using Mailgun and Python Django

how to send bulk email 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.

Domain details in mailgun

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:

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

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

 

So build Django app and send your first email. Please comment if something is not working for you.

 

(Visited 1,082 times, 1 visits today)

You must read this :

Leave a Reply

Your email address will not be published. Required fields are marked *