<?php
if(!function_exists('mailserver_checks'))
{
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Check if a mailserver and/or an email address exhist
#
# ===> At the moment can only run on Linux
#
function mailserver_checks($domain, # domain name to check
$email, # null = check ONLY the mailserver
$debug) # true = see debug info
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{
///$debug=true; # uncomment to debug code
#
if(!$domain)
{
list($box,$domain)=explode('@',$email);
if(!$domain) { return('not checked'); }
}
if($_SERVER[WINDIR]!='')
{
# "checkdnsrr()" not implemented on Windows!
#
return('not checked');
}
#
# It is running on Linux
#
if($debug) { echo '<br />Checking address: '.$email.'<br />'; flush(); ob_flush(); }
#
# Check if the mail exchanger (MX) record exists for the domain
#
if(@checkdnsrr($domain,'MX'))
{
if($debug) { echo $domain.' has an email server.<br />'; flush(); ob_flush(); }
#
# MX exists, get its address
#
if(@getmxrr($domain,$mxhost))
{
$mxhost_items=count($mxhost);
if($debug)
{
echo 'Got MX Host address.<br />'; flush(); ob_flush();
for($i=0,$j=1;$i<$mxhost_items;$i++,$j++)
{
echo " Result($j) - $mxhost[$i]<br />";
}
}
}
$connect_address=$mxhost[0];
if(!$email) { return('valid'); }
#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
}
else
{
$connect_address=$domain;
if($debug) { echo $domain.' does NOT have an email server.<br />'; flush(); ob_flush(); }
return(strtoupper($domain).' does NOT have a mail server.');
}
#
# Try to connect with the email addess to check if the MX accept it
#
$i=0; $ok='';
while($i<$mxhost_items && $ok != 'ok')
{
if($debug) { echo 'Temptative on mailserver: '.$mxhost[$i].'<br />'; flush(); ob_flush(); }
$connection=@fsockopen($mxhost[$i], 25);
if($connection)
{
if($debug) { echo "Connection succeeded to {$mxhost[$i]} SMTP.<br />[{$connection}]<br />"; flush(); ob_flush(); }
#
# If the mail server is preparing for the connection (220)
#
if(ereg("^220",$out=@fgets($connection,1024)))
{
# Start connection
#
@fputs($connection,"HELO $_SERVER[HTTP_HOST]\r\n"); if($debug) { echo "HELO $_SERVER[HTTP_HOST]<br />"; flush(); ob_flush(); }
$out=@fgets($connection,1024);
#
# Send the FROM address
#
@fputs($connection,"MAIL FROM: <{$email}>\r\n" );
$from=@fgets($connection,1024); if($debug) { echo "MAIL FROM: {$from}<br />"; flush(); ob_flush(); }
#
# Send the TO address
#
@fputs($connection,"RCPT TO: <{$email}>\r\n");
$to=@fgets($connection,1024); if($debug) { echo "RCPT TO: {$to}<br />"; flush(); ob_flush(); }
#
# Close connection
#
@fputs($connection,"QUIT\r\n"); if($debug) { echo "QUIT<br />"; flush(); ob_flush(); }
@fclose($connection);
#
# Email address not known
#
#--?--# if(!ereg("^250",$from) || !ereg("^250",$to))
if(ereg("^550",$from) || ereg("^550",$to)) { $ok=''; } else { $ok='ok'; }
}
}
else
{
# Failure in connection
#
if($debug) { echo "Cannot connect to {$domain} mail server ({$mxhost[$i]}).<br />"; flush(); ob_flush(); }
return('valid'); # let the user proceed...
}
$i++;
}
if($ok=='ok') { return('valid'); }
else
{
if($debug) { echo "{$email} NOT known at domain mail server.<br />"; flush(); ob_flush(); }
$tmp='['.$email.'] NOT known at ['.strtoupper($domain).'].';
return($tmp);
}
}
}
?>