How to Validate Email Address in Python

Check if an email address is correct and really exists using the Python programming language and Real Email

Validation in a nutshell

An email address can look right but still be wrong. Real email uses in depths address validation to check if emails really exist without sending any messages.

Checking Formatting

You can use a regular expression or email_validator to check if an email address is formatted correctly. For example using in python.

pattern_regex_test.py
import re
if re.match(r"[^@]+@[^@]+\.[^@]+", "foo@bar.com"):
print("address is valid")
else:
print("not valid")

Real Email API validation

To check if an address really exists you can use the Real Email API which does in depth email address inspection on the email server. In this example we use the python requests library.

real_email_validation.py
import requests
email_address = "foo@bar.com"
response = requests.get(
"https://isitarealemail.com/api/email/validate",
params = {'email': email_address})
status = response.json()['status']
if status == "valid":
print("email is valid")
elif status == "invalid":
print("email is invalid")
else:
print("email was unknown")

You will be able to test 100 emails per day for free. If you have more that that you will need to signup and get an API key. An email might be 'unknown' if the email server is unresponsive.

Using API Key

With an API key you wont be limited by how many addresses you can check. In python the example changes to add the API key.

real_email_validation_with_key.py
import requests
api_key = "" // todo put your api key here
email_address = "foo@bar.com"
response = requests.get(
"https://isitarealemail.com/api/email/validate",
params = {'email': email_address},
headers = {'Authorization': "Bearer " + api_key })
status = response.json()['status']
if status == "valid":
print("email is valid")
elif status == "invalid":
print("email is invalid")
else:
print("email was unknown")

Bulk Email Address Validation with CSV file

Depending on your use case you may like to use bulk csv file validation and read the csv file with python. This is better if you have one big list of emails to check rather than an on going process. First upload your csv file to Real Email, when it is validated you can read the result file with python like below.

import csv
with open('emails-validated.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
print(row)
# {'email': 'foo@bar.com', 'status': 'valid'}

Get started with real email validations today.