1,105,232 Community Members

check if email address is correct without sending email?

Member Avatar
nikesh.yadav
Posting Whiz in Training
220 posts since Feb 2008
Reputation Points: 4 [?]
Q&As Helped to Solve: 21 [?]
Skill Endorsements: 0 [?]
 
1
 

hi all,
i m working on the email validation i ve to check email id either it is correct or not but i cannt send mail on them. ie when i validate all email id client send newsletter and they should not bounce.

thanx in advance

Member Avatar
buddylee17
Practically a Master Poster
668 posts since Nov 2007
Reputation Points: 216 [?]
Q&As Helped to Solve: 137 [?]
Skill Endorsements: 2 [?]
 
0
 

Here's a function I use sometimes. It explodes the address to get the domain name. It then checks for a Mail Exchange on the domain. This will work for bogus domain names but won't do anything for email addresses like bill@hotmail.com or suzie@yahoo.com . Try to keep in mind, there is no sure-fire way to authenticate an email address without an email and a corresponding response.

function EmailValidation($email)
 { 
    $email = htmlspecialchars(stripslashes(strip_tags($email))); //parse unnecessary characters to prevent exploits
    
    if ( eregi ( '[a-z||0-9]@[a-z||0-9].[a-z]', $email ) ) 
	{ //checks to make sure the email address is in a valid format
    $domain = explode( "@", $email ); //get the domain name
        if ( @fsockopen ($domain[1],80,$errno,$errstr,3)) 
		{
            //if the connection can be established, the email address is probably valid
            return true;
        } else 
		{
            return false; //if a connection cannot be established return false
        }
    return false; //if email address is an invalid format return false
  }
}
Member Avatar
digital-ether
Nearly a Posting Virtuoso
1,264 posts since Sep 2005
Reputation Points: 399 [?]
Q&As Helped to Solve: 102 [?]
Skill Endorsements: 11 [?]
Team Colleague
 
1
 

hi all,
i m working on the email validation i ve to check email id either it is correct or not but i cannt send mail on them. ie when i validate all email id client send newsletter and they should not bounce.

thanx in advance

You can validate if an email matches the email syntax according to the SMTP protocol. However, this doensn't mean the email account actually exists on the domain you're trying to send email to.

To make sure the email address exsits, you will have to make an actual SMTP connection to the remote host/domain you want to send email to, and check if the email address is rejected or not. Usually, if it isn't rejected, then it means the email will be received by the server, this still doesn't mean it exists or the email will actually reach its recipient, since it could be routed to a catch all email or just trashed depending on the setup of the remote SMTP server.
It just means that the email you send will not bounce.

Here is an example of an SMTP connection:

http://www.yuki-onna.co.uk/email/smtp.html

In PHP, you'll just have to make a TCP connection to the remove SMTP server, and send it the SMTP commands up to the:

RCPT TO: mail@otherdomain.ext

If the server rejects the email account it will reply with something other then "250 ok".

A simple example:

Lets say the email address is user@example.com

$email = 'user@example.com';
list($user, $domain) = explode('@', $email);

$port = 25; // default smtp port

$sock = fsockopen($domain, $port);
if ($sock) {

fputs($sock, 'HELO mydomain.com');
$reply = fgets($sock); // not interesting

fputs($sock, 'MAIL FROM: user@mydomain.com');
$reply = fgets($sock); // not interesting

fputs($sock, 'RCPT TO: '.$email);
$reply = fgets($sock); // interesting

list($code, $msg) = explode(' ', $reply);

if ($code == '250') {
  // you received 250 so the email address is accepted
} else {
 // something went wrong, the email will most likely bounce
}

fclose($sock);

}
Member Avatar
digital-ether
Nearly a Posting Virtuoso
1,264 posts since Sep 2005
Reputation Points: 399 [?]
Q&As Helped to Solve: 102 [?]
Skill Endorsements: 11 [?]
Team Colleague
 
1
 

By the way, some email domains will actually have other domains relaying emails in their behalf.

