Last active
April 10, 2018 18:43
-
-
Save JamoCA/84d1a60b7325542d702c to your computer and use it in GitHub Desktop.
ColdFusion UDF to validate if an email address' MX record exists. (Spammers tend to generate random domain strings when submitting comment spam.)
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
<!--- NOTE: This technique is not 100% accurate because some DNS servers don't allow MX queries or may be slow to respond, | |
but this will identify addresses that are potentially bad or suspicious. ---> | |
<cfscript> | |
function isEmailDomainValid(email){ | |
var local.email = arguments.email; | |
var local.DNSServer = '8.8.8.8'; /* Google DNS */ | |
var local.timeout = 2000; | |
var local.attempts = 1; | |
var local.valid = true; | |
var local.emailDomain = trim(listlast(local.email,'@')); | |
var local.DNSRequest = ''; | |
var local.DNSResponse = ''; | |
var local.DNSService = ''; | |
if (arraylen(arguments) GT 1 and len(trim(arguments[2]))) { | |
local.DNSServer = arguments[2]; | |
} | |
if (arraylen(arguments) GT 2 and val(trim(arguments[3]))) { | |
local.timeout = val(arguments[3]); | |
} | |
if (arraylen(arguments) GT 3 and val(trim(arguments[4]))) { | |
local.attempts = val(arguments[4]); | |
} | |
if (NOT isvalid('email', arguments.email)){ | |
var local.valid = false; | |
} | |
if (local.valid){ | |
local.DNSService = CreateObject('java', 'java.util.Hashtable'); | |
local.DNSService.put('java.naming.factory.initial', 'com.sun.jndi.dns.DnsContextFactory'); | |
local.DNSService.put('java.naming.provider.url', 'dns://#local.DNSServer#'); | |
local.DNSService.put('com.sun.jndi.dns.timeout.initial', local.timeout); | |
local.DNSService.put('com.sun.jndi.dns.timeout.retries', local.attempts); | |
local.DNSRequest = CreateObject('java', 'javax.naming.directory.InitialDirContext'); | |
local.DNSRequest.init(local.DNSService); | |
try { | |
local.DNSResponse = local.DNSRequest.getAttributes(local.emailDomain, ['MX']).get('MX').getAll().next().ToString(); | |
} catch (any e){ | |
local.valid = false; | |
} | |
} | |
return local.valid; | |
} | |
</cfscript> | |
<cfoutput> | |
<cfsavecontent variable="Emails">test@#CGI.Server_Name# | |
[email protected] | |
[email protected] | |
[email protected] | |
[email protected] | |
[email protected] | |
[email protected] | |
[email protected] | |
[email protected] | |
[email protected] | |
[email protected] | |
[email protected] | |
test@aol | |
[email protected] | |
[email protected] | |
[email protected] | |
[email protected] | |
test@invalid_email</cfsavecontent> | |
<style type="text/css"> | |
li.no {color:##f30;} | |
li.yes {color:##3c0;} | |
</style> | |
<h2>Validate Email Address MX Records</h2> | |
<ol> | |
<cfloop array="#ListToArray(Emails,chr(10))#" index="thisEmail"> | |
<cfset Start = GetTickCount()><cfset Status = isEmailDomainValid(trim(thisEmail))><cfset TheTime = GetTickCount()-Start> | |
<li class="#lcase(yesnoformat(Status))#">#trim(ThisEmail)# = #Status# (#numberformat(TheTime)#ms)</li> | |
</cfloop> | |
</ol> | |
</cfoutput> |
All the results are false. That can't be right. I know these are valid email addresses (at least most are)
Please give thoughts?
Sam
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can't seem to get this to work
get error in var local.email = arguments.email;
You cannot use a variable reference with "." operators in this context
Any thoughts?
Ted Daniels [email protected]