Email Address Validation with External Services

Written by Shruti Sridharan

Categories: Email DIY

4 minute read time

Shruti Sridharan is a full stack Salesforce Developer with close to 3 years of experience working on almost every facet of the platform. She often blogs about everything that’s new on the Salesforce platform. Apart from that, Shruti speaks at various conferences, webinars and also leads the Women-in-Tech User Group and Student Group in her hometown. You can reach out to her via her website.

Are you tired of hearing about Email Bounces from your marketing users? Struggling with email address validation? Are you losing your potential leads because they didn’t give you the correct email address? If you have faced any of the above-mentioned difficulties, I know how it feels like being a Salesforce administrator.

As a Salesforce administrator, you can implement validation rules and other custom validations to ensure the quality of the email address. But at the end of the day, these options have their own limitations. So how can we resolve this trivial problem in an Admin friendly manner? In other words, with less or no code at all. An Email Address Validation API does all the hard work for you.

 

Email Address Validation APIs

I know you have probably started to freak out when you read the word API. No, don’t be. There are a lot of services out there that offers email address validations. One of them that got my attention is the Mailgun email validations tool.

Let’s look at integrating Mailgun’s Email Validation API with Salesforce using External Services. In case if you want to know more about External Services in Salesforce, I would suggest you take a look at this blog series.

Without further ado, let’s start building it!

 

Exploring the Mailgun Email Address Validation API

First, be sure you sign-up for Mailgun if you haven’t already. Once you have an account, navigate over to the Dashboard. Now, click on the Email Validation tab and copy the Public Validation Key as shown below –

Now, let’s look at the API that needs to be fired with the Public Validation Key and Email Address for validation. The API endpoint that needs to be used for the validation is shown below as an example –

https://api.mailgun.net/v3/address/validate?address=shruti.sridharan22@gmail.com&api_key=<PASTE THE PUBLIC VALIDATION KEY HERE>

Simply supply the address and the api_key as URL parameters and you should be golden!

The response upon invoking the API successfully would look like this:

{

    "address": "shruti.sridharan22@gmail.com",

    "did_you_mean": null,

    "is_disposable_address": false,

    "is_role_address": false,

    "is_valid": true,

    "mailbox_verification": "true",

    "parts": {

        "display_name": null,

        "domain": "gmail.com",

        "local_part": "shruti.sridharan22"

    },

    "reason": null

What’s important here is the is_valid node. A value of true indicates that the Email Address is correct.

Well, that’s with the API. In order to invoke this from Salesforce, we will employ External Service. But for External Service to work, we need to write a Swagger Schema for the same. Let’s take a stab at it.

 

Swagger Schema for the Mailgun Email Address Validation API

Here is your Swagger Schema for the above-mentioned API —

{

    "swagger": "2.0",

    "info": {

        "description": "Mailgun Email Address Validation API",

        "version": "3.0",

        "title": "Mailgun Email Address Validation API"

    },

    "host": "api.mailgun.net",

    "schemes": [

        "https"

    ],

    "paths": {

        "/v3/address/validate": {

            "get": {

                "summary": "Validate an Email Address",

                "produces": [

                    "application/json"

                ],

                "parameters": [

                    {

                        "in": "query",

                        "name": "address",

                        "description": "Email Address to be Verified",

                        "required": true,

                        "type": "string"

                    },

                    {

                        "in": "query",

                        "name": "api_key",

                        "description": "API Key",

                        "required": true,

                        "type": "string"

                    }

                ],

                "responses": {

                    "200": {

                        "description": "Result of the Email Address Validation",

                        "schema": {

                            "$ref": "#/definitions/validationResponse"

                        }

                    }

                }

            }

        }

    },

    "definitions" : {

        "validationResponse" : {

            "properties": {

                "is_valid": {

                    "description": "is_valid",

                    "type": "boolean"

                }

            }

        }

    }

}

Not so bad, right? If you are NOT able to decipher that (which you really don’t need to), you can take a brief look at the previous External Service blog posts.

However, a new addition here is the introduction of complex schema/object as the output. The return type when the API call was successful (200) looks like this:

"validationResponse" : {

    "properties": {

        "is_valid": {

            "description": "is_valid",

            "type": "boolean"

        }

    }

}

As mentioned earlier, since we are looking only at the is_valid node, we don’t really have to care about the other nodes in the JSON response.

 

Registering the External Service

Before we register, make sure we have a Named Credential created with this URL  —

https://api.mailgun.net

Now, switch to Lightning Experience and from the Setup, click on External Services. Hit Add an External Service and then copy and paste the Swagger Schema after selecting the checkbox – Provide Complete Schema. In the end, you should be able to see something like this: 

Voila! That’s completing about 70% of the whole thing. Registering an API is so simple that it hurts.

 

Creating a Flow to use the External Service

Let me break the suspense right away! Your Flow is as simple as this — 

Let’s look at each of the elements in the Flow.

1. Screen – Email Address Validation

The screen will just have a TextBox.

2. Apex Action (available in Flow as result of the External Service registration)

Set the address to the Screen Input Field and the api_key would be the Public Validation Key.

Wait! We need to configure the Outputs also. The response from the API (out of which we are looking ONLY for the is_valid) will be collected in a Variable of type – Boolean.

 

3. Screen – Validation Result

All you need to do is to add a Display Text and print the value within the Variable that was created in Step 2 to hold the response from the API.

That’s all! Now let’s see it in action.

 

Flow in Action

We can always expand the Flow by updating a checkbox on the Lead record or so to indicate the validation status. Trust me – API Integration has never become so simple!

Tags: | | |

Modified on: December 7, 2018

Stay up-to-date with our blog & new email resources

We'll let you know when we add new email resources and blog posts. We promise not to spam you.