eMail-validation by server-query

emailcheck.inc.php
<?php
 
$email = 'user@example.org';
 
function readSMTP($fsck) {
  $res = '';
  do {
    $x = @fgets($fsck, 2048);
    $res .= $x;
  } while (strlen(trim($x))>0);
  return $res;
}
 
/*
 * eMail-validation via asking the appropriate mail server
 * code taken from http://www.zend.com/zend/spotlight/ev12apr.php
 * modified by Markus Birth <mbirth@webwriters.de>
 */
list( $em_user, $em_domain) = split('@', $email);
if (getmxrr($em_domain, $em_mxhost)) {
  $em_connectaddress = $em_mxhost[0];
} else {
  $em_connectaddress = $em_domain;
}
$em_connect = @fsockopen($em_connectaddress, 25, $errno, $errstr, 5);
if ($em_connect) {
  $host = $_SERVER['HTTP_HOST'];
  if (empty($host)) $host = $_SERVER['SERVER_NAME'];
  stream_set_timeout($em_connect, 2);
  if (ereg('^220', $em_out = @fgets($em_connect, 2048))) {
    fputs($em_connect, "HELO " . $host . "\r\n");
    $em_out = trim(readSMTP($em_connect));
    if (strpos($host, '.') !== FALSE) {
      $dom = explode('.', $host);
      $c = count($dom);
      $sender = 'validate@' . $dom[$c-2] . '.' . $dom[$c-1];
    } else {
      $sender = 'validate@' . $host;
    }
    fputs($em_connect, "MAIL FROM: <$sender>\r\n");
    $em_from = trim(readSMTP($em_connect));
    fputs($em_connect, "RCPT TO: <$email>\r\n");
    $em_to = trim(readSMTP($em_connect));
    fputs($em_connect, "QUIT\r\n");
    fclose($em_connect);
    $emp = @fopen('login_errors.txt', 'at');
    @fwrite($emp, "Host: {$_SERVER['HTTP_HOST']} (" . date('Y-m-d H:i.s') . "\neMail from: $sender\neMail to check: $email\nMX: $em_connectaddress (" . implode(', ', $em_mxhost) . ")\nem_helo: $em_out\nem_from: $em_from\nem_to: $em_to\n<===>\n");
    @fclose($emp);
    if (!ereg('^250', $em_to)) {
      return array('error' => true, 'error_msg' => 'Der zust&auml;ndige eMail-Server kennt die eMail-Adresse nicht. Bitte &uuml;berpr&uuml;fe sie noch einmal.');
    }
  } else {
    return array('error' => true, 'error_msg' => 'Der zust&auml;ndige eMail-Server antwortet nicht. Pr&uuml;fe Deine eMail-Adresse!');
  }
} else {
  return array('error' => true, 'error_msg' => 'Kann nicht zum eMail-Server zwecks &Uuml;berpr&uuml;fung der Adresse verbinden. Pr&uuml;fe Deine eMail-Adresse oder probiere es sp&auml;ter nochmal.' . " ($errno: $errstr)");
}
 
return array('error' => false, 'error_msg' => '');
 
?>

 
snippets/php/emailvalidation.txt · Last modified: 2010-01-15 14:43.19 by mbirth
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki Contents powered by Club-Mate Contents powered by BassDrive.com Labelled with ICRA