Re: Re: email validation (no regex)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Jim Moseby said the following on 09/21/05 11:00:
>>>So, what is the general thought about validating email 
>>
>>addresses in this
>>
>>>manner?
>>>
>>>JM
>>
>>Thre is a good reason why virtually everyone uses regex 
>>patterns for email validating.
> 
> 
> Excellent start!  And that good reason is...?  
> How can regex ensure that the email address that is submitted is a valid (ie
> working, able to receive email) address?
> Why is regex a better way?

Personally I would go for a combination.  Regex is much faster so if you
can eliminate fake addresses with regex you won't have to waste your
time attempting to look up MX records or connect to mail servers that
don't exist.

My apologies for the line wrapping, but the following is a slightly
modified function I found online and have been using for a while.  It
doesn't actually connect to the remote server and try sending to the
address provided like your function does, it merely checks for a valid
MX for the domain.  The extra time spent attempting a fake send to an
address was deemed not worth the bother as some mail servers (especially
qmail) do not, by default or without patching, block messages from being
sent to non-existant email addresses.  Instead the message is accepted
and bounced.  Your method will not detect this.

- Ben


function isValidEmail($address, $checkMX = false) {
    // Return true or false depending on whether the email address is valid
    $valid_tlds = array("arpa", "biz", "com", "edu", "gov", "int",
"mil", "net", "org", "aero",
            "ad", "ae", "af", "ag", "ai", "al", "am", "an", "ao", "aq",
"ar", "as", "at", "au",
            "aw", "az", "ba", "bb", "bd", "be", "bf", "bg", "bh", "bi",
"bj", "bm", "bn", "bo",
            "br", "bs", "bt", "bv", "bw", "by", "bz", "ca", "cc", "cf",
"cd", "cg", "ch", "ci",
            "ck", "cl", "cm", "cn", "co", "cr", "cs", "cu", "cv", "cx",
"cy", "cz", "de", "dj",
            "dk", "dm", "do", "dz", "ec", "ee", "eg", "eh", "er", "es",
"et", "fi", "fj", "fk",
            "fm", "fo", "fr", "fx", "ga", "gb", "gd", "ge", "gf", "gh",
"gi", "gl", "gm", "gn",
            "gp", "gq", "gr", "gs", "gt", "gu", "gw", "gy", "hk", "hm",
"hn", "hr", "ht", "hu",
            "id", "ie", "il", "in", "io", "iq", "ir", "is", "it", "jm",
"jo", "jp", "ke", "kg",
            "kh", "ki", "km", "kn", "kp", "kr", "kw", "ky", "kz", "la",
"lb", "lc", "li", "lk",
            "lr", "ls", "lt", "lu", "lv", "ly", "ma", "mc", "md", "mg",
"mh", "mk", "ml", "mm",
            "mn", "mo", "mp", "mq", "mr", "ms", "mt", "mu", "mv", "mw",
"mx", "my", "mz", "na",
            "nc", "ne", "nf", "ng", "ni", "nl", "no", "np", "nr", "nt",
"nu", "nz", "om", "pa",
            "pe", "pf", "pg", "ph", "pk", "pl", "pm", "pn", "pr", "pt",
"pw", "py", "qa", "re",
            "ro", "ru", "rw", "sa", "sb", "sc", "sd", "se", "sg", "sh",
"si", "sj", "sk", "sl",
            "sm", "sn", "so", "sr", "st", "su", "sv", "sy", "sz", "tc",
"td", "tf", "tg", "th",
            "tj", "tk", "tm", "tn", "to", "tp", "tr", "tt", "tv", "tw",
"tz", "ua", "ug", "uk",
            "um", "us", "uy", "uz", "va", "vc", "ve", "vg", "vi", "vn",
"vu", "wf", "ws", "ye",
            "yt", "yu", "za", "zm", "zr", "zw", "coop", "info",
"museum", "name", "pro");

    // Rough email address validation using POSIX-style regular expressions
    if (!eregi("^[a-z0-9_\.-]+@[a-z0-9\-]{2,}\.[a-z0-9\-\.]{2,}$",
$address)) {
        return false;
    }
    else {
        $address = strtolower($address);
    }

    // Explode the address on name and domain parts
    $name_domain = explode("@", $address);

    // There can be only one ;-) I mean... the "@" symbol
    if (count($name_domain) != 2)


    // There can be only one ;-) I mean... the "@" symbol
    if (count($name_domain) != 2)
        return false;

    // Check the domain parts
    $domain_parts = explode(".", $name_domain[1]);
    if (count($domain_parts) < 2)
        return false;

    // Check the TLD ($domain_parts[count($domain_parts) - 1])
    if (!in_array($domain_parts[count($domain_parts) - 1], $valid_tlds))
        return false;

    // Search DNS for MX records corresponding to the hostname
($name_domain[0])
    if ($checkMX && !getmxrr($name_domain[1], $mxhosts))
        return false;

    return true;
}

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux