check if email address is correct without sending email?

Reply

Join Date: Sep 2005
Posts: 1,075
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: check if email address is correct without sending email?

 
0
  #11
Aug 27th, 2008
Originally Posted by pritaeas View Post
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.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 213
Reputation: nikesh.yadav is an unknown quantity at this point 
Solved Threads: 17
nikesh.yadav's Avatar
nikesh.yadav nikesh.yadav is offline Offline
Posting Whiz in Training

Re: check if email address is correct without sending email?

 
0
  #12
Aug 27th, 2008
hope i will not happen with me
Help as an alias

I think programming is great................
Tour Travel weblink by me and about Tour ,
Go To My Home Page and I m in Webdevelopment.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 8
Reputation: tgbyhn is an unknown quantity at this point 
Solved Threads: 1
tgbyhn tgbyhn is offline Offline
Newbie Poster

Re: check if email address is correct without sending email?

 
1
  #13
Sep 7th, 2008
Hi digital-ether,

Originally Posted by digital-ether View Post
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

  1. <?
  2. /**
  3.  * Validate an Email Address Via SMTP
  4.  * This queries the SMTP server to see if the email address is accepted.
  5.  * @copyright http://creativecommons.org/licenses/by/2.0/ - Please keep this comment intact
  6.  * @author gabe@fijiwebdesign.com
  7.  * @contributors adnan@barakatdesigns.net
  8.  * @version 0.1a
  9.  */
  10. class SMTP_validateEmail {
  11.  
  12. var $sock;
  13.  
  14. var $user;
  15. var $domain;
  16. var $port = 25;
  17. var $max_conn_time = 30;
  18. var $max_read_time = 5;
  19.  
  20. var $from_user = 'user';
  21. var $from_domain = 'localhost';
  22.  
  23. var $debug = false;
  24.  
  25. function SMTP_validateEmail($email = false, $sender = false) {
  26. if ($email) {
  27. $this->setEmail($email);
  28. }
  29. if ($sender) {
  30. $this->setSenderEmail($sender);
  31. }
  32. }
  33.  
  34. function setEmail($email) {
  35. list($user, $domain) = explode('@', $email);
  36. $this->user = $user;
  37. $this->domain = $domain;
  38. }
  39.  
  40. function setSenderEmail($email) {
  41. list($user, $domain) = explode('@', $email);
  42. $this->from_user = $user;
  43. $this->from_domain = $domain;
  44. }
  45.  
  46. /**
  47. * Validate an Email Address
  48. * @param String $email Email to validate (recipient email)
  49. * @param String $sender Senders Email
  50. */
  51. function validate($email = false, $sender = false) {
  52. if ($email) {
  53. $this->setEmail($email);
  54. }
  55. if ($sender) {
  56. $this->setSenderEmail($sender);
  57. }
  58. // retrieve SMTP Server via MX query on domain
  59. $result = getmxrr($this->domain, $hosts, $mxweights);
  60.  
  61. // retrieve MX priorities
  62. for($n=0; $n < count($hosts); $n++){
  63. $mxs[$hosts[$n]] = $mxweights[$n];
  64. }
  65.  
  66. asort($mxs);
  67.  
  68. // last fallback is the original domain
  69. array_push($mxs, $this->domain);
  70.  
  71. $this->debug(print_r($mxs, 1));
  72.  
  73. $timeout = $this->max_conn_time/count($hosts);
  74.  
  75. // try each host
  76. while(list($host) = each($mxs)) {
  77. // connect to SMTP server
  78. $this->debug("try $host:$this->port\n");
  79. if ($this->sock = fsockopen($host, $this->port, $errno, $errstr, (float) $timeout)) {
  80. socket_set_blocking($this->sock, false);
  81. break;
  82. }
  83. }
  84. $code = substr($reply, 0, 3);
  85. if($code != '220') {
  86. // MTA gave an error...
  87. return false;
  88. }
  89.  
  90. // did we get a TCP socket
  91. if ($this->sock) {
  92. $resp = '';
  93. $start = $this->microtime_float();
  94. while(1) {
  95. $reply = fread($this->sock, 2082);
  96. $resp .= $reply;
  97. if (($resp != '' && trim($reply) == '')
  98. || ($this->microtime_float() - $start > $this->max_read_time )) {
  99. break;
  100. }
  101. }
  102.  
  103. // say helo
  104. $this->send("HELO ".$this->from_domain);
  105. // tell of sender
  106. $this->send("MAIL FROM: <".$this->from_user.'@'.$this->from_domain.">");
  107. // ask of recepient
  108. $reply = $this->send("RCPT TO: <".$this->user.'@'.$this->domain.">");
  109. // quit
  110. $this->send("quit");
  111. // close socket
  112. fclose($this->sock);
  113. // get code and msg from response
  114. $code = substr($reply, 0, 3);
  115.  
  116. if ($code == '250') {
  117. // you received 250 so the email address was accepted
  118. return true;
  119. } elseif ($code == '451' || $code == '452') {
  120. // you received 45[12] so the email address was greylisted (or some temporary error occured on the MTA) - so assume mailbox exists
  121. return true;
  122. }
  123.  
  124. }
  125.  
  126. return false;
  127. }
  128.  
  129.  
  130. function send($msg) {
  131. fwrite($this->sock, $msg."\r\n");
  132.  
  133. $resp = '';
  134. $start = $this->microtime_float();
  135. while(1) {
  136. $reply = fread($this->sock, 2082);
  137. $resp .= $reply;
  138. if (($resp != '' && trim($reply) == '')
  139. || ($this->microtime_float() - $start > $this->max_read_time )) {
  140. break;
  141. }
  142. }
  143.  
  144. $this->debug(">>>\n$msg\n");
  145. $this->debug("<<<\n$resp");
  146.  
  147. return $resp;
  148. }
  149.  
  150. /**
  151. * Simple function to replicate PHP 5 behaviour. http://php.net/microtime
  152. */
  153. function microtime_float() {
  154. list($usec, $sec) = explode(" ", microtime());
  155. return ((float)$usec + (float)$sec);
  156. }
  157.  
  158. function debug($str) {
  159. if ($this->debug) {
  160. echo $str;
  161. }
  162. }
  163.  
  164. }
  165. ?>
