\n"); } // v-kind off error System or Failure(addr) // returns - function has a return value of define("e_SUCCESS",0); // 0 = success define("e_INVADDR",1); // F 1 = failed, invalid address define("e_SYSFAIL",2); // S 2 = system failure define("e_NOCONN_MX",3); // **** S 3 = no connection to assumed mailserver // mx doesn't answer smtp define("e_NOCONN_HOST",4); // **** F 4 = no connection to assumed mailserver // hostname doesn't answer smtp define("e_REFUSED", 5); // F 5 = the server refused the user define("e_NOSMTP_MX", 6); // S 6 = connected to something but mx failed to identfy itself as an SMTP server define("e_NOSMTP_HOST", 7); // F 7 = connected to something but host failed to identfy itself as an SMTP server define("e_TMP_REFUSED",9); // W 9 = temporarily refused (e.g. greylisting?) define("e_BADSYNTX",10); // ***** F 10 = bad address syntax function validateEmailErr($errno) { // return error messages to codes $errmsg = "Unknown error No. $errno"; switch($errno) { case 0: $errmsg="success"; break; case 1: $errmsg="failed, invalid address (general)"; break; case 2: $errmsg="system failure"; break; case 3: $errmsg="no connection, mx doesn't answer smtp"; break; case 4: $errmsg="no connection, hostname doesn't answer smtp"; break; case 5: $errmsg="the server refused the user"; break; case 6: $errmsg="connected, to something but remote mx failed to identfy itself as an SMTP server"; break; case 7: $errmsg="connected, to something but remote hostname failed to identfy itself as an SMTP server"; break; case 9: $errmsg="the server refused the mail temporarily (greylisting? over quota?)"; break; case 10: $errmsg="bad address syntax"; break; } return $errmsg; } function mapValErrno($errno) { // return 0, 1, 2 (succ, fail, sys_err) switch($errno) { case 0: return 0; case 1: return 1; case 2: return 2; case 3: return 2; case 4: return 1; case 5: return 1; case 6: return 2; case 7: return 1; case 9: return 2; case 10: return 1; } return 2; } function validateEmail($Email) { $debug=true; $debug=false; // Function to validate an e-mail address by both checking the // format of the address, and then testing it by holding an // authorisation conversation with the addresses SMTP server // // inputs - $Email = the email address itself // // returns - function has a return value of // 0 = success // 1 = failed, invalid address // 2 = system failure // ***** 3 = no connection to assumed mailserver // (either no mx and hostname doesn't answer smtp // or mx doesn't answer smtp) // ***** 10 = bad address syntax // // Written by Trib after reading, combining, enhancing and modifying // the efforts of too many other people to give a credit list. However // Jay Greenspan deserves a mention for his exhaustive work on address // parsing (see the link in the rutorial at // http://www.trib-design.com/gurututs/validatemail.php // and also so does an unknown person who wrote the SMTP MX record // query program which got me to thinking about the problem from the // SMTP perspective. // // You can also find the associated e-mail contact form at // // // Permission is hereby given to use, distribute and modify this code // without restriction or condition. However if you are a decent person // you might consider putting a credit into your comments. If you do, // I'm K. Salt (a.k.a. Trib) at http://www.trib-design.com. Thanks and ... // // enjoy ..... // // based on K.Salt's code I added support for multiple MX records. // Each MX will be tried in turn until the first one responds. // O.Schulz // global $HTTP_HOST; $http_host = $_SERVER['HTTP_HOST']; $env_fromhost = gethostbyaddr($_SERVER['SERVER_ADDR']); $euid=posix_geteuid(); $gecos=posix_getpwuid($euid); $env_fromaddr = $gecos['name'] . '@' . $env_fromhost; if ($debug) { print $gecos['name'];} $has_mxrec = false; // Check for a malformed address (roughly) if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $Email)) { // it failed the simple format test, so return with an invalid address error return e_BADSYNTX ; } // otherwise it passes the test so we can go off and do the 'real' validation // // first split #Email up into parts list ( $Username, $Domain ) = split ("@",$Email); // look for an MX record and get the mail host name $MXrec = array(); if (checkdnsrr ( $Domain, "MX" )) { if (getmxrr ($Domain, $MXrec)) { // save the MX hostname ready for Phase 3 testing $Mailserver = $MXrec[0]; $has_mxrec = true; } else { // there is an MX record, but we failed to retrieve it return e_SYSFAIL; // return system error NOT invalid address } } else { // in this case there isn't an MX record so assume that the domain // portion is also the name of the mail server itself (it can happen) // save it as the mailserver address ready for Phase 3 testing $Mailserver = $Domain; $MXrec[0] = $Domain; } if ($debug) print "Mailserver=$Mailserver".nlbr; if ($debug) print "HTTP_HOST=".$_SERVER['HTTP_HOST'].nlbr; // open a socket connection to the Mailserver reset($MXrec); // no warnings about connection failures to mx $old_errep=error_reporting(); error_reporting($old_errep & ~ E_WARNING ); foreach($MXrec as $Mailserver) { if ($Connection = fsockopen($Mailserver, 25)) { break; } } error_reporting($old_errep); if ($Connection) { // start the SMTP validation if (ereg("^220", $Rubbish = fgets($Connection, 1024))) { // it is an SMTP server so you can start talking to it // Tell it who you are and get the response (not needed later). fputs ( $Connection, "HELO $http_host\r\n" ); $Rubbish = fgets ( $Connection, 1024 ); // Ask it to accept mail from your $Email user - store the response (needed later) // No, this is no longer a good idea in the times of SPF and a spam rate // of more than 90%. fputs ( $Connection, "MAIL FROM: <{$env_fromaddr}>\r\n" ); $Fromstring = fgets ( $Connection, 1024 ); // Ask it to accept mail for your $Email user - store the response (needed later) fputs ( $Connection, "RCPT TO: <{$Email}>\r\n" ); $Tostring = fgets ( $Connection, 1024 ); // Now tell it you're done with chatting fputs ( $Connection, "QUIT\r\n"); // and close the connection fclose($Connection); if ($debug) print "\$Fromstring || \$Tostring = $Fromstring || $Tostring
\n"; // finally test the resonses did we get OK (type 250) messages? // TODO: 2xx or 4xx should be sufficient? if (ereg("^250", $Fromstring) && ereg("^250", $Tostring)) { // YAHOOO .. we got a good one return e_SUCCESS; // return successful validation } elseif (ereg("^250", $Fromstring) && ereg("^450", $Tostring)) { // 450: try again later 451,452,421: non-fatal error return e_TMP_REFUSED; // try again later, address MAY be valid } else { // the server refused the user return e_REFUSED; // return invalid address } } else { // connected, to something but it failed to identfy itself as an SMTP server // so assume its a bogus address if ($has_mxrec) return e_NOSMTP_MX; // return invalid address error else return e_NOSMTP_HOST; } } else { // it failed to connect if ($debug) print "no connection
\n"; if ($has_mxrec) return e_NOCONN_MX; else return e_NOCONN_HOST; // return invalid address or system error - its your call } } ?>

validate eMail address

address for testing   Take care of the correct spelling.

Syxntactic tests by Jay Greenspan and Brad Bulger
Mailaddress validation procedure by K. Salt (a.k.a. Trib); Release: 10-Nov-2002