ScanSkill
Sign up for daily dose of tech articles at your inbox.
Loading

How to Check for Disposable Emails in Node.js

How to Check for Disposable Emails in Node.js
How to Check for Disposable Emails in Node.js

Let’s learn how to check for disposable emails in Node.Js. We will achieve this using the npm package disposable-email-domains.

Introduction

Disposable email is a service that allows registered users to get an email at a temporary address that expires after a certain amount of time. They are popular on websites where users need to register and are required to subscribe to a newsletter. So, to prevent email mailboxes from filling up with unnecessary emails people basically tend to prefer to use these disposable email services.

In cases where you cannot have these kinds of disposable emails in your application, your need to test for these disposable emails. This is where we use the npm package disposable-email-domains to check for these disposable emails.

Prerequisites

  • Basic understanding of Nodejs

Check for Disposable Emails in Node.js

Let’s create a node project and install the required packages.

## creating project folder
$ mkdir disposable-email-test
$ cd disposable-email-test

## Initializing a node project
$ npm init --y

Now, that the project is initialized install the npm package disposable-email-domains, and also create a index.js file that will act as the starting point for the application.

## installing the required package
$ npm install disposable-email-domains

## creating index.js
$ touch index.js

## open with your favoutite code editor
$ code .

The node project is created, let’s get on to the coding part, for that open the index.js file.

//* index.js

const disposableDomains = require("disposable-email-domains");
console.log(disposableDomains);

The disposableDomains variable has an array of domain names that are known to be disposable email domains. Above all, there are 120,000+ disposable mail providers in this list, and new ones are being added to every release of the package. The result we obtain from printing the disposableDomains variable.

Result obtained from printing the disposableDomains array
Result obtained from printing the disposableDomains array

Now that we know the disposableDomains contains an array of disposable mailing domains, let’s wrap it with a JavaScript function. Finally, let’s write a function to check if the provided email is disposable or not.

## index.js

const disposableDomains = require("disposable-email-domains");

function checkEmail(email) {
  const domain = email.split("@")[1];
  let result;
  disposableDomains.includes(domain)
    ? (result = "Disposable")
    : (result = "Not Disposable");
  return result;
}

const safeEmail = "hello@gmail.com";
console.log(`The provided email ${safeEmail} is:`,checkEmail(safeEmail));

//* yopmail a famous disposable mail provider
const unsafeEmail = "hello@yopmail.com";
console.log(`The provided email ${unsafeEmail} is:`,checkEmail(unsafeEmail));

//* gurrillamail another disposable mail provider
const unsafeEmail1 = "qmuz2c+cn2wb9466f27c@guerrillamail.info";
console.log(`The provided email ${unsafeEmail1} is:`,checkEmail(unsafeEmail1));

The checkEmail function takes in email as a parameter, then tests if the email is disposable or not, and returns "Disposable" or "Not Disposable" according to the nature of provided email. Let’s run the code and look at the result.

Result

Check for Disposable Emails in Node.js
Checking if the provided email is disposable or not

From the result, we can see that as long as the provided email is provided is present in the disposable email list it will return the result as Disposable.

Conclusion

So in this article, we learned how to check for disposable emails in Node.Js. We also looked at the results by providing both disposable and non-disposable emails.

Sign up for daily dose of tech articles at your inbox.
Loading