Code: Select all
/**
 * Validate an Email Address Via SMTP
 * This queries the SMTP server to see if the email address is accepted.
 * @copyright http://creativecommons.org/licenses/by/2.0/ - Please keep this comment intact
 * @author [email protected]
 * @version 0.1a
 */
class SMTP_validateEmail {
 
    var $sock;
 
    var $user;
    var $domain;
    var $port = 25;
    var $max_conn_time = 30;
    var $max_read_time = 5;
 
    var $from_user = 'user';
    var $from_domain = 'localhost';
 
    var $debug = false;
 
    function SMTP_validateEmail($email = false, $sender = false) {
        if ($email) {
            $this->setEmail($email);
        }
        if ($sender) {
            $this->setSenderEmail($sender);
        }
    }
 
    function setEmail($email) {
        list($user, $domain) = explode('@', $email);
        $this->user = $user;
        $this->domain = $domain;
    }
 
    function setSenderEmail($email) {
        list($user, $domain) = explode('@', $email);
        $this->from_user = $user;
        $this->from_domain = $domain;
    }
 
    /**
    * Validate an Email Address
    * @param String $email Email to validate (recipient email)
    * @param String $sender Senders Email
    */
    function validate($email = false, $sender = false) {
        if ($email) {
            $this->setEmail($email);
        }
        if ($sender) {
            $this->setSenderEmail($sender);
        }
        // retrieve SMTP Server via MX query on domain
        $result = getmxrr($this->domain, $hosts);
        // last fallback is the original domain
        array_push($hosts, $this->domain);
 
        $this->debug(print_r($hosts,1 ));
 
        $timeout = $this->max_conn_time/count($hosts);
 
        // try each host
        foreach($hosts as $host) {
            // connect to SMTP server
            $this->debug("try $host:$this->port\n");
            if ($this->sock = fsockopen($host, $this->port, $errno, $errstr, (float) $timeout)) {
                socket_set_blocking($this->sock, false);
                break;
            }
        }
 
        // did we get a TCP socket
        if ($this->sock) {
            // say helo
            $this->send("HELO ".$this->from_domain);
            // tell of sender
            $this->send("MAIL FROM: <".$this->from_user.'@'.$this->from_domain.">");
            // ask of recepient
            $reply = $this->send("RCPT TO: <".$this->user.'@'.$this->domain.">");
            // quit
            $this->send("quit");
            // close socket
            fclose($this->sock);
            // get code and msg from response
            list($code, $msg) = explode(' ', $reply);
 
            if ($code == '250') {
                // you received 250 so the email address was accepted
                return true;
            }
 
        }
 
        return false;
    }
 
 
    function send($msg) {
        fwrite($this->sock, $msg."\n");
        $resp = '';
        $start = $this->microtime_float();
        while(1) {
            $reply = fread($this->sock, 2082);
            $resp .= $reply;
            if (($resp != '' && trim($reply) == '') 
            || ($this->microtime_float() - $start > $this->max_read_time )) {
                break;
            }
        }
 
        $this->debug(">>>\n$msg\n");
        $this->debug("<<<\n$resp");
 
        return $resp;
    }
 
    /**
     * Simple function to replicate PHP 5 behaviour. http://php.net/microtime
     */
    function microtime_float() {
        list($usec, $sec) = explode(" ", microtime());
        return ((float)$usec + (float)$sec);
    }
 
    function debug($str) {
        if ($this->debug) {
            echo $str;
        }
    }
}
 Code: Select all
// the email to validate
$email = '[email protected]';
// an optional sender
$sender = '[email protected]';
// instantiate the class
$SMTP_Valid = new SMTP_validateEmail();
// turn debugging on to view the SMTP session
$SMTP_Valid->debug = true;
// do the validation
$result = $SMTP_Valid->validate($email, $sender);
// view results
var_dump($result);
echo $email.' is '.($result ? 'valid' : 'invalid')."\n";
 
// send email? 
if ($result) {
  //mail(...);
}
 



 (I didn't really plan on using this server.) Turns out to be FreeBSD + PHP 4.4.x.
 (I didn't really plan on using this server.) Turns out to be FreeBSD + PHP 4.4.x.