In order to validate the domain portion of the email, and find the correct host to connect to (the authoritative mail server for that domain) you need to make a DNS query of type MX.

see: http://www.php.net/manual/en/function.getmxrr.php

Member Avatar
nikesh.yadav
Posting Whiz in Training
220 posts since Feb 2008
Reputation Points: 4 [?]
Q&As Helped to Solve: 21 [?]
Skill Endorsements: 0 [?]
 
0
 

hi digitalethe,
i already tried getmxrr,
but i m really impresed with your reply and this url http://www.yuki-onna.co.uk/email/smtp.html is really great i wass now that something like this is possible in php but tring to find since 15 days and at last u give the answer.

Really so thankfull to u.

Member Avatar
digital-ether
Nearly a Posting Virtuoso
1,264 posts since Sep 2005
Reputation Points: 399 [?]
Q&As Helped to Solve: 102 [?]
Skill Endorsements: 11 [?]
Team Colleague
 
1
 

hi digitalethe,
i already tried getmxrr,
but i m really impresed with your reply and this url http://www.yuki-onna.co.uk/email/smtp.html is really great i wass now that something like this is possible in php but tring to find since 15 days and at last u give the answer.

Really so thankfull to u.

I wrote a class that you can use for the validation with SMTP. It will only work on Linux as Windows doesn't have the getmxrr() function or other DNS functions.
If needed on windows, one can replace the DNS functions with the Pear Net_DNS library.
http://pear.php.net/package/Net_DNS

