PHP Verified Email address class (classic)

Source: Internet
Author: User
Tags php email preg strcmp mail exchange mx record
Share a PHP class, used to verify the e-mail address, generally we will be through the JS of the regular to verify that the code is shared here with the native PHP to achieve, the need for friends, you can refer to the next.

This article provides the PHP email authentication class, has the following characteristics: 1, can be used as a simple email address verification. 2, you can verify that the email address belongs to a valid mail exchange record field. 3, you can try to connect to the mail server to detect whether the mailbox is real. 4, for the Windows platform, the implementation of GETMXRR does not have the function, the use of Moriyoshi Koizumi DNS resolution class.


     Next_token; } for ($character =0; $character
      Next_token=substr ($string, $found + 1);         Return (substr ($string, 0, $found));             } else {$this->next_token= "";         return ($string);         }} Function Outputdebug ($message) {$message. = "\ n"; if ($this->html_debug) $message =str_replace ("\ n", "
\ n ", Htmlentities ($message)); Echo $message; Flush (); } Function GetLine ($connection) {for ($line = "";;) {if (feof ($connection)) return (0); $line. =fgets ($connection, 100); $length =strlen ($line); if ($length >=2 && substr ($line, $length -2,2) = = "\ r \ n") {$line =substr ($lin e,0, $length-2); if ($this->debug) $this->outputdebug ("S $line"); return ($line); }}} Function Putline ($connection, $line) {if ($this->debug) $this->outputde Bug ("C $line"); Return (Fputs ($connection, "$line \ r \ n")); } Function validateemailaddress ($email) {if (IsSet ($this->preg)) {if (strlen ($this- >preg)) return (Preg_match ($this->preg, $email)); } else {$tHis->preg= (function_exists ("Preg_match")? "/". Str_replace ("/", "\\/", $this->email_regular_expression). " /" : ""); Return ($this->validateemailaddress ($email)); } return (Eregi ($this->email_regular_expression, $email)!=0); } Function validateemailhost ($email,& $hosts) {if (! $this->validateemailaddress ($email)) return (0); $user = $this->tokenize ($email, "@"); $domain = $this->tokenize (""); $hosts = $weights =array (); $GETMXRR = $this->getmxrr; if (function_exists ($GETMXRR) && $getmxrr ($domain, $hosts, $weights)) {$mxhosts =array (); for ($host =0; $host exclude_address) ==0 | | strcmp (@gethostbyname ($this->exclude_address), $ip)) $hosts []= $domain; } return (count ($hosts)!=0); } Function Verifyresultlines ($connection, $code) {while (($line = $this->getline ($connection))) { $this->last_code= $this->tokenize ($line, "-"); if (strcmp ($this->last_code, $code)) return (0); if (!strcmp (substr ($line, strlen ($this->last_code), 1), "")) return (1); } return (-1); } Function Validateemailbox ($email) {if (! $this->validateemailhost ($email, $hosts)) return (0 ); if (!strcmp ($localhost = $this->localhost, "") &&!strcmp ($localhost =getenv ("SERVER_NAME"), "") &amp ;&!strcmp ($localhost =getenv ("HOST"), "") $localhost = "localhost"; if (!strcmp ($localuser = $this->localuser, "") &&!strcmp ($localuser=getenv ("USERNAME"), "" ") &&!strcmp ($localuser =getenv (" USER ")," ") $localuser =" root "; for ($host =0; $host Debug) $this->outputdebug ("Resolving host name \" ". $hosts [$host]." \"..."); if (!strcmp ($ip = @gethostbyname ($domain), $domain)) {if ($this->debug) $this->outputdebug ("Could not resolve host name \" ". $hosts [$host]." \"."); Continue }} if (Strlen ($this->exclude_address) &&!strcmp (@gethostbyname ($this->e xclude_address) ($IP)) {if ($this->debug) $this->outputdebug ("Host ad Dress of \ "". $hosts [$host]. " \ "is the exclude address"); Continue if ($this->debug) $this->outputdebug ("Connecting to host address \" ". $ip." \"..."); if ($connection = ($this->timeout? @fsockopen ($ip, $errno, $error, $this,->timeout): @fsockopen ($ip, 25)))) {$timeout = ($thisData_timeout? $this->data_timeout: $this->timeout); if ($timeout && function_exists ("Socket_set_timeout")) Socket_set_timeout ($conn Ection, $timeout, 0); if ($this->debug) $this->outputdebug ("Connected."); if ($this->verifyresultlines ($connection, "") >0 && $this->putline ($connection, "HELO $loc Alhost ") && $this->verifyresultlines ($connection," $ ") >0 && $this-& Gt Putline ($connection, "MAIL from: < $localuser @ $localhost >") && $this->verifyresultlines ($co Nnection, "$") >0 && $this->putline ($connection, "RCPT to: < $email >") & amp;& ($result = $this->verifyresultlines ($connection, ""]) >=0) {if ($result ) {if ($this-> Putline ($connection, "DATA")) $result = ($this->verifyresultlines ($connection, "354")!=0); } else {if (strlen ($this->last_code) &&!strcmp ($this->last_code[0], "4")) $result =-1; } if ($this->debug) $this->outputdebug ("This host states that T He address is ". ($result? ($result >0? "Valid": "Undetermined"): "Not valid"). "."); Fclose ($connection); if ($this->debug) $this->outputdebug ("Disconnected."); return ($result); if ($this->debug) $this->outputdebug ("Unable to validate the address with this Host. "); Fclose ($connection); if ($this->debug) $This->outputdebug ("Disconnected."); } else {if ($this->debug) $this->outputdebug ("Failed.") ; }} return (-1); }};?>//GETMXRR function emulation need to make the class work properly under Windows and other platforms without thi s function (needs dns.php mentioned in the related LINKS section). All rights Reserved. This library was free software; Can redistribute it and/or modify it under the terms of the GNU Lesser general public License as published by th e free software Foundation; Either version 2.1 of the License, or any later version. This library was distributed in the hope that it'll be useful, but without any WARRANTY; Without even the implied warranty of merchantability or FITNESS for A particular PURPOSE. See the GNU Lesser general public License for more details. You should has received a copy of the GNU Lesser general public License along with this library; If not, write to the Free Software Foundation, Inc., Temple Place, Suite, Boston, MA 02111-1307 USA------ ------------------------------------------------------------------*/if (IsSet ($_nameservers) && (GetType ( $_nameservers)! = "Array" | | Count ($_nameservers) ==0)) Unset ($_nameservers); /******************************************Description $_nameserver[] The array that contains IP addresses or domain names O F name servers used for DNS resolution. If Nothing was set before require () ' ING this library, the values would automatically prepared. BOOL Getmxrr (String $hostname, Arrayref $mxhosts, Arrayref $weight); This function works in the same-GETMXRR (), however the third parameter cannot be omitted. If you need no MX preference information, please do LIKE:GETMXRR (' ', $mxhosts, ${'}); -------------------------------------------------------------------------Configuration If You is doing with Win32 EN Vironments and don ' t set $_nameserver manually, make sure this ipconfig.exe is within the PATH. Ipconfig.exe is generally distributed with any Microsoft (R) Windows distributions except for Windows 95. /require_once (' dns.php '); /* Rewrite this path to the same as the box ' s configuration if you run scripts on *nix platforms */define ( ' Resolv_conf_path ', '/etc/resolv.conf '); if (!isset ($_nameservers)) {$_nameservers = array (); if (strncmp (Php_os, "WIN", 3) = = 0) {unset ($res); EXEC (' Ipconfig/all ', $res); $cnt = count ($res); for ($i = 0; $i < $cnt; + + $i) {if (Strpos ($res [$i], ' DNS Servers ')!== false) { $_nameservers[] = substr ($res [$i], Strpos ($res [$i], ': ') + 2); Break }} while ($i < $cnt-1 && strpos ($res [+ + $i], ': ') = = = = False) {$_names Ervers[] = Trim ($res [$i]); }} elseif (File_exists (Resolv_conf_path)) {$lines = file (Resolv_conf_path); $cnt = count ($lines); for ($i = 0; $i < $cnt; + + $i) {LIST ($dr, $val) = Split (' [\ t] ', $lines [$i]); if ($dr = = ' nameserver ') {$_nameservers[] = RTrim ($val); }} unset ($lines); }} if (count ($_nameservers)) $__phpresolver_rs = new Dnsresolver ($_nameservers[0]); else {Unset ($_nameservers); Unset ($__PHPRESOLVER_RS); } function Getmxrr ($hostname, & $mxhosts, & $weight) {global $__phpresolver_rs; if (! IsSet ($__PHPRESOLVER_RS)) return (false); $dnsname = & Dnsname::newfromstring ($hostname); $answer = & $__phpresolver_rs->sendquery (new DnsQuery (New Dnsrecord ($dnsname, Dns_recordty (PE_MX))); if ($answer = = = False | | $answer->rec_answer = = = False) {return false; } else {$i = count ($answer->rec_answer); $mxhosts = $weight = Array (); while (--$i >= 0) {if ($answer->rec_answer[$i]->type = = dns_recordtype_mx) { $rec = & $answer->rec_answer[$i]->specific_fields; $mxhosts [] = substr ($rec [' Exchange ']->getcanonicalname (), 0,-1); $weight [] = $rec [' preference ']; }} return true; }}?>//test SCRIPT <title>Test for Manuel Lemos ' s PHP e-mail validation class</title>

