Skip to content

Instantly share code, notes, and snippets.

@JamoCA
Last active September 18, 2019 20:16
Show Gist options
  • Save JamoCA/72cdcb77246ea0ee5820 to your computer and use it in GitHub Desktop.
Save JamoCA/72cdcb77246ea0ee5820 to your computer and use it in GitHub Desktop.
Dominic Sayers' is_email() Java integration w/ColdFusion. Adds DNS/MX check. Better validation than ColdFusion's isValid() or any regex.
<cfscript>
/* Copy IsEMail.jar to java path. Download from https://code.google.com/p/isemail/downloads/list */
/* Comparison of methods available at https://gamesover2600.tumblr.com/post/152034073124/clientserver-side-email-validation-compared */
emails = ["test@test.com",
"valid_but_no_mx@fake-domain.com",
"valid_but_no_dns@fake-domain2.com",
"""much.more unusual""@example.com",
"john.smith@example.com(comment)",
"""first\last""@iana.org",
"first.last@com",
"""Joe.\Blow""@iana.org",
"foobar@192.168.0.1",
"first.last@-xample.com",
"first.last@exampl-.com",
"first.last@x234567890123456789012345678901234567890123456789012345678901234.iana.org"];
/* Set DNS server to prevent ColdFusion error:
javax.naming.ConfigurationException: localhost:2932 is not a valid DNS pseudo-URL */
sys = createObject("java", "java.lang.System");
sys.setProperty("java.naming.provider.url","dns:/YOUR_DNS_SERVER");
isEmailObj = CreateObject("java", "com.dominicsayers.isemail.IsEMail");
check_DNS = true;
responseLabels = ListToArray("Email,isValid,Status,Rule ID,Rule Name,SMTP SMTP Code");
function checkEmail(e, useDNS){
var check = isEmailObj.is_email_verbose(e, Check_DNS);
var response = structNew();
response["Email"] = e;
response["isValid"] = check.getState().isValid();
response["Status"] = check.getStatusTextExplanatory();
response["Rule ID"] = check.getId();
response["Rule Name"] = check.getConstantName();
response["SMTP"] = "N/A";
response["SMTP Code"] = "N/A";
if (useDNS){
response["SMTP"] = check.getSmtpCode();
response["SMTP Code"] = check.getSmtpCodeText();
}
return response;
}
for(i=1; i LTE ArrayLen(emails); i=i+1){
writeoutput('<h2>#emails[i]#</h2>');
temp = checkEmail(emails[i], check_DNS);
for(a=1; a LTE ArrayLen(responseLabels); a=a+1){
if (StructKeyExists(temp, responseLabels[a])){
writeoutput('<div><b>#responseLabels[a]#:</b> #Temp[responseLabels[a]]#</div>');
}
}
writeoutput('<hr>');
}
</cfscript>
@randy-johnson
Copy link

I tried a local dns server and google public but it doesn't seem to find any mx records for any domain name.

Any ideas?

sys.setProperty("java.naming.provider.url","dns:/8.8.8.8");

Example:
Email: randyj@gmail.com
isValid: YES
Status: Couldn't find an MX-record or an A-record for this domain
Rule ID: 73
Rule Name: ISEMAIL_DOMAINNOTFOUND
Error: [ISEMAIL_ERROR] Address is invalid

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