Hi digital-ether,
Theres a bit of guessing in that class, in the reading from the socket.
The class currently reads from the socket between client and MTA in non-blocking mode.
socket_set_blocking($this->sock, false);
It tries to read from the socket until it receives a few bytes, and will finish reading until that stream is done.
function send($msg) {
fwrite($this->sock, $msg."\r\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;
}
The timer:
$this->microtime_float() - $start > $this->max_read_time )
makes sure it doesn't keep reading for ever.
I've done a few more tweaks, and a bit more testing, I've put it in blocking more (I tried it originally, but forgot about the MTA's greeting so it didn't work). One thing I noticed that using substr isn't really the correct way, as some commands give multiline responses, and I've found out why sometimes there is a hyphen after the response code, the hyphen is used on all the response lines, apart from the last line which should be a space. (see section 3.5.1 in RFC 2821)I'd think there is a better way to do this, a more standard way? maybe one defined in the SMTP specs?
I've just had a quick read through relevant bits of RFC 2821, I think blocking mode is the way to go, as the MTA should always respond with something. I've put in a socket timeout using PHP's built-in function stream_set_timeout(), so if nothing comes back the script will timeout and return false.Is there a delimiter to wait for when making a read, to know the MTA has finished the response?
Not afaik, though I could be wrong, and an easy way around it would be to set the buffer to a fairly high figure, so any replies will be read as a whole.At the moment, if the MTA doesn't respond before SMTP_validateEmail::max_read_time then the read will stop, and nothing will be received.
I started with having reads in blocking mode, but with hotmail it hanged on the first read? Maybe it was due to not reading the HELO first, and now that you're updated that reading in blocking mode should work with all MTAs?
In Hotmail's case it was because of the missing "\r", I know a lot of Windows apps need the "\r" as well as the "\n", whereas Linux/Unix is quite happy with just a "\n". I've just tested the new tweaks on lots of different servers, including Hotmail, Google, Yahoo, and some other, and they all work nicely.edit: Do you think using the "verify" command before trying 'RCPT TO' would be more polite and should give less chance of blacklisting?
The VRFY command doesn't work in all cases, there's a recommendation in RFC 2821 that it can be a security risk, and that it may be better disabled - VRFY doesn't work for Google, Hotmail or Yahoo. I think the RCPT approach would give a more accurate result.
Adnan
<?
/**
* 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 gabe@fijiwebdesign.com
* @contributers adnan@barakatdesigns.net
* @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, $mxweights);
// retrieve MX priorities
for($n=0; $n < count($hosts); $n++){
$mxs[$hosts[$n]] = $mxweights[$n];
}
asort($mxs);
// last fallback is the original domain
array_push($mxs, $this->domain);
$this->debug(print_r($mxs, 1));
$timeout = $this->max_conn_time/count($hosts);
// try each host
while(list($host) = each($mxs)) {
// connect to SMTP server
$this->debug("try $host:$this->port\n");
if ($this->sock = fsockopen($host, $this->port, $errno, $errstr, (float) $timeout)) {
stream_set_timeout($this->sock, $this->max_read_time);
break;
}
}
// did we get a TCP socket
if ($this->sock) {
$reply = fread($this->sock, 2082);
preg_match('/^([0-9]{3}) /ims', $reply, $matches);
$code = isset($matches[1]) ? $matches[1] : '';
if($code != '220') {
// MTA gave an error...
return false;
}
// 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
preg_match('/^([0-9]{3}) /ims', $reply, $matches);
$code = isset($matches[1]) ? $matches[1] : '';
if ($code == '250') {
// you received 250 so the email address was accepted
return true;
} elseif ($code == '451' || $code == '452') {
// you received 451 so the email address was greylisted (or some temporary error occured on the MTA) - so assume is ok
return true;
}
}
return false;
}
function send($msg) {
fwrite($this->sock, $msg."\r\n");
$reply = fread($this->sock, 2082);
$this->debug(">>>\n$msg\n");
$this->debug("<<<\n$reply");
return $reply;
}
/**
* 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;
}
}
}
?>