Creating email verification function on Google Sheet using GoogleApp Script
Tags: emailverification, googleappscript, tutorial, googlesheet
Published: 2021-12-06 04:32:36

Whether you are managing your customer's data or planning to launch your email marketing campaigns, validating and cleaning customer's email address is a tedious task.

There are many tools available to verify email addresses. But there is nothing as useful as having a 'verify email' function in your Google Sheet. It can save time and make email verification process as easy as using any of the standard Google Sheet's function. 

You can use any email verification API or services, but I will be using BigDataCloud's free email verification API. You can access this API for free by [creating a free account](https://www.bigdatacloud.com/account/) and can use 1,000 queries per month for free. You can find more details about [BigDataCloud's email verification API here](https://www.bigdatacloud.com/packages/phone-email-verification).

So if you have the API endpoints and the API key, let's get started. 

_Note: You need to have a gmail account._

## Step 1: Open a new Google Sheet
![Screenshot Google Sheet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qhmxo8qd4xt5h6ao74th.png)

## Step 2: Click on Apps Script from Extension menu
![Screenshot Google Apps Script](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fw9on8f4z2ve3xj5d6zh.png)
 
This is a console where you will be defining your email verification function. The function defined in this module will be available by default to only this spreadsheet. 

Give the AppsScript a name and define a function.

## Step 3: Define Email Verification Function

![Email Verification Function](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7kd4d7xp12yb0gjhukno.png)

```
function emailIsValid(input) {

var url = 'https://api.bigdatacloud.net/data/email-verify'
  + '?emailAddress=' + input
  + '&key=APIKEY';

var response = UrlFetchApp.fetch(url, {'muteHttpExceptions': true});

// Make request to API and get response before this point.
var json = response.getContentText();
var data = JSON.parse(json);
return data.isValid;
  
}
```

The function above takes a single argument called input (email address) and returns a boolean data. 

Inside the function, we are calling BigDataCloud's API and parsing through its output data to return the value. If you are using any other API, please use their endpoints and the data objects to parse the result. 

Below is the standard output data of BigDataCloud's email verification API.
```
{
"inputData": "email@domain.com",
"isValid": true,
"isSyntaxValid": true,
"isMailServerDefined": true,
"isKnownSpammerDomain": false,
"isDisposable": false
}
```
## Step 4: Run your code

On the toolbar click on Run button.
![Run button](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9jhvh1yg8ur9w448j3mg.png)

Wait for sometime before it starts asking you to provide permission to execute the code. Follow the instruction from the Google and provide necessary permissions.

![Google Appscript Authentication](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/itavxm6rrel1s55w5wgi.png)
 
Once the execution is complete you should see Execution Log as below:
![Execution of GoogleAppscript](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5ug0pfom8xi0rdluih54.png)

Now you are ready to test the function.

## Step 5: Test the function

Go back to your Google sheet.

On the sheet, enter any email address and on different column enter your newly created function as below:

`=emailIsValid(B2)`

![Email Verification Function](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mdr4unhfaqrr50dusfw0.png)
 
Now you can add more email id and simply drag and drop the function to check the email validation. 😎 

![Email Verification](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bn1usr888umbpt8j87pd.gif)
 

## Last step: Add more functions

Now that you have a successfully running email verification function, you can create more functions that provide other important information about the email address. May be you want to check if the email address belongs to a known spammer domain or check if the email address is disposable. 

Check the function definition example below:
 ![More function](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ist49n18y1k67b58nq8t.png)