Express.js | Developer libraries | Email Verification API | WhoisXML API

Express.js Email Verification client library Express.js Email Verification client library

How to Verify an Email Address in Express.js

If you’re building a website (or API) using Express.js, it’s useful to verify the user’s email address to find out if it’s real and not disposable. The verification process typically involves checking the email in black lists, and checking whether it’s possible to set up a SMTP connection to the mail server. Unfortunately, it isn’t simple.

The reason it isn’t easy to verify an email address is that you have to get answers to a large number of questions. Namely, you need to check the host part of the email address. Is it a real domain name? Does it have DNS records, can you connect to the IPs specified in the DNS record? Can you set up the SMTP connection? Does the service provide disposable email addresses? You can get the answers to all of these questions by one request to Email Verification API.

Today, I will walk you through an incredibly simple example of integrating email-verifier developer library.

Create a Email Verification API Lookup Account

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

Email Verification API is one of the best and least expensive email verification services. You can use the Email Verification API service to perform 1,000 free queries each month, or you can pay a flat fee of $9 per month for 10,000 queries. Other 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, which you will need later to make queries.

Install the Email Verifier Package

Now that your account is set up, the next thing you need to do is install the email verifier NPM library. From the command line, run the following command:

            
$ npm install email-verifier
            
            

This will download and install the latest release of the email-verifier package from NPM.

Perform an Email Verification Request Using Email Verifier

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

Here’s a simple Express.js app that only contains a single endpoint, /users, which returns a simple hello world response:

            
const express = require('express');

const usersRouter = require('./routes/users');

const app = express();

app.use(express.urlencoded({ extended: false }));

app.use(express.json());

app.use('/users', usersRouter);

app.listen(3000);
            
            

You need to put this code into a app.js file and before you can run it, you create a folder “routes” and inside it place the users.js file which will contain the following code.

                    
const express = require('express');
const router = express.Router();
const verifier = new(require('email-verifier'))("Your-api-key");

router.post('/', function(req, res, next) {
    verifier.verify(req.body.email, (err, data) => {
        if (err) {
            console.error(err);
            return res
                .status(500)
                .send({
                    message: 'Internal error'
                });
        }
        console.log(data);
        if (data.formatCheck === 'true' &&
            data.disposableCheck === 'false' &&
            data.dnsCheck === ’true’ &&
            data.smtpCheck !== 'false'
        ) {
            return res.send({
                saved: true
            });
        }
        return res
            .status(400)
            .send({
                message: 'Invalid or disposable email.'
            });
    });
});

module.exports = router;
                    
                

The const verifier = new (require('email-verifier'))("Your-api-key"); line initializes the EmailVerifier class. Now you can use verifier.verify() method in your app. In the code above this method is called when the app receives a POST /users request. The verifier.verify(req.body.email, (err, data) => { line illustrates a verify method call. We assume that req.body.email contains an email address from the user input, so we want to verify it. The callback function has 2 arguments: err and data. If the first one isn’t null, it means that something went wrong. The second argument is a result of the API call. In the following lines we check if the email has a valid format, if the host has DNS records, if the mail server refuses SMTP connection and if the host provides disposable email addresses.

The data argument may store a JavaScript object 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"
    }
}
            
            

Wrap Up Email Verifier

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

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

If you have any questions, please email us!