Проверка электронной почты PHP скрипт
Мой первый пост здесь, хотя я много раз использовал этот сайт, чтобы найти исправления для кода.
Я использую script для проверки сообщений электронной почты на моем старом хосте, который работает отлично, но новый хост (123-reg) он вообще не проверяет их правильно.
Этот script на самом деле проверяет существование электронной почты не только формата этого файла.
см. следующие ссылки
t43.co.uk/[email protected](отлично работает)
smile-database.co.uk/[email protected](всегда говорит, что действительно, когда я знаю, что это не так)
my script ниже.
function checkEmail( $email, $chFail = false )
{
$msgs = Array();
$msgs[] = 'Received email address: '.$email;
if( !preg_match( "/^(([^<>()[\]\\\\.,;:\[email protected]\"]+(\.[^<>()[\]\\\\.,;:\[email protected]\"]+)*)|(\"([^\"\\\\\r]|(\\\\[\w\W]))*\"))@((\[([0-9]{1,3}\.){3}[0-9]{1,3}\])|(([a-z\-0-9áàäçéèêñóòôöüæøå]+\.)+[a-z]{2,}))$/i", $email ) )
{
$msgs[] = 'Email address was not recognised as a valid email pattern<br><br>';
return $chFail ? Array( false, $msgs ) : false;
}
$msgs[] = 'Email address was recognised as a valid email pattern';
//get the mx host name
if( preg_match( "/@\[[\d.]*\]$/", $email ) )
{
$mxHost[0] = preg_replace( "/[\w\W]*@\[([\d.]+)\]$/", "$1", $email );
$msgs[] = 'Email address contained IP address '.$mxHost[0].' - no need for MX lookup';
}
else
{
//get all mx servers - if no MX records, assume domain is MX (SMTP RFC)
$domain = preg_replace( "/^[\w\W]*@([^@]*)$/i", "$1", $email );
if( !getmxrr( $domain, $mxHost, $weightings ) )
{
$mxHost[0] = $domain;
$msgs[] = 'Failed to obtain MX records, defaulting to '.$domain.' as specified by SMTP protocol';
}
else
{
array_multisort( $weightings, $mxHost );
$cnt = ''; $co = 0; foreach( $mxHost as $ch ) { $cnt .= ( $cnt ? ', ' : '' ) . $ch . ' (' . $weightings[$co] . ')'; $co++; }
$msgs[] = 'Obtained the following MX records for '.$domain.': '.$cnt;
}
}
//check each server until you are given permission to connect, then check only that one server
foreach( $mxHost as $currentHost )
{
$msgs[] = 'Checking MX server: '.$currentHost;
if( $connection = @fsockopen( $currentHost, 25 ) )
{
$msgs[] = 'Created socket ('.$connection.') to '.$currentHost;
if( preg_match( "/^2\d\d/", $cn = fgets( $connection, 1024 ) ) )
{
$msgs[] = $currentHost.' sent SMTP connection header - no futher MX servers will be checked: '.$cn;
while( preg_match( "/^2\d\d-/", $cn ) )
{
$cn = fgets( $connection, 1024 );
$msgs[] = $currentHost.' sent extra connection header: '.$cn;
}
if( !$_SERVER )
{
global $HTTP_SERVER_VARS; $_SERVER = $HTTP_SERVER_VARS;
}
//attempt to send an email from the user to themselves (not <> as some misconfigured servers reject it)
echo $_SERVER['HTTP_HOST'] . "<BR>";
$localHostIP = gethostbyname(preg_replace("/^.*@|:.*$/",'',$_SERVER['HTTP_HOST']));
echo $localHostIP . "<BR>";
$localHostName = gethostbyaddr($localHostIP);
fputs( $connection, 'HELO '.($localHostName?$localHostName:('['.$localHostIP.']'))."\r\n" );
if( $success = preg_match( "/^2\d\d/", $hl = fgets( $connection, 1024 ) ) )
{
$msgs[] = $currentHost.' sent HELO response: '.$hl;
fputs( $connection, "MAIL FROM: <$email>\r\n" );
if( $success = preg_match( "/^2\d\d/", $from = fgets( $connection, 1024 ) ) )
{
$msgs[] = $currentHost.' sent MAIL FROM response: '.$from;
fputs( $connection, "RCPT TO: <$email>\r\n" );
if( $success = preg_match( "/^2\d\d/", $to = fgets( $connection, 1024 ) ) )
{
$msgs[] = $currentHost.' sent RCPT TO response: '.$to;
}
else
{
$msgs[] = $currentHost.' rejected recipient: '.$to;
}
}
else
{
$msgs[] = $currentHost.' rejected MAIL FROM: '.$from;
}
}
else
{
$msgs[] = $currentHost.' rejected HELO: '.$hl;
}
fputs( $connection, "QUIT\r\n");
fgets( $connection, 1024 ); fclose( $connection );
//see if the transaction was permitted (i.e. does that email address exist)
$msgs[] = $success ? ('Email address was accepted by '.$currentHost) : ('Email address was rejected by '.$currentHost);
return $chFail ? Array( $success, $msgs ) : $success;
}
elseif ( preg_match( "/^550/", $cn ) )
{
$msgs[] = 'Mail domain denies connections from this host - no futher MX servers will be checked: '.$cn;
return $chFail ? Array( false, $msgs ) : false;
}
else
{
$msgs[] = $currentHost.' did not send SMTP connection header: '.$cn;
}
}
else
{
$msgs[] = 'Failed to create socket to '.$currentHost;
}
}
$msgs[] = 'Could not establish SMTP session with any MX servers';
return $chFail ? Array( false, $msgs ) : false;
}
echo "<br><br>Email Validation Check<br><br>";
$return_msgs = checkEmail( $_REQUEST['email'], true );
if ( $return_msgs[0] == 0 )
{
echo "<img src='img/Fail.png'> May be invalid<br>".$_REQUEST['email'];
}
elseif ( $return_msgs[0] == 1 )
{
echo "<img src='img/OK.png'> Valid<br>".$_REQUEST['email'];
}
else
{
echo "<img src='img/Caution.png'> Caution<br>".$_REQUEST['email'];
}
Ответы
Ответ 1
Я прочитал один замечательный вопрос о проверке подлинности электронной почты.
Автор предлагает использовать это регулярное выражение: /[email protected]+\..+/i
и дать отличное описание для многих "почему именно это регулярное выражение"
'somestring' @'somestring'. 'somestring'