Last edited by tgbyhn; Sep 7th, 2008 at 10:05 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,075
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: check if email address is correct without sending email?

 
0
  #14
Sep 7th, 2008
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.
  1. 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.

  1. function send($msg) {
  2. fwrite($this->sock, $msg."\r\n");
  3.  
  4. $resp = '';
  5. $start = $this->microtime_float();
  6. while(1) {
  7. $reply = fread($this->sock, 2082);
  8. $resp .= $reply;
  9. if (($resp != '' && trim($reply) == '')
  10. || ($this->microtime_float() - $start > $this->max_read_time )) {
  11. break;
  12. }
  13. }
  14.  
  15. $this->debug(">>>\n$msg\n");
  16. $this->debug("<<<\n$resp");
  17.  
  18. return $resp;
  19. }

The timer:

  1. $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.
Last edited by digital-ether; Sep 7th, 2008 at 11:18 am.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 8
Reputation: tgbyhn is an unknown quantity at this point 
Solved Threads: 1
tgbyhn tgbyhn is offline Offline
Newbie Poster

Re: check if email address is correct without sending email?

 
0
  #15
Sep 7th, 2008
Hi digital-ether,

Originally Posted by digital-ether View Post
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.
  1. 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.

  1. function send($msg) {
  2. fwrite($this->sock, $msg."\r\n");
  3.  
  4. $resp = '';
  5. $start = $this->microtime_float();
  6. while(1) {
  7. $reply = fread($this->sock, 2082);
  8. $resp .= $reply;
  9. if (($resp != '' && trim($reply) == '')
  10. || ($this->microtime_float() - $start > $this->max_read_time )) {
  11. break;
  12. }
  13. }
  14.  
  15. $this->debug(">>>\n$msg\n");
  16. $this->debug("<<<\n$resp");
  17.  
  18. return $resp;
  19. }