Test for Manuel Lemos ' s PHP e-mail validation class

Getmxrr= "_GETMXRR"; } */* How many seconds to wait before each attempt to connect to the destination e-mail server */$va lidator->timeout=10; /* How many seconds to wait for data exchanged with the server. Set to a non zero value if the data timeout would be different than the connection timeout. */$validator->data_timeout=0; /* User part of the e-mail address of the sending user ( in this example) */$validator Localuser= "Info"; /* Domain part of the e-mail address of the sending user */$validator->localhost= ""; /* Set to 1 If your want to output of the dialog with the destination mail server */$validator->debug=1; /* Set to 1 If your want the debug output to being formatted to is displayed properly in a HTML page. */$validator->html_debug=1; /* When it isn't possible to resolve the e-mail address of destination server (MX RecORD) eventually because the domain is invalid, this class tries to resolve the domain address (A record). If it fails, usually the resolver library assumes that could being because the specified domain is just the subdomain Part. So, it appends the local default domain and tries to resolve the resulting domain. It may happen that the local DNS have an * for the A record, so any sub-domain are resolved to some local IP A Ddress. This prevents the class from figuring if the specified e-mail address domain is valid. To avoid this problem, just specify in this variable the local address, the resolver library would retur N with gethostbyname () function for invalid global domains, that would is confused with valid local domains. Here it can is either the domain name or its IP address. */$validator->exclude_address= ""; if (IsSet ($_get["email")) $email =$_get["email"]; if (IsSet ($email) && strcmp ($email, "")) {if ($result = $validator->validateemailbox ($email)) <0) echo "

It was not possible to determine if $email is a valid deliverable E-mail box address.

\ n "; else echo "

$email is ". ($result? "": "Not"). " A valid deliverable E-mail box address.

\ n "; } else {$port = (strcmp ($port =getenv ("Server_port"), "")? Intval ($port): 80); $site = "http://". (strcmp ($site =getenv ("SERVER_NAME"), "" ") $site:" localhost "). ($port ==80? "": ":". $port). GETENV ("Request_uri"); echo "

Access This page using a URLs like: $site?

\ n "; }?>
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.