Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

valid/invalid Email format policy difference between ColdFusion and PHP

I am neither here to make a war between PHP and ColdFusion people nor to find out which language is better than other. So please give reasonable arguments for this particular point.

I am also not sure whether this kind of question fits into the policy of stackoverflow. But here it is

If you validate email address test@testdomain in ColdFusion, it says it's valid email address. but if you test the email address in PHP, it says it's an invalid email address.

According to wikipedia, this is valid email address. Please search term admin@mailserver1 (local domain name with no TLD)

My question is that why there is a difference in the implementation and the reasons behind it.

ColdFusion Code

<cfdump var="#isValid("email","test@testdomain")#">

Output

Yes

PHP code

<?php
$email = "test@testdomain";
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  $emailErr = "Invalid"; 
}
else
{
 $emailErr = "valid";
}

echo $emailErr; 
?>

Output

Invalid
like image 568
Tushar Bhaware Avatar asked Feb 26 '15 08:02

Tushar Bhaware


Video Answer


1 Answers

Create your own wrapper. My isEmail() UDF uses special rules for including/excluding test email addresses depending on IP. (I had to allow some specific IPs for testing.) I also added a 2nd parameter to indicate type of validation... ColdFusion's BIF isValid("email") is the default, then there's regex, Java w/DNS (which respects DNS TTL) and Java w/o DNS.

Here's a regex UDF from CFLib.org:

http://www.cflib.org/udf/isEmail

function isEmail(str) {
    return (REFindNoCase("^['_a-z0-9-\+]+(\.['_a-z0-9-\+]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.(([a-z]{2,3})|(aero|asia|biz|cat|coop|info|museum|name|jobs|post|pro|tel|travel|mobi))$",
arguments.str) AND len(listGetAt(arguments.str, 1, "@")) LTE 64 AND
len(listGetAt(arguments.str, 2, "@")) LTE 255) IS 1;
}

I prefer using Java because it has more features, better failure messages, optionally uses DNS (for TLD validation & MX record checking) and can be installed on ColdFusion 8-11. (I've found that reported bugs in currently supported versions of ColdFusion are fixed only in the latest or upcoming versions.)

There is a PHP version of the java class available. By using this, ColdFusion, PHP and C# results will be consistent.

https://code.google.com/p/isemail/downloads/list

If you want to try the Java implementation with ColdFusion, check out:

http://isemail.info/

Here's some sample code I blogged about here:

http://gamesover2600.tumblr.com/post/93979011009/better-coldfusion-email-validation-using-java

<cfscript>
/* Copy IsEMail.jar to java path. Download from https://code.google.com/p/isemail/downloads/list */
emails = ["[email protected]",
    "[email protected]",
    "[email protected]",
    """much.more unusual""@example.com",
    "[email protected](comment)",
    """first\last""@iana.org",
    "first.last@com",
    """Joe.\Blow""@iana.org",
    "[email protected]",
    "[email protected]",
    "[email protected]",
    "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>
like image 191
James Moberg Avatar answered Oct 06 '22 19:10

James Moberg