A comprehensive test of the validity of an email

Source: Internet
Author: User
Tags count mail strcmp strtok valid domain
We often hope that friends who visit your website can leave an email.
But a lot of people are going to fight, causing the manager to bother,
The following class can be used to check whether email is valid or not.

?
Class Cemail {
var $email _regular_expression= "^" ([a-z0-9_]|\-|\.) +@ (([a-z0-9_]|\-) +\.) +[a-z]{2,4}$ ";
var $timeout = 0;
var $localhost = "";
var $localuser = "";

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) = = "RN")
Return (substr ($line, 0, $length-2));
}
}

Function putline ($connection, $line)
{
Return (Fputs ($connection, "$linern"));
}

Function Verifyrule ($email)
{
Return (eregi ($this->email_regular_expression, $email)!=0);
}

Function validateemailhost ($email, $hosts =0)
{
if (! $this->verifyrule ($email))
return (0);
$user =strtok ($email, "@");
$domain =strtok ("");
if (GETMXRR ($domain,& $hosts,& $weights))
{
$mxhosts =array ();
for ($host =0; $host <count ($hosts); $host + +)
$mxhosts [$weights [$host]]= $hosts [$host];
Ksort ($mxhosts);
For (Reset ($mxhosts), $host =0 $host <count ($mxhosts); Next ($mxhosts), $host + +)
$hosts [$host]= $mxhosts [Key ($mxhosts)];
}
Else
{
$hosts =array ();
if (strcmp (@gethostbyname ($domain), $domain)!=0)
$hosts []= $domain;
}
Return (count ($hosts)!=0);
}

Function verifyresultlines ($connection, $code)
{
while (($line = $this->getline ($connection)))
{
if (!strcmp (Strtok ($line, ""), $code))
return (1);
if (strcmp (Strtok ($line, "-"), $code))
return (0);
}
Return (-1);
}

Function Verifyonline ($email)
{
if (! $this->validateemailhost ($email,& $hosts))
return (0);
if (!strcmp ($localhost = $this->localhost, "")
&&!strcmp ($localhost =getenv ("SERVER_NAME"), "")
&&!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 <count ($hosts); $host + +)
{
if ($connection = ($this->timeout fsockopen ($hosts [$host],25,& $errno,& $error, $this->timeout): Fsockopen ($hosts [$host],25)))
{
if ($this->verifyresultlines ($connection, ">0")
&& $this->putline ($connection, "HELO $localhost")
&& $this->verifyresultlines ($connection, ">0")
&& $this->putline ($connection, "MAIL from: < $localuser @ $localhost >")
&& $this->verifyresultlines ($connection, ">0")
&& $this->putline ($connection, "RCPT to: < $email >")
&& ($result = $this->verifyresultlines ($connection, ">=0"))
{
Fclose ($connection);
return ($result);
}
Fclose ($connection);
}
}
Return (-1);
}

function Verify ($email, $type =0) {
if ($type ==0) return $this->verifyrule ($email);
else return $this->verifyonline ($email);

}

};


?>


Usage:
$m =new Cemail;
Just check the language method
if ($m->verify ("jerry@mail.jerry.com.tw", 0)) echo "valid";
else echo "ineffective";

Check to see if there's a real email.
if ($m->verify ("jerry@mail.jerry.com.tw", 1)) echo "valid";
else echo "ineffective";


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: info-contact@alibabacloud.com 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.