Flask | Developer libraries | Email Verification API | WhoisXML API

Flask Email Verification client library Flask Email Verification client library

How to perform Email Verification in Flask

If you’re building a website (or API) using Flask, it’s often a good idea to verify your users’ email addresses. The process of verification typically involves retrieving email address information, and it isn’t simple.

What makes it difficult is that there is no public database to map email address on whether this email provider is trusted enough or even whether this user exists on the service. Most companies verify user’s emails by sending a confirmation email, but the most significant drawback of this method is that they often get emails to bounce because the user specified a non-existent email address.

Email address info is typically comprised of:

  • Format validation of a given email address;
  • Black lists of domains with disposable emails;
  • Checking DNS records for the domain part;
  • Checking SMTP connection to the server;
  • Checking if this domain’s mailboxes are free.

In short, you need to make lots of steps to fully verify an email address.

Today, we’ll walk you through using the incredibly simple Flask-EmailVerifier developer library we created, which makes performing email verification requests a breeze in Flask.

Create an Email Verification API Account

The first thing you'll need to do to use the Flask-EmailVerifier library is go create a free Email Verification API account: https://emailverification.whoisxmlapi.com/signup.

Email Verification API is one of the largest and least expensive providers of email addresses data. You can use the Email Verification API service to perform 1,000 free email verification queries each month, or you can pay them a flat fee of $49 per month for 100,000 queries. Extra tariff plans are available here.

Once you've created and logged into your Email Verification API account, you'll need to view your account's products page and copy your API key — you will require that later to make email verification requests.

Install the Flask-EmailVerifier Package

Now that your account is set up, the next thing you do is install the Flask-EmailVerifier PyPi library. From the command line, run the following command in the root of your project:

            
$ pip install Flask-EmailVerifier
            
            

This will download and install the latest release of the Flask-EmailVerifier package from PyPi.

Perform a Email Verification Request Using Flask-EmailVerifier

Now that you have both an account and the Flask-EmailVerifier package installed, let’s take a look at some code you can run to look up various email address.

Here’s a simple Flask app that only contains a single endpoint,'/email/<email>', which returns a simple response with an API request result:

            
from flask import Flask, make_response
from flask_email_verifier import EmailVerifier
from json import dumps, loads

app = Flask(__name__)
# Initialize the extension
verifier = EmailVerifier(app)

@app.route('/email/<email>')
def email(email):
    # Retrieve an info for the given email address
    email_address_info = verifier.verify(email)
if email_address_info is not None:
    data = dumps(loads(email_address_info.json_string), indent=4)
    resp = make_response(data, 200)
    resp.headers['Content-Type'] = 'application/json'
else:
    resp = make_response('None', 404)
    return resp
            
            

You need to take some more steps before you are able to run our simple application.

  • Specify a 'FLASK_APP' environment variable ( export FLASK_APP=app.py)
  • Specify a 'EMAIL_VERIFIER_KEY' environment variable with the API key ( export EMAIL_VERIFIER_KEY=’your-api-key’)

Put this code into the 'app.py' file and run it, and you should see a JSON response with the info about test.email@gmail.com when you visit the '/test/test.email@gmail.com' endpoint in your browser.

If you run this new server and visit the '/email/test.email@gmail.com' URL in your browser, you should see a new response that looks something like this:

            
{
    "emailAddress": "test.email@gmail.com",
    "formatCheck": "true",
    "smtpCheck": "false",
    "dnsCheck": "true",
    "freeCheck": "true",
    "disposableCheck": "false",
    "catchAllCheck": "false",
    "mxRecords": [
        "alt3.gmail-smtp-in.l.google.com",
        "alt1.gmail-smtp-in.l.google.com",
        "alt2.gmail-smtp-in.l.google.com",
        "alt4.gmail-smtp-in.l.google.com",
        "gmail-smtp-in.l.google.com"
    ],
    "audit": {
        "auditCreatedDate": "2018-11-14 13:05:09.000 UTC",
        "auditUpdatedDate": "2018-11-14 13:05:09.000 UTC"
    }
}
            
            

As you can see, the response data object contains all of the email data! Pretty neat, isn’t it? The response object contains everything you need to know about a given email address.

Wrap Up Flask-EmailVerifier

Performing email verification can be tricky, but Flask-EmailVerifier in conjunction with the Email Verification API service makes it simple – and cheap! By using the new Flask-EmailVerifier library you can easily build and manage email verification for even the largest enterprise sites.

To learn more, check out the Flask-EmailVerifier library on GitHub where you can find all the docs and more in-depth information: https://github.com/whois-api-llc/flask-email-verification

If you have any questions, please email us!