Created
May 21, 2017 17:37
-
-
Save radnip/fc15d4763c4333010c16a2602c3f92ca to your computer and use it in GitHub Desktop.
Check that Email domains exist from Google Sheets (MX Lookup).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function MXLookup(domain) { | |
if (domain.length == 0){ | |
return "No Email"; | |
} | |
domain = domain.substring(domain.indexOf("@")+1); | |
//domain = "gmail.com"; | |
try { | |
var url = "https://dns.google.com/resolve?name=%FQDN%&type=MX".replace("%FQDN%",domain); | |
//var url = "https://dns.google.com/resolve?name=e-mercy.com&type=MX"; // USED FOR TESTING ONLY | |
Utilities.sleep(100); | |
var result = UrlFetchApp.fetch(url,{muteHttpExceptions:true}); | |
var rc = result.getResponseCode(); | |
var response = JSON.parse(result.getContentText()); | |
if (rc !== 200) { | |
throw new Error( response.message ); | |
} | |
if (response.Answer[0].data == null) { | |
var mxRaw = response.Authority[0].data; | |
} else { | |
var mxRaw = response.Answer[0].data; | |
} | |
var mx = mxRaw.toLowerCase(); | |
if (mx.indexOf("google.com") >= 0 || mx.indexOf("googlemail.com") >= 0) { | |
var emailProvider = "Yes (Google Apps)"; | |
} | |
else if (mx.indexOf("outlook.com") >= 0) { | |
var emailProvider = "Yes (Office 365)"; | |
} | |
else emailProvider = "Yes"; | |
return emailProvider; | |
} | |
catch (e) { | |
return "ERROR Can't Find ("+domain+"): "+e; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment