op . pl This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see http://www.gnu.org/licenses/. */ class EmailValidator { const EV_VALID = 0; const EV_INVALID = 1; const EV_UNKNOWN = 2; private $server_response; public function get_server_response () { return $this->server_response; } public static function get_status_as_string ($res) { if ( $res == EmailValidator::EV_VALID ) return 'Valid'; else if ( $res == EmailValidator::EV_INVALID ) return 'Invalid'; else return 'Unknown'; } public function validate ($address, $our_address = 'root@localhost', $our_domain = 'localhost') { $parts = explode ('@', $address); $mx = array (); if ( getmxrr ($parts[1], &$mx) === true ) { foreach ($mx as $weigth => $host ) { $v = $this->validate1 ($address, $our_address, $our_domain, $host); if ( $v == EmailValidator::EV_VALID || $v == EmailValidator::EV_INVALID ) { return $v; } // continue when status unknown } } else { // use the hostname if no MX records available return $this->validate1 ($address, $our_address, $our_domain, $parts[1]); } return EmailValidator::EV_UNKNOWN; } private function validate1 ($address, $our_address, $our_domain, $host) { $this->server_response = ''; $sock = socket_create (AF_INET, SOCK_STREAM, SOL_TCP); if ( $sock === false ) return EmailValidator::EV_UNKNOWN; if ( socket_connect ($sock, $host, 25) === false ) { socket_close ($sock); return EmailValidator::EV_UNKNOWN; } $buf = ''; socket_recv ($sock, &$buf, 1000, 0); if ( $buf[0] != '2' ) { socket_close ($sock); $this->server_response = $buf; return EmailValidator::EV_UNKNOWN; } $buf = "EHLO $our_domain\r\n"; socket_send ($sock, $buf, strlen ($buf), 0); $buf = ''; socket_recv ($sock, &$buf, 1000, 0); if ( $buf[0] != '2' ) { $buf = "HELO $our_domain\r\n"; socket_send ($sock, $buf, strlen ($buf), 0); $buf = ''; socket_recv ($sock, &$buf, 1000, 0); if ( $buf[0] != '2' ) { socket_close ($sock); $this->server_response = $buf; return EmailValidator::EV_UNKNOWN; } } $buf = "MAIL FROM:<$our_address>\r\n"; socket_send ($sock, $buf, strlen ($buf), 0); $buf = ''; socket_recv ($sock, &$buf, 1000, 0); if ( $buf[0] != '2' ) { socket_close ($sock); $this->server_response = $buf; return EmailValidator::EV_UNKNOWN; } $buf = "RCPT TO:<$address>\r\n"; socket_send ($sock, $buf, strlen ($buf), 0); $buf = ''; socket_recv ($sock, &$buf, 1000, 0); socket_close ($sock); $this->server_response = $buf; if ( $buf[0] == '2' ) return EmailValidator::EV_VALID; else if ( $buf[0] == '5' ) return EmailValidator::EV_INVALID; return EmailValidator::EV_UNKNOWN; } } ?>