Skip to content
NodeJS library for checking MX records exist on a domain
Branch: master
Clone or download
Latest commit 2013eb0 Jun 4, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
example both valid and invalid should return in the then part of the promise Apr 23, 2018
src update error and bump to 1.0.5 Sep 17, 2018
test update test Apr 1, 2019
.gitignore Update to use Promises Apr 23, 2018
.travis.yml Update travis.yml Jun 4, 2019
LICENSE Create LICENSE Jun 4, 2019
package.json update error and bump to 1.0.5 Sep 17, 2018
readme.md update readme Apr 1, 2019

readme.md

Legit

Travis CI

A wrapper for the NodeJS Dns.resolveMx method that checks the domain of an email address for valid/existence of MX records.

Installation

$ npm install legit

Usage

const legit = require('legit');

legit('validemail@validdomain.com')
  .then(result => {
    result.isValid ? console.log('Valid!') : console.log('Invalid!');
    console.log(JSON.stringify(result));
  })
  .catch(err => console.log(err));

If an email addresses domain is legit then the object returned will include an isValid key that will be set to true as well as an mxArray key with all the MX record information for the valid domain.

If the domain has no MX or cannot resolve any MX then it will return isValid as false.

Anything else is considered an error and you'll get it in the .catch

Async/Await Usage

For a more modern approach using ES6, you can await the reponse before acting on it.

const legit = require('legit');

(async () => {
  try {
    const response = await legit('validemail@validdomain.com');
    response.isValid ? console.log('valid') : console.log('invalid');
  } catch (e) {
    console.log(e);
  }
})();

Example Response

For a valid email address, you'll get the following response object:

{
  "isValid": true,
  "mxArray": [
    {
      "exchange": "aspmx.l.google.com",
      "priority": 1
    },
    {
      "exchange": "alt1.aspmx.l.google.com",
      "priority": 5
    },
    {
      "exchange": "alt2.aspmx.l.google.com",
      "priority": 5
    },
    {
      "exchange": "alt3.aspmx.l.google.com",
      "priority": 10
    },
    {
      "exchange": "alt4.aspmx.l.google.com",
      "priority": 10
    }
  ]
}

License

(The MIT License)

Copyright (c) 2015-2019 Martyn Davies, and contributors.

You can’t perform that action at this time.