7

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
10
  • I think that PHP is just helping developers, as in the most cases you want to validate emails of third parties. And they are very likely (like 99,9% sure) to have emails outside local server, or even if on local, then accessible with some domain in TLD scope. That's only my theory, but personally I'm happy with PHP validation even if it's not following standarized email specifications.
    – Forien
    Feb 26, 2015 at 9:23
  • @Forien, I also think that for 99.9% of the time, you have to validate emails with TLD. Few hours ago, I didn't even aware of the fact that there can be email addresses without TLD. Feb 26, 2015 at 9:41
  • 1
    I understand your question, and would be interested in the answer... but I would suggest to anyone using regex's or inbuilt language features to test email validity, just don't. With the ability for anyone to buy their own "TLD" these days, the days of validating against the usual suspects of a few years ago is long over.
    – Jarede
    Feb 26, 2015 at 12:22
  • 2
    Pretty sure the only ones who can answer why there is a difference and why each chose the way they did are the engineers at Adobe and those who contributed the code to PHP. Feb 26, 2015 at 13:48
  • 1
    not sure whether this kind of question fits into the policy of stackoverflow Probably not. Though the anatomy of a "valid" email is an interesting topic, any answers about the implementation will just be guesses/opinions.
    – Leigh
    Feb 26, 2015 at 15:33

2 Answers 2

1

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>
1
  • The answer is not what I expected, but this is the most useful answer. So I marked it as accepted. Thanks for the answer btw. Mar 23, 2015 at 16:15
0

PHP is further from the RFC standard than Coldfusion but closer to the general idea of what an email will most likely have:

  • an @
  • a FQDN - Fully Qualified Domain Name (i.e @mydomain.com)
  • a TLD - Top Level Domain (i.e .com)
  • an alpha-numeric + non-space word boundary username( i.e john.doe@...)

(Coldfusion does have all of these as well plus more, as your example points out)

1
  • Starting in ColdFusion 11, the "display name" as now part of a valid email address. I'm desperately hoping that this is a long-term unreported bug since CF2018u3 is still doing it. As of this comment, the following strings are evaluated "true" when tested against isValid("email") even though they may be unrouteable: test@test; [email protected]; "test" <[email protected]> Mar 16, 2019 at 0:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.