How to Validate Email Address in Python

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

Email Validation in a nutshell

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

Checking Formatting

You can use a Regular Expression or the email_validator library to check if an Email Address is formatted correctly. For example using in Python.

import re
email_regex = re.compile(r"[^@][email protected][^@]+\.[^@]+")
# "not an email" is invalid so its false.
>>> email_regex.match("not an email") != None
False
# "[email protected]" looks like an email, so it passes even though its not real.
>>> email_regex.match("[email protected]") != None
False
# "[email protected]" passes, gmail is a valid email server,
# but gmail require more than 3 letters for the address.
>>> >>> email_regex.match("[email protected]") != None
True

You could use it in your code like..

pattern_regex_test.py
import re
email_regex = re.compile(r"[^@][email protected][^@]+\.[^@]+")
if email_regex.match("[email protected]"):
print("address is valid")
else:
print("not valid")

But these the Email accounts might not actually exist and if you send an Email to them it might bounce.

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 = "[email protected]"
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 Address might have the unknown status if the email server is unresponsive.

Email Validation Using an API Key

With an API key you wont be limited by how many addresses you can check or how fast you can check them. 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 = "[email protected]"
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': '[email protected]', 'status': 'valid'}

Get started with Real Email validations today.