The timer:

  1. $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)

Originally Posted by digital-ether View Post
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.

Originally Posted by digital-ether View Post
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.

Originally Posted by digital-ether View Post
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.

Originally Posted by digital-ether View Post
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

  1. <?
  2. /**
  3.  * Validate an Email Address Via SMTP
  4.  * This queries the SMTP server to see if the email address is accepted.
  5.  * @copyright http://creativecommons.org/licenses/by/2.0/ - Please keep this comment intact
  6.  * @author gabe@fijiwebdesign.com
  7.  * @contributers adnan@barakatdesigns.net
  8.  * @version 0.1a
  9.  */
  10. class SMTP_validateEmail {
  11.  
  12. var $sock;
  13.  
  14. var $user;
  15. var $domain;
  16. var $port = 25;
  17. var $max_conn_time = 30;
  18. var $max_read_time = 5;
  19.  
  20. var $from_user = 'user';
  21. var $from_domain = 'localhost';
  22.  
  23. var $debug = false;
  24.  
  25. function SMTP_validateEmail($email = false, $sender = false) {
  26. if ($email) {
  27. $this->setEmail($email);
  28. }
  29. if ($sender) {
  30. $this->setSenderEmail($sender);
  31. }
  32. }
  33.  
  34. function setEmail($email) {
  35. list($user, $domain) = explode('@', $email);
  36. $this->user = $user;
  37. $this->domain = $domain;
  38. }
  39.  
  40. function setSenderEmail($email) {
  41. list($user, $domain) = explode('@', $email);
  42. $this->from_user = $user;
  43. $this->from_domain = $domain;
  44. }
  45.  
  46. /**
  47. * Validate an Email Address
  48. * @param String $email Email to validate (recipient email)
  49. * @param String $sender Senders Email
  50. */
  51. function validate($email = false, $sender = false) {
  52. if ($email) {
  53. $this->setEmail($email);
  54. }
  55. if ($sender) {
  56. $this->setSenderEmail($sender);
  57. }
  58. // retrieve SMTP Server via MX query on domain
  59. $result = getmxrr($this->domain, $hosts, $mxweights);
  60.  
  61. // retrieve MX priorities
  62. for($n=0; $n < count($hosts); $n++){
  63. $mxs[$hosts[$n]] = $mxweights[$n];
  64. }
  65.  
  66. asort($mxs);
  67.  
  68. // last fallback is the original domain
  69. array_push($mxs, $this->domain);
  70.  
  71. $this->debug(print_r($mxs, 1));
  72.  
  73. $timeout = $this->max_conn_time/count($hosts);
  74.  
  75. // try each host
  76. while(list($host) = each($mxs)) {
  77. // connect to SMTP server
  78. $this->debug("try $host:$this->port\n");
  79. if ($this->sock = fsockopen($host, $this->port, $errno, $errstr, (float) $timeout)) {
  80. stream_set_timeout($this->sock, $this->max_read_time);
  81. break;
  82. }
  83. }
  84.  
  85. // did we get a TCP socket
  86. if ($this->sock) {
  87. $reply = fread($this->sock, 2082);
  88. preg_match('/^([0-9]{3}) /ims', $reply, $matches);
  89. $code = isset($matches[1]) ? $matches[1] : '';
  90.  
  91. if($code != '220') {
  92. // MTA gave an error...
  93. return false;
  94. }
  95.  
  96. // say helo
  97. $this->send("HELO ".$this->from_domain);
  98. // tell of sender
  99. $this->send("MAIL FROM: <".$this->from_user.'@'.$this->from_domain.">");
  100. // ask of recepient
  101. $reply = $this->send("RCPT TO: <".$this->user.'@'.$this->domain.">");
  102. // quit
  103. $this->send("quit");
  104. // close socket
  105. fclose($this->sock);
  106. // get code and msg from response
  107. preg_match('/^([0-9]{3}) /ims', $reply, $matches);
  108. $code = isset($matches[1]) ? $matches[1] : '';
  109.  
  110. if ($code == '250') {
  111. // you received 250 so the email address was accepted
  112. return true;
  113. } elseif ($code == '451' || $code == '452') {
  114. // you received 451 so the email address was greylisted (or some temporary error occured on the MTA) - so assume is ok
  115. return true;
  116. }
  117.  
  118. }
  119.  
  120. return false;
  121. }
  122.  
  123.  
  124. function send($msg) {
  125. fwrite($this->sock, $msg."\r\n");
  126.  
  127. $reply = fread($this->sock, 2082);
  128.  
  129. $this->debug(">>>\n$msg\n");
  130. $this->debug("<<<\n$reply");
  131.  
  132. return $reply;
  133. }
  134.  
  135. /**
  136. * Simple function to replicate PHP 5 behaviour. http://php.net/microtime
  137. */
  138. function microtime_float() {
  139. list($usec, $sec) = explode(" ", microtime());
  140. return ((float)$usec + (float)$sec);
  141. }
  142.  
  143. function debug($str) {
  144. if ($this->debug) {
  145. echo $str;
  146. }
  147. }
  148.  
  149. }
  150. ?>
