Skip to content

Instantly share code, notes, and snippets.

@ciaranmahoney
Last active November 21, 2023 23:09
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ciaranmahoney/414ad3252212f37c48652a8fb724d89f to your computer and use it in GitHub Desktop.
Save ciaranmahoney/414ad3252212f37c48652a8fb724d89f to your computer and use it in GitHub Desktop.
Google Apps Script to Run MX Lookups on domains from Google Sheets
function MXLookup(domain) {
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 = "Google Apps";
}
else if (mx.indexOf("outlook.com") >= 0) {
var emailProvider = "Office 365";
}
else emailProvider = "Other";
return emailProvider;
}
catch (e) {
return "ERROR";
}
}
function MXLookupArray(input) {
if (input.map) {
return input.map(MXLookup);
} else {
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
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 = "Google Apps";
}
else if (mx.indexOf("outlook.com") >= 0) {
var emailProvider = "Office 365";
}
else emailProvider = "Other";
return emailProvider;
}
catch (e) {
return "ERROR";
}
}
}
@ciaranmahoney
Copy link
Author

ciaranmahoney commented Dec 9, 2016

Just copy the script into the Google Sheets script editor (from the Tools menu), save the script, then use functions mxlookup() or mxlookuparray() to run the lookups.

mxlookuparray accepts a range of cells - eg =mxlookuparray(A1:A500) - while mxlookup just accepts one cell at a time - eg, =mxlookup(A1). For larger lists use the array function - it seems to be able to handle around 1000 rows at a time.

It requires a naked domain (no www/http, etc). So you can use a regex formula to convert full domains to naked. I have a Gist for that here: https://gist.github.com/ciaranmahoney/046a6c1cfd9e12eb37a9adea34a5e7df

@ciaranmahoney
Copy link
Author

Added better error handling and a sleep function to the standard mxlookup to better handle larger data sets.

@RomainLapeyre
Copy link

Thanks a lot!

@ryangravetteadmin
Copy link

Just wanted to say a hearty thank you for this. It saved me a bunch of time looking up what school districts in Idaho were using Google vs O365

@siddheshmishra
Copy link

I think it will not work for this, www.akasamconsulting.com !

@Jrosales2019
Copy link

And me thinking that it would give the result of nslookup, instead of "Other"

Please update this so that it is really useful.

@paul-hph
Copy link

@Jrosales2019 At Row 75: instead of return emailProvider; just use return mx;

@mjoeabraham
Copy link

Hi,

I have found this tool to be very helpful as well. However, today I am only receiving an "ERROR" response even when it is looking up domains that I know are either Google or Office etc. Can anyone help with this? Thanks.

@Jrosales2019
Copy link

Incredible.
works perfectly

How can I find new codes for google sheet for network functions, pint, tracert, whois, nslookup, among others

@juliennnnn
Copy link

It works perfectly, thanks a lot.
I added some providers in the script (yahoo, comcast, proofpoint...) and it's perfect.

@emonnaim
Copy link

Perfect! Thank you so so much!

@r-alnairab
Copy link

Thanks for this script.
It helped me a lot.
I made some changes to it to be able to get A record and NS record for a domain.

5 Starts 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment