1.
<?
2.
/*
3.
* 文件名: class CEmail
4.
* 用 途: 对 email 进行 (1)格式检查 (2)在线测试有效性
5.
* 用 法: $ismail = new CEmail;
6.
*
7.
* if ( $m->Verify ( "jerry@mail.jerry.com.tw",0 ) ) return TRUE; //仅检查语法
8.
* else return FALSE;
9.
*
10.
* if ( $m->Verify( "jerry@mail.jerry.com.tw", 1 ) ) return TRUE; //在线测试有效性
11.
* else return FLASE;
12.
*
13.
**/
14.
15.
class CEmail {
16.
var $email_regular_expression = "^([a-z0-9_]|-|.)+@(([a-z0-9_]|-)+.)+[a-z]{2,4}$";
17.
var $timeout = 0;
18.
var $localhost = "";
19.
var $localuser = "";
20.
21.
Function GetLine( $connection ) {
22.
for( $line = "";; ) {
23.
if( feof( $connection ) )
24.
return(0);
25.
26.
$line .= fgets( $connection, 100 );
27.
$length = strlen( $line );
28.
29.
if($length >= 2 && substr( $line, $length-2, 2 ) == "rn" )
30.
return( substr( $line, 0, $length-2 ) );
31.
}
32.
}
33.
34.
Function PutLine( $connection, $line ) {
35.
return( fputs( $connection, "$linern" ) );
36.
}
37.
38.
Function VerifyRule($email) {
39.
return( eregi( $this->email_regular_expression, $email ) != 0 );
40.
}
41.
42.
Function ValidateEmailHost( $email, $hosts = 0 ) {
43.
if( !$this->VerifyRule( $email ) )
44.
return( 0 );
45.
46.
$user=strtok($email,"@");
47.
$domain=strtok("");
48.
49.
if( GetMXRR( $domain, &$hosts, &$weights ) ) {
50.
$mxhosts = array();
51.
for( $host = 0; $host < count( $hosts ); $host++ )
52.
$mxhosts[$weights[$host]] = $hosts[$host];
53.
KSort($mxhosts);
54.
55.
for( Reset( $mxhosts ), $host = 0; $host < count( $mxhosts ); Next( $mxhosts ), $host++ )
56.
$hosts[$host] = $mxhosts[Key($mxhosts)];
57.
}
58.
else {
59.
$hosts = array();
60.
if( strcmp( @gethostbyname( $domain ), $domain ) != 0 )
61.
$hosts[] = $domain;
62.
}
63.
return( count( $hosts ) != 0 );
64.
}
65.
66.
Function VerifyResultLines( $connection, $code ) {
67.
while( ( $line = $this->GetLine( $connection ) ) ) {
68.
if( !strcmp( strtok( $line, " " ), $code ) )
69.
return( 1 );
70.
if( strcmp( strtok( $line, "-" ), $code ) )
71.
return( 0 );
72.
}
73.
return( -1 );
74.
}
75.
76.
Function VerifyOnline( $email ) {
77.
if( !$this->ValidateEmailHost( $email, &$hosts ) )
78.
79.
return( 0 );
80.
81.
if( !strcmp( $localhost = $this->localhost, "" )
82.
&& !strcmp( $localhost = getenv( "SERVER_NAME" ), "" )
83.
&& !strcmp( $localhost = getenv( "HOST" ), "" ) )
84.
85.
$localhost="localhost";
86.
87.
if( !strcmp( $localuser = $this->localuser, "" )
88.
&& !strcmp( $localuser = getenv( "USERNAME" ), "" )
89.
&& !strcmp( $localuser = getenv( "USER" ), "" ) )
90.
91.
$localuser = "root";
92.
93.
for($host=0;$host<count($hosts);$host++) {
94.
if( ( $connection = ( $this->timeout ? fsockopen( $hosts[$host], 25, &$errno, &$error, $this->timeout ) : fsockopen( $hosts[$host], 25 ) ) ) ) {
95.
if( $this->VerifyResultLines( $connection, "220" ) > 0
96.
&& $this->PutLine( $connection, "HELO $localhost" )
97.
&& $this->VerifyResultLines( $connection, "250" ) > 0
98.
&& $this->PutLine( $connection, "MAIL FROM: <$localuser@$localhost>" )
99.
&& $this->VerifyResultLines( $connection, "250" ) > 0
100.
&& $this->PutLine( $connection, "RCPT TO: <$email>" )
101.
&& ($result=$this->VerifyResultLines( $connection, "250" ) ) >= 0 ) {
102.
103.
fclose( $connection );
104.
return( $result );
105.
}
106.
fclose( $connection );
107.
}
108.
}
109.
return(-1);
110.
}
111.
112.
function Verify( $email, $type = 0 ) {
113.
if( $type == 0 ) return $this->VerifyRule( $email );
114.
else return $this->VerifyOnline($email) ;
115.
}
116.
117.
}
118.
119.
?>