Проверка email-адреса через 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
<?php
function sWrite( $socket, $data, $echo = true ){
if( $echo ) echo $data;
fputs( $socket, $data );
$answer = fread( $socket, 1 );
$remains = socket_get_status( $socket );
if( $remains --> 0 ) $answer .= fread( $socket, $remains['unread_bytes'] );
return $answer;
}
$email = "proveryaemyj-email@host-name.ru";
$mx = dns_get_record( end( explode( "@", $email ) ), DNS_MX );
$mx = $mx[0]['target'];
$socket = fsockopen( $mx, 25, $errno, $errstr, 10 );
if( !$socket ){
echo "$errstr ($errno)\n";
}else{
echo sWrite( $socket, "" );
echo sWrite( $socket, "EHLO hostname.org\r\n" );
echo sWrite( $socket, "MAIL FROM: noreply@hostname.org\r\n" );
$response = sWrite( $socket, "RCPT TO: $email\r\n" );
echo $response;
echo sWrite( $socket, "QUIT\r\n" );
fclose( $socket );
echo "\nCheck report:\n";
if( substr_count( $response, "550" ) > 0 ) echo "Required email address does not exist.\n\n";
else if( substr_count( $response, "250" ) > 0 ) if( substr_count( $response, "OK" ) > 0 ) echo " Required email address exists.\n\n";
else echo " Email address accepted but it looks like the server is working as a relay host.\n\n";
else echo " Required email address existence was not recovered. Last response:\n ---\n$response ---\n\n";
}
?>