MX Checking and Email Validation

Whilst encountering people inputting technically correct email address, but generally not correct (there is no gmail.com.au etc), was looking around for an MX validation, particularly important for eCommerce guest checkouts when receipt emails are just not arriving.

Dominic Sayers example in PHP (ported in Java also) seems to be one of the top ones, but kept replying with invalid MX records (probably my fault) and it is written PHP, which didn't suit my needs, see https://code.google.com/p/isemail/

Also attempted this guys implementation in ColdFusion of the above one, but didn't quite work for me either: https://gist.github.com/JamoCA/72cdcb77246ea0ee5820



When checking how the PHP was actually written; noticed a function called dns_get_record in PHP, haven't seen that one in ColdFusion, had a root round, found Pete Freitag example at http://www.petefreitag.com/item/487.cfm

Modifed that slighlty for my own needs, using Google's Public DNS; ended up with below (Note the email is not actually validated, but just checked the pattern and validates that an MX record exists)



<cffunction name="validateEmailAndMXRecord" returntype="Struct">
<cfargument name="emailAddress" required="true">
  
<cfscript>
var env = CreateObject("java", "java.util.Hashtable");
var dirContext = CreateObject("java", "javax.naming.directory.InitialDirContext");
var type = ArrayNew(1);
var attributes = "";
var atribEnum = "";
var stReturn = StructNew();
</cfscript>

<cfif isvalid("email", arguments.emailAddress)><!--- basic validation (possibly not good to rely on this.) --->
<cfscript>
env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
env.put("java.naming.provider.url", "dns://8.8.8.8"); // Using Googles Public DNS
env.put("com.sun.jndi.dns.timeout.initial", "2000");
env.put("com.sun.jndi.dns.timeout.retries", "3");

dirContext.init(env);

type[1] = "MX";
type[2] = "NS";
type[3] = "A";

stReturn.email = arguments.emailAddress;
stReturn.diagnostics = StructNew();
</cfscript>
<cftry>
<cfscript>
attributes = dirContext.getAttributes(listgetat(arguments.emailaddress, "2", "@"), type);
atribEnum = attributes.getAll();
stReturn.validMX = 0;// will be overwritten if found
</cfscript>
<cfloop condition="#atribEnum.hasMore()#">
<cfscript>
attribute = atribEnum.next();
stReturn.validEmail = 1;
if(listfirst(attribute.toString(), ":") eq "MX"){
stReturn.validMX = 1;// will be overwritten if found
}
stReturn.diagnostics[listfirst(attribute.toString(), ":")] = listlast(attribute.toString(), ":");
stReturn.message = "Email Ok";
</cfscript>
</cfloop>
<cfcatch type="any">
<cfscript>
stReturn.email = arguments.emailAddress;
stReturn.validEmail = 1;
stReturn.validMX = 0;
stReturn.message = cfcatch.message;
</cfscript>
</cfcatch>
</cftry>
<cfelse>
<cfscript>
stReturn.email = arguments.emailAddress;
stReturn.validEmail = 0;
stReturn.validMX = -1;
stReturn.diagnostics = StructNew();
stReturn.message = "Invalid Email Pattern";
</cfscript>
</cfif>
<cfreturn stReturn>
</cffunction>

Comments

Popular posts from this blog

cf_sql_timestamp vs cf_sql_date vs getdate()

Global SQL Procedure, System Objects and sp_ms_marksystemobject

Ghost Records, Card Recon and PCI Compliance