Last edited by tgbyhn; Sep 7th, 2008 at 5:29 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,075
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: check if email address is correct without sending email?

 
0
  #16
Sep 7th, 2008
Beautiful...
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,075
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: check if email address is correct without sending email?

 
0
  #17
Sep 7th, 2008
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.

  1. function setEmail($email) {
  2. $parts = explode('@', $email);
  3. $this->domain = array_pop($parts);
  4. $this->user= implode('@', $parts);
  5. }

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.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,075
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: check if email address is correct without sending email?

 
0
  #18
Sep 9th, 2008
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

  1. <?php
  2.  
  3. /**
  4.  * Validate Email Addresses Via SMTP
  5.  * This queries the SMTP server to see if the email address is accepted.
  6.  * @copyright http://creativecommons.org/licenses/by/2.0/ - Please keep this comment intact
  7.  * @author gabe@fijiwebdesign.com
  8.  * @contributers adnan@barakatdesigns.net
  9.  * @version 0.1a
  10.  */
  11. class SMTP_validateEmail {
  12.  
  13. /**
  14.   * PHP Socket resource to remote MTA
  15.   * @var resource $sock
  16.   */
  17. var $sock;
  18.  
  19. /**
  20.   * Current User being validated
  21.   */
  22. var $user;
  23. /**
  24.   * Current domain where user is being validated
  25.   */
  26. var $domain;
  27. /**
  28.   * List of domains to validate users on
  29.   */
  30. var $domains;
  31. /**
  32.   * SMTP Port
  33.   */
  34. var $port = 25;
  35. /**
  36.   * Maximum Connection Time to an MTA
  37.   */
  38. var $max_conn_time = 30;
  39. /**
  40.   * Maximum time to read from socket
  41.   */
  42. var $max_read_time = 5;
  43.  
  44. /**
  45.   * username of sender
  46.   */
  47. var $from_user = 'user';
  48. /**
  49.   * Host Name of sender
  50.   */
  51. var $from_domain = 'localhost';
  52.  
  53. /**
  54.   * Nameservers to use when make DNS query for MX entries
  55.   * @var Array $nameservers
  56.   */
  57. var $nameservers = array(
  58. '192.168.0.1'
  59. );
  60.  
  61. var $debug = false;
  62.  
  63. /**
  64.   * Initializes the Class
  65.   * @return SMTP_validateEmail Instance
  66.   * @param $email Array[optional] List of Emails to Validate
  67.   * @param $sender String[optional] Email of validator
  68.   */
  69. function SMTP_validateEmail($emails = false, $sender = false) {
  70. if ($emails) {
  71. $this->setEmails($emails);
  72. }
  73. if ($sender) {
  74. $this->setSenderEmail($sender);
  75. }
  76. }
  77.  
  78. function _parseEmail($email) {
  79. $parts = explode('@', $email);
  80. $domain = array_pop($parts);
  81. $user= implode('@', $parts);
  82. return array($user, $domain);
  83. }
  84.  
  85. /**
  86.   * Set the Emails to validate
  87.   * @param $emails Array List of Emails
  88.   */
  89. function setEmails($emails) {
  90. foreach($emails as $email) {
  91. list($user, $domain) = $this->_parseEmail($email);
  92. if (!isset($this->domains[$domain])) {
  93. $this->domains[$domain] = array();
  94. }
  95. $this->domains[$domain][] = $user;
  96. }
  97. }
  98.  
  99. /**
  100.   * Set the Email of the sender/validator
  101.   * @param $email String
  102.   */
  103. function setSenderEmail($email) {
  104. $parts = $this->_parseEmail($email);
  105. $this->from_user = $parts[0];
  106. $this->from_domain = $parts[1];
  107. }
  108.  
  109. /**
  110.  * Validate Email Addresses
  111.  * @param String $emails Emails to validate (recipient emails)
  112.  * @param String $sender Sender's Email
  113.  * @return Array Associative List of Emails and their validation results
  114.  */
  115. function validate($emails = false, $sender = false) {
  116.  
  117. $results = array();
  118.  
  119. if ($emails) {
  120. $this->setEmails($emails);
  121. }
  122. if ($sender) {
  123. $this->setSenderEmail($sender);
  124. }
  125.  
  126. // query the MTAs on each Domain
  127. foreach($this->domains as $domain=>$users) {
  128.  
  129. $mxs = array();
  130.  
  131. // retrieve SMTP Server via MX query on domain
  132. list($hosts, $mxweights) = $this->queryMX($domain);
  133.  
  134. // retrieve MX priorities
  135. for($n=0; $n < count($hosts); $n++){
  136. $mxs[$hosts[$n]] = $mxweights[$n];
  137. }
  138. asort($mxs);
  139.  
  140. // last fallback is the original domain
  141. array_push($mxs, $this->domain);
  142.  
  143. $this->debug(print_r($mxs, 1));
  144.  
  145. $timeout = $this->max_conn_time/count($hosts);
  146.  
  147. // try each host
  148. while(list($host) = each($mxs)) {
  149. // connect to SMTP server
  150. $this->debug("try $host:$this->port\n");
  151. if ($this->sock = fsockopen($host, $this->port, $errno, $errstr, (float) $timeout)) {
  152. stream_set_timeout($this->sock, $this->max_read_time);
  153. break;
  154. }
  155. }
  156.  
  157. // did we get a TCP socket
  158. if ($this->sock) {
  159. $reply = fread($this->sock, 2082);
  160. $this->debug("<<<\n$reply");
  161.  
  162. preg_match('/^([0-9]{3}) /ims', $reply, $matches);
  163. $code = isset($matches[1]) ? $matches[1] : '';
  164.  
  165. if($code != '220') {
  166. // MTA gave an error...
  167. foreach($users as $user) {
  168. $results[$user.'@'.$domain] = false;
  169. }
  170. continue;
  171. }
  172.  
  173. // say helo
  174. $this->send("HELO ".$this->from_domain);
  175. // tell of sender
  176. $this->send("MAIL FROM: <".$this->from_user.'@'.$this->from_domain.">");
  177.  
  178. // ask for each recepient on this domain
  179. foreach($users as $user) {
  180.  
  181. // ask of recepient
  182. $reply = $this->send("RCPT TO: <".$user.'@'.$domain.">");
  183.  
  184. // get code and msg from response
  185. preg_match('/^([0-9]{3}) /ims', $reply, $matches);
  186. $code = isset($matches[1]) ? $matches[1] : '';
  187.  
  188. if ($code == '250') {
  189. // you received 250 so the email address was accepted
  190. $results[$user.'@'.$domain] = true;
  191. } elseif ($code == '451' || $code == '452') {
  192. // you received 451 so the email address was greylisted (or some temporary error occured on the MTA) - so assume is ok
  193. $results[$user.'@'.$domain] = true;
  194. } else {
  195. $results[$user.'@'.$domain] = false;
  196. }
  197.  
  198. }
  199.  
  200. // quit
  201. $this->send("quit");
  202. // close socket
  203. fclose($this->sock);
  204.  
  205. }
  206. }
  207. return $results;
  208. }
  209.  
  210.  
  211. function send($msg) {
  212. fwrite($this->sock, $msg."\r\n");
  213.  
  214. $reply = fread($this->sock, 2082);
  215.  
  216. $this->debug(">>>\n$msg\n");
  217. $this->debug("<<<\n$reply");
  218.  
  219. return $reply;
  220. }
  221.  
  222. /**
  223.   * Query DNS server for MX entries
  224.   * @return
  225.   */
  226. function queryMX($domain) {
  227. $hosts = array();
  228. $mxweights = array();
  229. if (function_exists('getmxrr')) {
  230. getmxrr($domain, $hosts, $mxweights);
  231. } else {
  232. // windows, we need Net_DNS
  233. require_once 'Net/DNS.php';
  234.  
  235. $resolver = new Net_DNS_Resolver();
  236. $resolver->debug = $this->debug;
  237. // nameservers to query
  238. $resolver->nameservers = $this->nameservers;
  239. $resp = $resolver->query($domain, 'MX');
  240. if ($resp) {
  241. foreach($resp->answer as $answer) {
  242. $hosts[] = $answer->exchange;
  243. $mxweights[] = $answer->preference;
  244. }
  245. }
  246.  
  247. }
  248. return array($hosts, $mxweights);
  249. }
  250.  
  251. /**
  252.   * Simple function to replicate PHP 5 behaviour. http://php.net/microtime
  253.   */
  254. function microtime_float() {
  255. list($usec, $sec) = explode(" ", microtime());
  256. return ((float)$usec + (float)$sec);
  257. }
  258.  
  259. function debug($str) {
  260. if ($this->debug) {
  261. echo htmlentities($str);
  262. }
  263. }
  264.  
  265. }
  266.  
  267.  
  268. ?>
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 8
Reputation: tgbyhn is an unknown quantity at this point 
Solved Threads: 1
tgbyhn tgbyhn is offline Offline
Newbie Poster