/**
 * 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
 * @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;
		}
	}


}

Example Usage:

// the email to validate
$email = 'joe@gmail.com';
// an optional sender
$sender = 'user@example.com';
// 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've added it to the code snippets:
http://www.daniweb.com/code/snippet940.html

Member Avatar
nikesh.yadav
Posting Whiz in Training
220 posts since Feb 2008
Reputation Points: 4 [?]
Q&As Helped to Solve: 21 [?]
Skill Endorsements: 0 [?]
 
0
 

k thnaks i will try

Member Avatar
pritaeas
mod_pritaeas
11,285 posts since Jul 2006
Reputation Points: 1,420 [?]
Q&As Helped to Solve: 1,829 [?]
Skill Endorsements: 154 [?]
Moderator
Featured
Sponsor
 
0
 

Be careful with connecting to random/unknown hosts. It can get you into legal trouble. Just a warning, since it happened to me...

Member Avatar
digital-ether
Nearly a Posting Virtuoso
1,264 posts since Sep 2005
Reputation Points: 399 [?]
Q&As Helped to Solve: 102 [?]
Skill Endorsements: 11 [?]
Team Colleague
 
0
 

Be careful with connecting to random/unknown hosts. It can get you into legal trouble. Just a warning, since it happened to me...

I don't see how you could get into any legal issues for connecting to an SMTP server to ask if an email exists, given that the email address you want to query is solicited and not spam?

Member Avatar
pritaeas
mod_pritaeas
11,285 posts since Jul 2006
Reputation Points: 1,420 [?]
Q&As Helped to Solve: 1,829 [?]
Skill Endorsements: 154 [?]
Moderator
Featured
Sponsor
 
0
 

Some companies do not like such access and will report it is a cracking attempt. Maybe it's not as big an issue as it was several years ago, but there are ignorant companies out there. I didn't understand it either, but obviously it happens.

Member Avatar
digital-ether
Nearly a Posting Virtuoso
1,264 posts since Sep 2005
Reputation Points: 399 [?]
Q&As Helped to Solve: 102 [?]
Skill Endorsements: 11 [?]
Team Colleague
 
0
 

Some companies do not like such access and will report it is a cracking attempt. Maybe it's not as big an issue as it was several years ago, but there are ignorant companies out there. I didn't understand it either, but obviously it happens.

Well, sorry that happened to you.

Member Avatar
nikesh.yadav
Posting Whiz in Training
220 posts since Feb 2008
Reputation Points: 4 [?]
Q&As Helped to Solve: 21 [?]
Skill Endorsements: 0 [?]
 
0
 

hope i will not happen with me

Member Avatar
tgbyhn
Newbie Poster
8 posts since Sep 2008
Reputation Points: 5 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
1
 

Hi digital-ether,

I wrote a class that you can use for the validation with SMTP. It will only work on Linux as Windows doesn't have the getmxrr() function or other DNS functions.
If needed on windows, one can replace the DNS functions with the Pear Net_DNS library.
http://pear.php.net/package/Net_DNS

Thanks for the SMTP validation class, I've made a few of tweaks, and thought I'd share with everyone.

1) The new line terminator should include a carriage return (\r), otherwise it won't work with some Windows MTAs, eg. Hotmail.
2) Now checks MX servers in order of priority.
3) fread should be executed before fwrite, as some MTAs with anti-spam mechanisms will close the connection (and blacklist the client) if the HELO is sent before the server's greeting.
4) Consider code 451 and 452 as success, as if the MTAs uses greylisting (451) it will take a while before we can really verify (comment this bit out if not needed), and if there's a lack of space on the MTA it should be temporary (452), and resolved eventually, but the mailbox exists.
5) Using explode(' ', $code) doesn't work in all cases, as some MTAs use a '-'. Replaced with substr.


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
 * @contributors 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)) {
				socket_set_blocking($this->sock, false);
				break;
			}
		}
		$code = substr($reply, 0, 3);
		if($code != '220') {
			// MTA gave an error...
			return false;
		}

		// did we get a TCP socket
		if ($this->sock) {
			$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;
				}
			}

			// 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
			$code = substr($reply, 0, 3);
			
			if ($code == '250') {
				// you received 250 so the email address was accepted
				return true;
			} elseif ($code == '451' || $code == '452') {
				// you received 45[12] so the email address was greylisted (or some temporary error occured on the MTA) - so assume mailbox exists
				return true;
			}
		
		}
		
		return false;
	}


	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;
	}
	
	/**
	 * 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;
		}
	}

}
?>
Member Avatar
digital-ether
Nearly a Posting Virtuoso
1,264 posts since Sep 2005
Reputation Points: 399 [?]
Q&As Helped to Solve: 102 [?]
Skill Endorsements: 11 [?]
Team Colleague
 
0
 

Hi tgbyhn,

Thanks for making those fixes.

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'd think there is a better way to do this, a more standard way? maybe one defined in the SMTP specs?

Is there a delimiter to wait for when making a read, to know the MTA has finished the response?

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?

edit: Do you think using the "verify" command before trying 'RCPT TO' would be more polite and should give less chance of blacklisting?

Thanks for your time.

Member Avatar
tgbyhn
Newbie Poster
8 posts since Sep 2008
Reputation Points: 5 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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;
		}
	}

}
?>
Member Avatar
digital-ether
Nearly a Posting Virtuoso
1,264 posts since Sep 2005
Reputation Points: 399 [?]
Q&As Helped to Solve: 102 [?]
Skill Endorsements: 11 [?]
Team Colleague
 
0
 

Beautiful... :)

Member Avatar
digital-ether
Nearly a Posting Virtuoso
1,264 posts since Sep 2005
Reputation Points: 399 [?]
Q&As Helped to Solve: 102 [?]
Skill Endorsements: 11 [?]
Team Colleague
 
0
 

I'm at a net cafe right now so I can't test the class, but a few things after looking at it a bit more:

1) The emails are separated into user and domain portions using explode('@', $email); I remember reading that @ can occur in email username portion if escaped with a backslash, or quoted.

But since domains cannot have @ in them, I think explode() would work by taking the last piece as the domain, and the remaining as the email.

function setEmail($email) {
		$parts = explode('@', $email);
		$this->domain = array_pop($parts);
		$this->user= implode('@', $parts);
	}

2) The class opens and closes a socket connection to the MTA on each email it validates. Maybe a grouping of emails on the same host could keep the socket alive, and just issue RCPT commands to the open connection? Not even sure if that is supported by SMTP?

I'll see what I can do when I get to a development machine.

Member Avatar
digital-ether
Nearly a Posting Virtuoso
1,264 posts since Sep 2005
Reputation Points: 399 [?]
Q&As Helped to Solve: 102 [?]
Skill Endorsements: 11 [?]
Team Colleague
 
0
 

Hey Guys,

I've updated the SMTP_validateEmail class with better email address parsing, and it can now validate multiple emails on the same domain via a single socket connection for efficiency.

Also added is support for Windows clients, which requires Net_DNS.
http://pear.php.net/package/Net_DNS

<?php
 
 /**
 * Validate Email Addresses 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 {

 /**
  * PHP Socket resource to remote MTA
  * @var resource $sock 
  */
 var $sock;

 /**
  * Current User being validated
  */
 var $user;
 /**
  * Current domain where user is being validated
  */
 var $domain;
 /**
  * List of domains to validate users on
  */
 var $domains;
 /**
  * SMTP Port
  */
 var $port = 25;
 /**
  * Maximum Connection Time to an MTA 
  */
 var $max_conn_time = 30;
 /**
  * Maximum time to read from socket
  */
 var $max_read_time = 5;
 
 /**
  * username of sender
  */
 var $from_user = 'user';
 /**
  * Host Name of sender
  */
 var $from_domain = 'localhost';
 
 /**
  * Nameservers to use when make DNS query for MX entries
  * @var Array $nameservers 
  */
 var $nameservers = array(
	'192.168.0.1'
);
 
 var $debug = false;

 /**
  * Initializes the Class
  * @return SMTP_validateEmail Instance
  * @param $email Array[optional] List of Emails to Validate
  * @param $sender String[optional] Email of validator
  */
 function SMTP_validateEmail($emails = false, $sender = false) {
  if ($emails) {
   $this->setEmails($emails);
  }
  if ($sender) {
   $this->setSenderEmail($sender);
  }
 }
 
 function _parseEmail($email) {
 	$parts = explode('@', $email);
	$domain = array_pop($parts);
	$user= implode('@', $parts);
	return array($user, $domain);
 }
 
 /**
  * Set the Emails to validate
  * @param $emails Array List of Emails
  */
 function setEmails($emails) {
 	foreach($emails as $email) {
		list($user, $domain) = $this->_parseEmail($email);
		if (!isset($this->domains[$domain])) {
			 $this->domains[$domain] = array();
		}
		$this->domains[$domain][] = $user;
	}
 }
 
 /**
  * Set the Email of the sender/validator
  * @param $email String
  */
 function setSenderEmail($email) {
	$parts = $this->_parseEmail($email);
	$this->from_user = $parts[0];
	$this->from_domain = $parts[1];
 }
 
 /**
 * Validate Email Addresses
 * @param String $emails Emails to validate (recipient emails)
 * @param String $sender Sender's Email
 * @return Array Associative List of Emails and their validation results
 */
 function validate($emails = false, $sender = false) {
 	
  $results = array();

  if ($emails) {
   $this->setEmails($emails);
  }
  if ($sender) {
   $this->setSenderEmail($sender);
  }

  // query the MTAs on each Domain
  foreach($this->domains as $domain=>$users) {
  	
	 $mxs = array();
  
	  // retrieve SMTP Server via MX query on domain
	  list($hosts, $mxweights) = $this->queryMX($domain);

	  // 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);
	   $this->debug("<<<\n$reply");
	   
	   preg_match('/^([0-9]{3}) /ims', $reply, $matches);
	   $code = isset($matches[1]) ? $matches[1] : '';
	
	   if($code != '220') {
	    // MTA gave an error...
	    foreach($users as $user) {
	    	$results[$user.'@'.$domain] = false;
		}
		continue;
	   }

	   // say helo
	   $this->send("HELO ".$this->from_domain);
	   // tell of sender
	   $this->send("MAIL FROM: <".$this->from_user.'@'.$this->from_domain.">");
	   
	   // ask for each recepient on this domain
	   foreach($users as $user) {
	   
		   // ask of recepient
		   $reply = $this->send("RCPT TO: <".$user.'@'.$domain.">");
		   
		    // 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
		    $results[$user.'@'.$domain] = 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
			$results[$user.'@'.$domain] = true;
		   } else {
		   	$results[$user.'@'.$domain] = false;
		   }
	   
	   }
	   
	   // quit
	   $this->send("quit");
	   // close socket
	   fclose($this->sock);
	  
	  }
 	}
	return $results;
 }


 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;
 }
 
 /**
  * Query DNS server for MX entries
  * @return 
  */
 function queryMX($domain) {
 	$hosts = array();
	$mxweights = array();
 	if (function_exists('getmxrr')) {
 		getmxrr($domain, $hosts, $mxweights);
 	} else {
 		// windows, we need Net_DNS
		require_once 'Net/DNS.php';

		$resolver = new Net_DNS_Resolver();
		$resolver->debug = $this->debug;
		// nameservers to query
		$resolver->nameservers = $this->nameservers;
		$resp = $resolver->query($domain, 'MX');
		if ($resp) {
			foreach($resp->answer as $answer) {
				$hosts[] = $answer->exchange;
				$mxweights[] = $answer->preference;
			}
		}
		
 	}
	return array($hosts, $mxweights);
 }
 
 /**
  * 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 htmlentities($str);
  }
 }

}

 
?>
Member Avatar
tgbyhn
Newbie Poster
8 posts since Sep 2008
Reputation Points: 5 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi digital-ether,

Thanks for your update, however I've just noticed a bug in my last update, line 141 was incorrectly adding an array, which was breaking things if there was no MX record.

$mxs[$this->domain] = '0';

Hey Guys,

I've updated the SMTP_validateEmail class with better email address parsing, and it can now validate multiple emails on the same domain via a single socket connection for efficiency.

Also added is support for Windows clients, which requires Net_DNS.
http://pear.php.net/package/Net_DNS

Here is the latest update including your update:

<?php
 
 /**
 * Validate Email Addresses 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 {

 /**
  * PHP Socket resource to remote MTA
  * @var resource $sock 
  */
 var $sock;

 /**
  * Current User being validated
  */
 var $user;
 /**
  * Current domain where user is being validated
  */
 var $domain;
 /**
  * List of domains to validate users on
  */
 var $domains;
 /**
  * SMTP Port
  */
 var $port = 25;
 /**
  * Maximum Connection Time to an MTA 
  */
 var $max_conn_time = 30;
 /**
  * Maximum time to read from socket
  */
 var $max_read_time = 5;
 
 /**
  * username of sender
  */
 var $from_user = 'user';
 /**
  * Host Name of sender
  */
 var $from_domain = 'localhost';
 
 /**
  * Nameservers to use when make DNS query for MX entries
  * @var Array $nameservers 
  */
 var $nameservers = array(
	'192.168.0.1'
);
 
 var $debug = false;

 /**
  * Initializes the Class
  * @return SMTP_validateEmail Instance
  * @param $email Array[optional] List of Emails to Validate
  * @param $sender String[optional] Email of validator
  */
 function SMTP_validateEmail($emails = false, $sender = false) {
  if ($emails) {
   $this->setEmails($emails);
  }
  if ($sender) {
   $this->setSenderEmail($sender);
  }
 }
 
 function _parseEmail($email) {
 	$parts = explode('@', $email);
	$domain = array_pop($parts);
	$user= implode('@', $parts);
	return array($user, $domain);
 }
 
 /**
  * Set the Emails to validate
  * @param $emails Array List of Emails
  */
 function setEmails($emails) {
 	foreach($emails as $email) {
		list($user, $domain) = $this->_parseEmail($email);
		if (!isset($this->domains[$domain])) {
			 $this->domains[$domain] = array();
		}
		$this->domains[$domain][] = $user;
	}
 }
 
 /**
  * Set the Email of the sender/validator
  * @param $email String
  */
 function setSenderEmail($email) {
	$parts = $this->_parseEmail($email);
	$this->from_user = $parts[0];
	$this->from_domain = $parts[1];
 }
 
 /**
 * Validate Email Addresses
 * @param String $emails Emails to validate (recipient emails)
 * @param String $sender Sender's Email
 * @return Array Associative List of Emails and their validation results
 */
 function validate($emails = false, $sender = false) {
 	
  $results = array();

  if ($emails) {
   $this->setEmails($emails);
  }
  if ($sender) {
   $this->setSenderEmail($sender);
  }

  // query the MTAs on each Domain
  foreach($this->domains as $domain=>$users) {
  	
	 $mxs = array();
  
	  // retrieve SMTP Server via MX query on domain
	  list($hosts, $mxweights) = $this->queryMX($domain);

	  // retrieve MX priorities
	  for($n=0; $n < count($hosts); $n++){
	   $mxs[$hosts[$n]] = $mxweights[$n];
	  }
	  asort($mxs);
	
	  // last fallback is the original domain
	  $mxs[$this->domain] = '0';
	  
	  $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);
	   $this->debug("<<<\n$reply");
	   
	   preg_match('/^([0-9]{3}) /ims', $reply, $matches);
	   $code = isset($matches[1]) ? $matches[1] : '';
	
	   if($code != '220') {
	    // MTA gave an error...
	    foreach($users as $user) {
	    	$results[$user.'@'.$domain] = false;
		}
		continue;
	   }

	   // say helo
	   $this->send("HELO ".$this->from_domain);
	   // tell of sender
	   $this->send("MAIL FROM: <".$this->from_user.'@'.$this->from_domain.">");
	   
	   // ask for each recepient on this domain
	   foreach($users as $user) {
	   
		   // ask of recepient
		   $reply = $this->send("RCPT TO: <".$user.'@'.$domain.">");
		   
		    // 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
		    $results[$user.'@'.$domain] = 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
			$results[$user.'@'.$domain] = true;
		   } else {
		   	$results[$user.'@'.$domain] = false;
		   }
	   
	   }
	   
	   // quit
	   $this->send("quit");
	   // close socket
	   fclose($this->sock);
	  
	  }
 	}
	return $results;
 }


 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;
 }
 
 /**
  * Query DNS server for MX entries
  * @return 
  */
 function queryMX($domain) {
 	$hosts = array();
	$mxweights = array();
 	if (function_exists('getmxrr')) {
 		getmxrr($domain, $hosts, $mxweights);
 	} else {
 		// windows, we need Net_DNS
		require_once 'Net/DNS.php';

		$resolver = new Net_DNS_Resolver();
		$resolver->debug = $this->debug;
		// nameservers to query
		$resolver->nameservers = $this->nameservers;
		$resp = $resolver->query($domain, 'MX');
		if ($resp) {
			foreach($resp->answer as $answer) {
				$hosts[] = $answer->exchange;
				$mxweights[] = $answer->preference;
			}
		}
		
 	}
	return array($hosts, $mxweights);
 }
 
 /**
  * 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 htmlentities($str);
  }
 }

}

 
?>

Adnan

Member Avatar
whytek
Newbie Poster
2 posts since Sep 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi Folks, I have done a little on the class SMTP_validateEmail this morning, as it is something I have been meaning to implement for a few months. - nice work so far, thanks.

Basically, I would like to get a confirmed "NO" if the email address is 553, like when Joe Public typoes his address in as jowpublic@domain.com on my site, and a confimed "YES" if I can get at least a postive 25x or 45x from the MX. In all other cases, what I am going to do is remind the user that I was unable to verify the address and ask them to look at it twice for typos before finally hitting submit.

So, as you can see, I have moved away from just returning true/false, to also returning some text strings to check for. This could maybe be done in a better way.

I have also added a nasty hack to work on windoze without NET_DNS, the ability to set nameservers when initialising the class and I have added to, and formatted the debug output a little. There's also a few other little changes in there as you can see.

Thanks again.

K.

Attachments code.txt (8.84KB)
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article