Re: Email Validate.php

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

 



Edi Weitz, a free-lance consultant and application developer in Hamburg, 
Germany, has created a very helpful tool named Regexp Coach. This program 
will coach you through PCRE.

http://www.weitz.de/regex-coach/

Regards,
León van de Pavert

On Fri, 7 May 2004 16:03:49 +0100 (BST), Jonathan Heaton 
<jonathanheaton@yahoo.co.uk> wrote:

> I'm a recent convert to PHP and I hope to use this
> script. I have used the Unix utilities sed, grep and
> awk a number of times. There is an art to pattern
> matching and regular expressions.
>
> Thanks
>
> Jonathan Heaton
>
> --- León_van_de_Pavert
> <leon.vandepavert@yritysrekisteri.com> wrote: > LS,
>>
>> The RE
>> "^[a-zA-Z\.\-_]+@([a-zA-Z\.\-_]+\.)+[a-zA-Z]{2,4}$",
>> used by
>> latifcom@yahoo.com will validate "name@domain.xxxx"
>> even though that TLD
>> does not exist and it will not validate
>> name2004@domain.com, even though
>> it is a very valid email address.
>>
>> An email can be of the following forms, before the @
>> sign :
>>
>> name
>> firstname.lastname
>> firstname.middlename.lastname
>> etc.
>>
>> and after the @ sign :
>>
>> domain.tld
>> sub.domain.tld
>> sub1.sub2.domain.tld
>> etc.
>>
>> Several characters are not allowed at certain
>> places. I use this PCRE with
>> preg_match :
>>
> "/^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,})$/si"
>>
>> http://php.net/manual/en/function.ereg.php says
>> "Note: preg_match(), which
>> uses a Perl-compatible regular expression syntax, is
>> often a faster
>> alternative to ereg()."
>>
>> I tend to agree when validating a large number of
>> emails from database.
>>
>> IANA has lists of all Top Level Domains that are
>> allowed at
>> http://www.iana.org/domain-names.htm but the only
>> way to be absolutely
>> sure an email address exists is to check and ask the
>> mail server.
>>
>> /* START OF CODE */
>> <?php
>> // validates email. IDN is NOT taken into
>> consideration yet.
>>
>> function validate_mail($Email) {
>> global $HTTP_HOST;
>> $result = array();
>> $Pattern =
>>
> "/^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,})$/si";
>> if (!preg_match($Pattern, $Email)) {
>> $result[0]=false;
>> $result[1]="$Email is not properly formatted (IDN
>> is not supported
>> yet).";
>> return $result;
>> }
>> list ( $Username, $Domain ) = split ("@",$Email);
>> if (getmxrr($Domain, $MXHost)) {
>> $ConnectAddress = $MXHost[0];
>> } else {
>> $ConnectAddress = $Domain;
>> }
>> $Connect = @fsockopen($ConnectAddress, 25);
>> if ($Connect) {
>> if (ereg("^220", $Out = fgets($Connect, 1024))) {
>> fputs($Connect, "HELO $HTTP_HOST\r\n");
>> $Out = fgets($Connect, 1024);
>> fputs($Connect, "MAIL FROM: <{$Email}>\r\n");
>> $From = fgets($Connect, 1024);
>> fputs($Connect, "RCPT TO: <{$Email}>\r\n");
>> $To = fgets($Connect, 1024);
>> fputs ($Connect, "QUIT\r\n");
>> fclose($Connect);
>> $result[2] = $Out;
>> $result[3] = $From;
>> $result[4] = $To;
>> $result[5] = $Domain;
>> $result[6] = $MXHost[0];
>>
>> if (!ereg("^250", $From) || !ereg ("^250", $To))
>> {
>> $result[0] = false;
>> $result[1] = "Server rejected address.\n";
>> return $result;
>> }
>> } else {
>> $result[0] = false;
>> $result[1] = "No response from server.\n";
>> return $result;
>> }
>> } else {
>> $result[0] = false;
>> $result[1] = "Can not connect E-Mail server.\n";
>> return $result;
>> }
>> $result[0] = true;
>> $result[1] = "Email address appears to be
>> valid.\n";
>> return $result;
>> }
>>
>> if ($_GET) {
>> $email = $_GET['email'];
>> } else if ($_POST) {
>> $email = $_POST['email'];
>> } else {
>> echo "no email address to validate";
>> exit();
>> }
>>
>> // IANA Country Code Top-Level Domains
>> $_CCTLD = array("ac", "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", "cd", "cf", "cg",
>> "ch", "ci", "ck", "cl",
>> "cm", "cn", "co", "cr", "cu", "cv", "cx", "cy",
>> "cz", "de", "dj", "dk",
>> "dm", "do", "dz", "ec", "ee", "eg", "eh", "er",
>> "es", "et", "fi", "fj",
>> "fk", "fm", "fo", "fr", "ga", "gd", "ge", "gf",
>> "gg", "gh", "gi", "gl",
>> "gm", "gn", "gp", "gq", "gr", "gs", "gt", "gu",
>> "gw", "gy", "hk", "hm",
>> "hn", "hr", "ht", "hu", "id", "ie", "il", "im",
>> "in", "io", "iq", "ir",
>> "is", "it", "je", "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", "nu",
>> "nz", "om", "pa", "pe", "pf", "pg", "ph", "pk",
>> "pl", "pm", "pn", "pr",
>> "ps", "pt", "pw", "py", "qa", "re", "ro", "ru",
>> "rw", "sa", "sb", "sc",
>> "sd", "se", "sg", "sh", "si", "sj", "sk", "sl",
>> "sm", "sn", "so", "sr",
>> "st", "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", "zw");
>>
>> // IANA Top-Level Domains
>> $_TLD = array("aero", "biz", "com", "coop", "edu",
>> "gov", "info", "int",
>> "mil", "museum", "name", "net", "org", "pro");
>>
>> // Cut the email address in pieces at the . (dot)
>> $tld = explode(".",$email);
>>
>> // Reverse the array. Now the first element is the
>> TLD we're going to
>> check.
>> $tld = array_reverse($tld);
>>
>> // init
>> $answer = array();
>>
>> if (in_array($tld[0],$_CCTLD) ||
>> in_array($tld[0],$_TLD) ) {
>> // valid TLD
>> $answer = validate_mail($email);
>> if ($answer[0] === true) {
>> // email validates
>> } else {
>> // email rejected.
>> }
>> } else {
>> // invalid TLD
>> $answer[0] = false;
>> $answer[1] = "TLD .{$tld[0]} does not exist.
>> Please enter a valid email
>> address.";
>> }
>> ?>
>> <html>
>> <head>
>> <title>validate_email.php?email=</title>
>> <meta http-equiv="Content-Type"
>> content="text/html; charset=utf-8" />
>> </head>
>> <body>
>>
> === message truncated ===
>
>
>
>
>
> ____________________________________________________________
> Yahoo! Messenger - Communicate instantly..."Ping"
> your friends today! Download Messenger Now
> http://uk.messenger.yahoo.com/download/index.html
>
>
>
> PHP Data object relational mapping generator - 
> http://www.meta-language.net/
>
>
> Yahoo! Groups Sponsor
> ADVERTISEMENT
>
> Yahoo! Groups Links
>
> To visit your group on the web, go to:
> http://groups.yahoo.com/group/php-objects/
>
> To unsubscribe from this group, send an email to:
> php-objects-unsubscribe@yahoogroups.com
>
> Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.



-- 
"Verdorie, kan ik weer helemaal opnieuw beginnen?"


------------------------ Yahoo! Groups Sponsor ---------------------~-->
Yahoo! Domains - Claim yours for only $14.70
http://us.click.yahoo.com/Z1wmxD/DREIAA/yQLSAA/saFolB/TM
---------------------------------------------------------------------~->

PHP Data object relational mapping generator - http://www.meta-language.net/ 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
     http://groups.yahoo.com/group/php-objects/

<*> To unsubscribe from this group, send an email to:
     php-objects-unsubscribe@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
     http://docs.yahoo.com/info/terms/
 


[Index of Archives]     [PHP Home]     [PHP Users]     [PHP Soap]     [Kernel Newbies]     [Yosemite]     [Yosemite Campsites]

  Powered by Linux