<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
    <title>Check Mail</title>
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />
    <meta name="generator" content="Geany 0.18.1" />
    <style type="text/css">
        table {
            border-collapse:collapse;
        }
        td {
            padding:4px;
            border:1px solid #777777;
            vertical-align:top;
        }
        tr td:first-child {
            font-weight:bold;
            border-right:1px dotted #CCCCCC;
            width:100px;
        }
    </style>
</head>
<body>
    <form method="post" action=<?php echo("\"".getenv("PHP_SELF")."\""); ?>>
        <input type="text" name="mail" /> <button>Pr&uuml;fen</button>
    </form>
    <p>&nbsp;</p>
<?php
if(isset($argv[0])) $_REQUEST["mail"] == $argv[0];
function checkmail($mail) {
    $smtp_error = array(
        "421" => "Dienst nicht bereit, schlie&szlig;e &Uuml;bertragungskanal (Das kann eine Antwort auf jede beliebige Anforderung sein, wenn der Dienst wei&szlig;, dass er schlie&szlig;en mu&szlig;)",
        "450" => "Angeforderte Mail Aktion nicht ausgef&uuml;hrt: Mailbox nicht erreichbar (z.B., Mailbox aktiv)",
        "451" => "Angeforderte Aktion abgebrochen: Lokaler Fehler in der Verarbeitung",
        "452" => "Angeforderte Aktion nicht ausgef&uuml;hrt: Nicht gen&uuml;gend Systemspeicher",
        "500" => "Syntax Fehler, Befehl nicht erkannt (Das schlie&szlig;t auch den Fehler einer zu langen Befehlszeile ein)",
        "501" => "Syntax Fehler bei Parameter oder Argumenten",
        "502" => "Befehl nicht implementiert",
        "503" => "Unbrauchbare Befehlsabfolge",
        "504" => "Befehlsparameter nicht implementiert",
        "550" => "Angeforderte Aktion nicht ausgef&uuml;hrt: Mailbox unerreichbar (z.B.: Mailbox nicht gefunden, kein Zugang)",
        "551" => "Benutzer nicht lokal; bitte [Weiterleitungspfad] versuchen",
        "552" => "Angeforderte Mail Aktion abgebrochen: &Uuml;berschreitung der zugewiesenen Speicherhgr&ouml;&szlig;e",
        "553" => "Angeforderte Aktion nicht ausgef&uuml;hrt: Name der Mailbox nicht erlaubt (z.B.: Mailbox Syntax inkorrekt)",
        "554" => "&Uuml;bertragung mi&szlig;lungen"
    );
    if(filter_var($mail, FILTER_VALIDATE_EMAIL) === false) return 1;
    $host = substr($mail,strpos($mail,"@")+1);
    checkdnsrr($host,"MX");
    getmxrr($host,$mx);
    $mx[] = $host;
    $one_host = false;
    $hosts_color = array();
    $send_check = false;
    foreach($mx AS $h) {
        echo("<!-- $h -->");
        $fp = @fsockopen($h,25,$errno,$errstr,1);
        if($fp != true) {
            $hosts_color[] = "<span style=\"color:#990000;\">".htmlentities($h)."</span>";
        }else{
            $one_host = true;
            $hosts_color[] = "<span style=\"color:#009900;\">".htmlentities($h)."</span>";
            if($send_check == false) {
                $send_check = true;
                $ret =fgets($fp);
                fputs($fp,"ehlo t-battermann.de\r\n");
                $ret.=fgets($fp);
                fputs($fp,"mail from: mail-test@t-battermann.de\r\n");
                $ret.=fgets($fp);
                fputs($fp,"rcpt to: $mail\r\n");
                $ret.=fgets($fp);
                fputs($fp,"quit\r\n");
                $ret.=fgets($fp);
                //if(preg_match("#\n(500|501|502|503|504|550|551|552|553|554)#is",$ret,$match)) return $match[1]." - ".$smtp_error[$match[1]];
                if(preg_match("#\n(421|450|451|452|500|501|502|503|504|550|551|552|553|554)#is",$ret,$match)) $smtp_status = $match[1]." - ".$smtp_error[$match[1]];
                else $smtp_status = true;
            }
            fclose($fp);
        }
    }
    if($one_host !== true) return 2;
    natsort($mx);
    return array("host"=>$host,"server"=>$mx,"server_color"=>$hosts_color,"status"=>$smtp_status);
}
if(isset($_REQUEST["mail"])) {
    $cm = checkmail($_REQUEST["mail"]);
    if(is_array($cm)) {
?>
<table>
    <tr>
        <td>E-Mail:</td>
        <td><?php echo(htmlentities($_REQUEST["mail"])); ?></td>
    </tr>
    <tr>
        <td>Status:</td>
        <td><span style="color:#009900;">G&uuml;ltig</span></td>
    </tr>
    <tr>
        <td>Host:</td>
        <td><?php echo(htmlentities($cm["host"])); ?></td>
    </tr>
    <tr>
        <td>SMTP:</td>
        <td>
<?php
        foreach($cm["server_color"] AS $s) {
            echo("          ".$s."<br />\n");
        }
?>
        </td>
    </tr>
    <tr>
        <td>Anfrage:</td>
        <td><?php
if($cm["status"] === true) {
    echo('<span style="color:#009900;">250 - OK</span>');
}elseif(intval(substr($cm["status"],0,3)) >= 500){
    echo('<span style="color:#990000;">Problem:<br />'.$cm["status"].'</span>');
}else{
    echo('<span style="color:#aaaa00;">Tempor&auml;res Problem:<br />'.$cm["status"].'</span>');
}
?>
        </td>
    </tr>
</table>
<?php
    }else{
?>
<table>
    <tr>
        <td>E-Mail:</td>
        <td><?php echo(htmlentities($_REQUEST["mail"])); ?></td>
    </tr>
    <tr>
        <td>Status:</td>
<?php
if(is_numeric($cm)) {
    echo('      <td><span style="color:#990000;">Ung&uuml;ltig</span></td>');
}else{
    if(intval(substr($cm,0,3)) >= 500) {
        echo('      <td><span style="color:#990000;">Ung&uuml;ltig</span></td>');
    }else{
        echo('      <td><span style="color:#aaaa00;">Tempor&auml;r</span></td>');
    }
}
?>
    </tr>
    <tr>
        <td>Grund:</td>
        <td><?php
            if($cm === 1)      echo("Entspricht nicht der typischen Syntax einer Mail-Adresse.");
            if($cm === 2)      echo("Kein Mail-Server gefunden.");
            echo($cm);
        ?></td>
    </tr>
</table>
<?php
    }
}

?>
</body>
</html>