-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathemail-validator.inc.php
executable file
·263 lines (194 loc) · 6.65 KB
/
email-validator.inc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<?php
/*
File-Name: email-validator.inc.php
Author: nepomuk ([email protected])
Description: class to check, if an email-address exists
Date: 2003
*/
/*******************************/
/*** global static constants ***/
/*******************************/
/*** Return-Constants ***/
define ( "INVALID_MAIL", -10 ) ;
define ( "UNKNOWN_SERVER", -20 ) ;
define ( "SYNTAX_INCORRECT", -30 ) ;
define ( "CONNECTION_FAILED", -40 ) ;
define ( "REQUEST_REJECTED", -50 ) ;
define ( "VALID_MAIL", 10 ) ;
define ( "SYNTAX_CORRECT", 20 ) ;
class EMailValidator
{
function validateEMailAddress( $strEMailAddress )
{
$strEMailAddress = trim( $strEMailAddress ) ;
if( !$this -> checkSyntax( $strEMailAddress ) )
return SYNTAX_INCORRECT ;
$strHostName = $this -> extractHostName( $strEMailAddress ) ;
if( !$this -> checkHostName( $strHostName ) )
return UNKNOWN_SERVER ;
return $this -> checkEMailAddress( $strHostName, $strEMailAddress ) ;
}
function checkSyntax( &$strEMailAddress )
{
return preg_match( "/^[0-9a-z_]([-_\.]*[0-9a-z])*@[0-9a-z]([-\._]*[0-9a-z])*\\.[a-z]{2,3}$/i", $strEMailAddress ) == 1 ;
}
function extractHostName( &$strEMailAddress )
{
$arrElements = explode( "@", $strEMailAddress ) ;
return $arrElements[ 1 ] ;
}
function checkHostName( &$strHostName )
{
if ( preg_match( "/^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$/", $strHostName ) )
$numHostIP = @gethostbyaddr ( $strHostName ) ;
else if ( $this -> countChars( $strHostName, "." ) == 1 )
$numHostIP = @gethostbyname ( "www.".$strHostName ) ;
else
$numHostIP = @gethostbyname ( $strHostName ) ;
return strlen( $numHostIP ) > 6 && $numHostIP != $strHostName && $numHostIP != "www." . $strHostName ;
}
function getMailExchanger( &$strHostName )
{
$arrMXHosts = array( ) ;
$arrHostsWeight = array( ) ;
$blnHasMXHosts = @getmxrr ( $strHostName, $arrMXHosts, $arrHostsWeight ) ;
if( !$blnHasMXHosts )
$arrMXHosts[ 0 ] = $strHostName ;
else if( sizeof( $arrMXHosts ) > 1 )
$this -> sortByKey( $arrMXHosts, $arrHostsWeight ) ;
return $arrMXHosts ;
}
function checkEMailAddress( $strHostName,
$strEMailAddress )
{
$arrMXHosts = $this -> getMailExchanger( $strHostName ) ;
// iterate through the mail-server ( MX ) -names and send an request
// to check, if given email-Adress exists
// if the establishing of a connection failed, try the next one
for ( $i=0; $i < sizeof( $arrMXHosts ); $i++ )
{
$fpMailServer = $this -> connectToMailServer( $arrMXHosts[ $i ] ) ;
if( $fpMailServer == CONNECTION_FAILED ) // connection failed
$blnConnect = FALSE ;
else // successful connected
{
$blnConnect = TRUE ;
$numEMailExists = $this -> sendMailRequest( $fpMailServer, $strEMailAddress ) ;
if( $numEMailExists != CONNECTION_FAILED && $numEMailExists != REQUEST_REJECTED )
break ;
}
}
if ( !$blnConnect ) // connection to smtp-service failed
return SYNTAX_CORRECT ;
else
return $numEMailExists ;
}
/*** Ask the smtp-server, if given email-address exists ***/
function sendMailRequest( $fpMailServer,
$strMailRecipient )
{
$SERVER_NAME = getenv( "SERVER_NAME" ) ;
// check, if server is ready to accept smtp-commands ( Return-Code: 220 )
$strAnswer = @fgets( $fpMailServer, 3000 ) ;
if( strlen( $strAnswer ) == 0 ) // no answer
{
$this -> closeConnection( $fpMailServer, FALSE ) ;
return CONNECTION_FAILED ;
}
else if( !preg_match( "/^220/", $strAnswer ) ) // request rejected
{
$this -> closeConnection( $fpMailServer, FALSE ) ;
return REQUEST_REJECTED ;
}
// say hi ( Return-Code: 250 )
@fwrite ( $fpMailServer, "HELO " . $SERVER_NAME . "\n" ) ;
$strAnswer = @fgets( $fpMailServer, 3000 ) ;
if( !preg_match( "/^250/", $strAnswer ) ) // request rejected ( bad client-host?? )
{
$this -> closeConnection( $fpMailServer ) ;
return REQUEST_REJECTED ;
}
// tell the server, who wants to send the mail ( Return-Code: 250 )
@fwrite ( $fpMailServer, "MAIL FROM: <kunibert@" . $SERVER_NAME . ">\n" ) ;
$strAnswer = @fgets( $fpMailServer, 1500 ) ;
if( !preg_match( "/^250/", $strAnswer ) ) // REQUEST_REJECTED
{
$this -> closeConnection( $fpMailServer ) ;
return REQUEST_REJECTED ;
}
// tell the server, who is the mail-recipient ( Return-Code: 250 )
// if the recipient is unknown, the mail-address is invalid
@fwrite ( $fpMailServer, "RCPT TO: <" . $strMailRecipient . ">\n" ) ;
$strAnswer = @fgets( $fpMailServer, 1500 ) ;
if( !preg_match( "/^250/", $strAnswer ) ) // recipient unknown
{
$this -> closeConnection( $fpMailServer ) ;
return INVALID_MAIL ;
}
// say goodbye
$this -> closeConnection( $fpMailServer ) ;
// no error occured, so the mail-address is valid
return VALID_MAIL ;
}
// Opens a socket-connection to smtp-mail-service
// try max. 5 times, if connection failed, return CONNECTION_FAILED
function connectToMailServer( $strMXHost )
{
for ( $i = 0; $i < 5; $i++ )
{
// open an socket-connection at tcp-port 25 ( default mail-port )
$fpMailServer = @fsockopen ( $strMXHost, 25, $errno, $errstr, 100 ); // $errno and $errstr currently not used
if( $fpMailServer )
{
// stream should be closed after 1 second, PHP >= PHP 4.3
$strPHPVersion = phpversion( ) ;
if( preg_match( "/^(4\.[3-9])/", $strPHPVersion ) || $strPHPVersion{ 0 } == '5' )
@stream_set_timeout( $fpMailServer, 1 ) ;
return $fpMailServer ; // successful connected
}
}
return CONNECTION_FAILED ; // connection failed
}
// Closes an open socket-connection
function closeConnection( $fpConnection,
$bStarted = TRUE )
{
if( $bStarted )
@fwrite ( $fpConnection, "QUIT\n" ) ;
@fclose ( $fpConnection ) ;
}
// a simple bubble-sort-implementation
// sorts first the key-array in ascending order
// and then the array given with the first parameter
// in the order of the key-array
function sortByKey( &$objArray,
&$arrKey )
{
$numEnd = sizeof( $objArray ) -1 ;
$numEnd = sizeof( $objArray ) -1 ;
for ( $i = 1; $i <= $numEnd; $i++ )
{
for ( $j = $numEnd; $j >= $i; $j-- )
{
if ( $arrKey[ $j - 1 ] > $arrKey[ $j ] )
{
$numBuffer[ 0 ] = $arrKey[ $j ] ;
$numBuffer[ 1 ] = $objArray[ $j ] ;
$arrKey[ $j ] = $arrKey[ $j - 1 ] ;
$objArray[ $j ] = $objArray[ $j - 1 ] ;
$arrKey[ $j - 1 ] = $numBuffer[ 0 ] ;
$objArray[ $j - 1 ] = $numBuffer[ 1 ] ;
}
}
}
}
// counts the number of occurances of a given char
// in a given string
function countChars( &$strString,
$charSeparator )
{
$arrSplit = explode( $charSeparator, $strString ) ;
return sizeof( $arrSplit ) - 1 ;
}
}
?>