Re: check if email address is correct without sending email?

 
0
  #19
Sep 10th, 2008
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.
  1. $mxs[$this->domain] = '0';
Originally Posted by digital-ether View Post
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:
  1. <?php
  2.  
  3. /**
  4.  * Validate Email Addresses Via SMTP
  5.  * This queries the SMTP server to see if the email address is accepted.
  6.  * @copyright http://creativecommons.org/licenses/by/2.0/ - Please keep this comment intact
  7.  * @author gabe@fijiwebdesign.com
  8.  * @contributers adnan@barakatdesigns.net
  9.  * @version 0.1a
  10.  */
  11. class SMTP_validateEmail {
  12.  
  13. /**
  14.   * PHP Socket resource to remote MTA
  15.   * @var resource $sock
  16.   */
  17. var $sock;
  18.  
  19. /**
  20.   * Current User being validated
  21.   */
  22. var $user;
  23. /**
  24.   * Current domain where user is being validated
  25.   */
  26. var $domain;
  27. /**
  28.   * List of domains to validate users on
  29.   */
  30. var $domains;
  31. /**
  32.   * SMTP Port
  33.   */
  34. var $port = 25;
  35. /**
  36.   * Maximum Connection Time to an MTA
  37.   */
  38. var $max_conn_time = 30;
  39. /**
  40.   * Maximum time to read from socket
  41.   */
  42. var $max_read_time = 5;
  43.  
  44. /**
  45.   * username of sender
  46.   */
  47. var $from_user = 'user';
  48. /**
  49.   * Host Name of sender
  50.   */
  51. var $from_domain = 'localhost';
  52.  
  53. /**
  54.   * Nameservers to use when make DNS query for MX entries
  55.   * @var Array $nameservers
  56.   */
  57. var $nameservers = array(
  58. '192.168.0.1'
  59. );
  60.  
  61. var $debug = false;
  62.  
  63. /**
  64.   * Initializes the Class
  65.   * @return SMTP_validateEmail Instance
  66.   * @param $email Array[optional] List of Emails to Validate
  67.   * @param $sender String[optional] Email of validator
  68.   */
  69. function SMTP_validateEmail($emails = false, $sender = false) {
  70. if ($emails) {
  71. $this->setEmails($emails);
  72. }
  73. if ($sender) {
  74. $this->setSenderEmail($sender);
  75. }
  76. }
  77.  
  78. function _parseEmail($email) {
  79. $parts = explode('@', $email);
  80. $domain = array_pop($parts);
  81. $user= implode('@', $parts);
  82. return array($user, $domain);
  83. }
  84.  
  85. /**
  86.   * Set the Emails to validate
  87.   * @param $emails Array List of Emails
  88.   */
  89. function setEmails($emails) {
  90. foreach($emails as $email) {
  91. list($user, $domain) = $this->_parseEmail($email);
  92. if (!isset($this->domains[$domain])) {
  93. $this->domains[$domain] = array();
  94. }
  95. $this->domains[$domain][] = $user;
  96. }
  97. }
  98.  
  99. /**
  100.   * Set the Email of the sender/validator
  101.   * @param $email String
  102.   */
  103. function setSenderEmail($email) {
  104. $parts = $this->_parseEmail($email);
  105. $this->from_user = $parts[0];
  106. $this->from_domain = $parts[1];
  107. }
  108.  
  109. /**
  110.  * Validate Email Addresses
  111.  * @param String $emails Emails to validate (recipient emails)
  112.  * @param String $sender Sender's Email
  113.  * @return Array Associative List of Emails and their validation results
  114.  */
  115. function validate($emails = false, $sender = false) {
  116.  
  117. $results = array();
  118.  
  119. if ($emails) {
  120. $this->setEmails($emails);
  121. }
  122. if ($sender) {
  123. $this->setSenderEmail($sender);
  124. }
  125.  
  126. // query the MTAs on each Domain
  127. foreach($this->domains as $domain=>$users) {
  128.  
  129. $mxs = array();
  130.  
  131. // retrieve SMTP Server via MX query on domain
  132. list($hosts, $mxweights) = $this->queryMX($domain);
  133.  
  134. // retrieve MX priorities
  135. for($n=0; $n < count($hosts); $n++){
  136. $mxs[$hosts[$n]] = $mxweights[$n];
  137. }
  138. asort($mxs);
  139.  
  140. // last fallback is the original domain
  141. $mxs[$this->domain] = '0';
  142.  
  143. $this->debug(print_r($mxs, 1));
  144.  
  145. $timeout = $this->max_conn_time/count($hosts);
  146.  
  147. // try each host
  148. while(list($host) = each($mxs)) {
  149. // connect to SMTP server
  150. $this->debug("try $host:$this->port\n");
  151. if ($this->sock = fsockopen($host, $this->port, $errno, $errstr, (float) $timeout)) {
  152. stream_set_timeout($this->sock, $this->max_read_time);
  153. break;
  154. }
  155. }
  156.  
  157. // did we get a TCP socket
  158. if ($this->sock) {
  159.  
  160. $reply = fread($this->sock, 2082);
  161. $this->debug("<<<\n$reply");
  162.  
  163. preg_match('/^([0-9]{3}) /ims', $reply, $matches);
  164. $code = isset($matches[1]) ? $matches[1] : '';
  165.  
  166. if($code != '220') {
  167. // MTA gave an error...
  168. foreach($users as $user) {
  169. $results[$user.'@'.$domain] = false;
  170. }
  171. continue;
  172. }
  173.  
  174. // say helo
  175. $this->send("HELO ".$this->from_domain);
  176. // tell of sender
  177. $this->send("MAIL FROM: <".$this->from_user.'@'.$this->from_domain.">");
  178.  
  179. // ask for each recepient on this domain
  180. foreach($users as $user) {
  181.  
  182. // ask of recepient
  183. $reply = $this->send("RCPT TO: <".$user.'@'.$domain.">");
  184.  
  185. // get code and msg from response
  186. preg_match('/^([0-9]{3}) /ims', $reply, $matches);
  187. $code = isset($matches[1]) ? $matches[1] : '';
  188.  
  189. if ($code == '250') {
  190. // you received 250 so the email address was accepted
  191. $results[$user.'@'.$domain] = true;
  192. } elseif ($code == '451' || $code == '452') {
  193. // you received 451 so the email address was greylisted (or some temporary error occured on the MTA) - so assume is ok
  194. $results[$user.'@'.$domain] = true;
  195. } else {
  196. $results[$user.'@'.$domain] = false;
  197. }
  198.  
  199. }
  200.  
  201. // quit
  202. $this->send("quit");
  203. // close socket
  204. fclose($this->sock);
  205.  
  206. }
  207. }
  208. return $results;
  209. }
  210.  
  211.  
  212. function send($msg) {
  213. fwrite($this->sock, $msg."\r\n");
  214.  
  215. $reply = fread($this->sock, 2082);
  216.  
  217. $this->debug(">>>\n$msg\n");
  218. $this->debug("<<<\n$reply");
  219.  
  220. return $reply;
  221. }
  222.  
  223. /**
  224.   * Query DNS server for MX entries
  225.   * @return
  226.   */
  227. function queryMX($domain) {
  228. $hosts = array();
  229. $mxweights = array();
  230. if (function_exists('getmxrr')) {
  231. getmxrr($domain, $hosts, $mxweights);
  232. } else {
  233. // windows, we need Net_DNS
  234. require_once 'Net/DNS.php';
  235.  
  236. $resolver = new Net_DNS_Resolver();
  237. $resolver->debug = $this->debug;
  238. // nameservers to query
  239. $resolver->nameservers = $this->nameservers;
  240. $resp = $resolver->query($domain, 'MX');
  241. if ($resp) {
  242. foreach($resp->answer as $answer) {
  243. $hosts[] = $answer->exchange;
  244. $mxweights[] = $answer->preference;
  245. }
  246. }
  247.  
  248. }
  249. return array($hosts, $mxweights);
  250. }
  251.  
  252. /**
  253.   * Simple function to replicate PHP 5 behaviour. http://php.net/microtime
  254.   */
  255. function microtime_float() {
  256. list($usec, $sec) = explode(" ", microtime());
  257. return ((float)$usec + (float)$sec);
  258. }
  259.  
  260. function debug($str) {
  261. if ($this->debug) {
  262. echo htmlentities($str);
  263. }
  264. }
  265.  
  266. }
  267.  
  268.  
  269. ?>

Adnan
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 2
Reputation: whytek is an unknown quantity at this point 
Solved Threads: 0
whytek whytek is offline Offline
Newbie Poster

Re: check if email address is correct without sending email?

 
0
  #20
Sep 10th, 2008
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.
Last edited by peter_budo; Sep 10th, 2008 at 3:42 pm. Reason: Posted code moved to attachment at it was breaking Firefox view
Attached Files
File Type: txt code.txt (8.8 KB, 16 views)
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC