How to validate email addresses in Python

Our free API to validate e-mail addresses is really easy to use in Python.

 

Validate e-mail in Python with http.client object


import http.client

conn = http.client.HTTPSConnection("mailcheck.p.rapidapi.com")

headers = {
    'x-rapidapi-host': "mailcheck.p.rapidapi.com",
    'x-rapidapi-key': "YOUR-API-KEY"
    }

conn.request("GET", "/?domain=EMAIL-OR-DOMAIN", headers=headers)

res = conn.getresponse()
data = res.read()

print(data.decode("utf-8"))
                

This will return a JSON-array with information about the domain, and if you should block it or not.

 

Validate e-mail in Python with requests object


import requests

url = "https://mailcheck.p.rapidapi.com/"

querystring = {"disable_test_connection":"true","domain":"EMAIL-OR-DOMAIN"}

headers = {
    'x-rapidapi-host': "mailcheck.p.rapidapi.com",
    'x-rapidapi-key': "YOUR-API-KEY"
    }

response = requests.request("GET", url, headers=headers, params=querystring)

print(response.text)
                

 

Verify e-mail in Python with Unirest object


response = unirest.get("https://mailcheck.p.rapidapi.com/?domain=EMAIL-OR-DOMAIN",
  headers={
    "X-RapidAPI-Host": "mailcheck.p.rapidapi.com",
    "X-RapidAPI-Key": "YOUR-API-KEY"
  }
)                

 

You can of course just send it as a regular GET-request to that URL also, in your favorite way, as long as you have the API-key in the header.

You will receive a JSON response, telling you if this is a disposable e-mail to block, or if there are other issues with the domain:

 


{
   "valid": true,
   "block": true,
   "disposable": true,
   "domain":" zhorachu.com",
   "text": "Disposable e-mail",
   "reason": "Heuristics (2a)",
   "risk": 91,
   "mx_host": "mail57.zhorachu.com",
   "mx_info": "Using MX pointer mail57.zhorachu.com from DNS with priority: 10",
   "mx_ip": "109.236.80.110",
   "last_changed_at": "2020-06-11T09:56:02+02:00"
}
               

This will tell you if you should block the domain zhorachu.com or not, and the reason to why the API thinks so (disposable e-mail, etc). You can also see more detailed documentation of the response here.

To get started for free, and get an API key, click here!