Skip to content

titoncio/deep-email-validator

 
 

Repository files navigation

Email Validator

Validates email addresses based on regex, common typos, disposable email blacklists, DNS records and SMTP server response.

  • Identify strings that looks like an email using the isEmail function (i.e. contains an "@" and a "." to the right of it).
  • Validates email address using a regex with options to change parsing strictness, see parse options.
  • Validates common typos e.g. [email protected] using mailcheck.
  • Validates email was not generated by disposable email service using disposable-email-domains.
  • Validates MX records are present on DNS.
  • Validates SMTP server is running.
  • Validates mailbox exists on SMTP server.
  • Native typescript support.

Getting Started

Compatible with nodejs only. Not browser ready.

Install like so

npm i deep-email-validator --save

or with yarn

yarn add deep-email-validator

Use like so

import { validate } from 'deep-email-validator'
const main = async () => {
  let res = await validate('[email protected]')
  // {
  //   "valid": false,
  //   "reason": "smtp",
  //   "validators": {
  //       "regex": {
  //         "valid": true
  //       },
  //       "typo": {
  //         "valid": true
  //       },
  //       "disposable": {
  //         "valid": true
  //       },
  //       "mx": {
  //         "valid": true
  //       },
  //       "smtp": {
  //         "valid": false,
  //         "reason": "Mailbox not found.",
  //       }
  //   }
  // }

  // Can also be called with these default options
  await validate({
    email: '[email protected]',
    sender: '[email protected]',
    validateRegex: true,
    validateMx: true,
    validateTypo: true,
    validateDisposable: true,
    validateSMTP: true,
  })
}

If you want to validate domains with TLDs that are not supported by default, you can use additionalTopLevelDomains option:

await validate({
  email: '[email protected]',
  sender: '[email protected]',
  validateRegex: true,
  validateMx: true,
  validateTypo: true,
  validateDisposable: true,
  validateSMTP: true,
  additionalTopLevelDomains: [ 'ir' ]
})

For a list of TLDs that are supported by default you can see here.

Important

You must enable validateRegex for other validations to be reliable.

Parse Options

The email address specification is quite complex and there are multiple conflicting standards, the default options are based on WhatWG recommendation which is the reference for form validation in most modern browsers. However depending on your use case you may want to override these options:

export type ParseEmailOptions = {
  // Allow RFC 5322 angle address such as '"Name" <email@domain>'
  // use this option if you want to parse emails from headers or envelope addresses
  allowAngle?: boolean,
  // Allow RFC 5322 quoted email address such as '"[email protected]"@gmail.com'
  // use this option if you want to accept lesser known email address formats
  allowQuoted?: boolean,
  // Reject addresses containing "+", which is used for subaddressing
  // use this option to enforce one email per user
  rejectSubaddressing?: boolean,
};

Default options can be found here

About

Validates regex, typos, disposable, dns and smtp

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 98.5%
  • JavaScript 1.5%