TUTORIAL
Send Email from a custom domain with Mailgun API using Python
Send Email from a custom domain with Mailgun API using Python
Intro
There are plenty of tutorials on the Internet teaching how to send an Email using Python. Why bothering with another tutorial talking about sending Email with a custom domain? Well at this point, I agree with you that you’re right. But this tutorial is aiming for the folks who might be running a professional online business or website and looking for possibilities of sending customers and marketing emails in an easy and scalable manner while being able to maintain their brandings (Such as using their domains or sub-domains). Luckily, Mailgun makes this way a lot easier to do this.
So, what exactly is Mailgun?
Mailgun is a service that offers you an easy-to-use API (Application Programming Interface) in many programming languages(Python, Go, Curl and Ruby etc.) to send emails scalably and at the same time, abstracts away the complex server credentials or SSL logics with just a simple post request to do the all the heavy liftings for you.
Step 1 - Sign up for a Mailgun account( Credit card required - Monthly free usage up to 10000 emails. !!!)
Go to the Mailgun sign up page for account sign-up. The sign-up process is pretty straight-forward. If you just follow the instructions there, you will be most likely just fine.
Step 2 - Log in and Add a Domain/Sub Domain
After Sign up process, you can log in in the Mailgun Login Page. After logging in, please go to Domains Section in the Dashboard.
Then you need to hit the “Add New Domain” button to add a new domain or a subdomain, in the new page, you can add a SUBDOMAIN like “example.domain.com” or your main DOMAIN “domain.com”.
Howevever, it’s recommended by Mailgun that you should add a subdomain to avoid any potential DNS records conflicting with your current DNS records under your MAIN domain.
Let’s assume in this case, you added a SUBDOMAIN as “subdomain.domain.com”. After adding the new sub domain, you will be directed to the DNS Verifying instructions page.
In this text-basded tutorial, I won’t dive into details of how to verify your domain in your DNS providers. As the case might vary that each DNS provider is slightly different, so if you have struggles following Mailgun instructions how to verify your domain. Please reach out to me via the comment section of YouTube video of this tutorial, I will be happy to help!!!
### How to proceed with code?
1. Get your API Key and API Base URL
After your domain name is verified by Mailgun, you can go to the credentials page of your domain at https://app.mailgun.com/app/domains/yourdomain.com to view the credentials.
In the “Domain Information” section, your API Key and API Base URL will be available to you and you will need them later in your Python code.
2. Code it here
In order this to work, you will need “requests” pip installed in your computer.:
& pip install requests
import requests
# Instantiate a post request
requests.post(
# Here goes your Base API URL
"https://api.mailgun.net/v3/YOURDOMAIN/messages",
# Authentication part - A Tuple
auth=("api", "your api key"),
# mail data will be used to send emails
data={"from": "Your Name <[email protected]>",
"to":["[email protected]"], # passing a list or a signle email address with string data type.
"subject": "Testing some awesomeness of Mailgun",
"text":" Mailgun test on the first day of 2019."}
)
Send with cc and bcc
requests.post(
# Here goes your Base API URL
"https://api.mailgun.net/v3/YOURDOMAIN/messages",
# Authentication part - A Tuple
auth=("api", "your api key"),
# mail data will be used to send emails
data={"from": "Your Name <[email protected]>",
"to":["[email protected]"], # passing a list or a signle email address with string data type.
"cc":["[email protected]"],
"bcc":["[email protected]"],
"subject": "Testing some awesomeness of Mailgun",
"text":" Mailgun test on the first day of 2019."}
)
If your code prints a “Response [200]” message, your message is then successfully sent.