943,193 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 27303
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 23rd, 2008
1

check if email address is correct without sending email?

Expand Post »
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
Similar Threads
Reputation Points: 15
Solved Threads: 21
Posting Whiz in Training
nikesh.yadav is offline Offline
219 posts
since Feb 2008
Aug 23rd, 2008
0

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

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.
php Syntax (Toggle Plain Text)
  1. function EmailValidation($email)
  2. {
  3. $email = htmlspecialchars(stripslashes(strip_tags($email))); //parse unnecessary characters to prevent exploits
  4.  
  5. if ( eregi ( '[a-z||0-9]@[a-z||0-9].[a-z]', $email ) )
  6. { //checks to make sure the email address is in a valid format
  7. $domain = explode( "@", $email ); //get the domain name
  8. if ( @fsockopen ($domain[1],80,$errno,$errstr,3))
  9. {
  10. //if the connection can be established, the email address is probably valid
  11. return true;
  12. } else
  13. {
  14. return false; //if a connection cannot be established return false
  15. }
  16. return false; //if email address is an invalid format return false
  17. }
  18. }
Reputation Points: 232
Solved Threads: 137
Practically a Master Poster
buddylee17 is offline Offline
665 posts
since Nov 2007
Aug 24th, 2008
1

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

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:
PHP Syntax (Toggle Plain Text)
  1. 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

php Syntax (Toggle Plain Text)
  1.  
  2. $email = 'user@example.com';
  3. list($user, $domain) = explode('@', $email);
  4.  
  5. $port = 25; // default smtp port
  6.  
  7. $sock = fsockopen($domain, $port);
  8. if ($sock) {
  9.  
  10. fputs($sock, 'HELO mydomain.com');
  11. $reply = fgets($sock); // not interesting
  12.  
  13. fputs($sock, 'MAIL FROM: user@mydomain.com');
  14. $reply = fgets($sock); // not interesting
  15.  
  16. fputs($sock, 'RCPT TO: '.$email);
  17. $reply = fgets($sock); // interesting
  18.  
  19. list($code, $msg) = explode(' ', $reply);
  20.  
  21. if ($code == '250') {
  22. // you received 250 so the email address is accepted
  23. } else {
  24. // something went wrong, the email will most likely bounce
  25. }
  26.  
  27. fclose($sock);
  28.  
  29. }
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005
Aug 24th, 2008
1

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

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
Last edited by digital-ether; Aug 24th, 2008 at 1:31 pm.
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005
Aug 24th, 2008
0

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

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.
Reputation Points: 15
Solved Threads: 21
Posting Whiz in Training
nikesh.yadav is offline Offline
219 posts
since Feb 2008
Aug 24th, 2008
1

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

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

php Syntax (Toggle Plain Text)
  1. /**
  2.  * Validate an Email Address Via SMTP
  3.  * This queries the SMTP server to see if the email address is accepted.
  4.  * @copyright http://creativecommons.org/licenses/by/2.0/ - Please keep this comment intact
  5.  * @author gabe@fijiwebdesign.com
  6.  * @version 0.1a
  7.  */
  8. class SMTP_validateEmail {
  9.  
  10. var $sock;
  11.  
  12. var $user;
  13. var $domain;
  14. var $port = 25;
  15. var $max_conn_time = 30;
  16. var $max_read_time = 5;
  17.  
  18. var $from_user = 'user';
  19. var $from_domain = 'localhost';
  20.  
  21. var $debug = false;
  22.  
  23. function SMTP_validateEmail($email = false, $sender = false) {
  24. if ($email) {
  25. $this->setEmail($email);
  26. }
  27. if ($sender) {
  28. $this->setSenderEmail($sender);
  29. }
  30. }
  31.  
  32. function setEmail($email) {
  33. list($user, $domain) = explode('@', $email);
  34. $this->user = $user;
  35. $this->domain = $domain;
  36. }
  37.  
  38. function setSenderEmail($email) {
  39. list($user, $domain) = explode('@', $email);
  40. $this->from_user = $user;
  41. $this->from_domain = $domain;
  42. }
  43.  
  44. /**
  45. * Validate an Email Address
  46. * @param String $email Email to validate (recipient email)
  47. * @param String $sender Senders Email
  48. */
  49. function validate($email = false, $sender = false) {
  50. if ($email) {
  51. $this->setEmail($email);
  52. }
  53. if ($sender) {
  54. $this->setSenderEmail($sender);
  55. }
  56. // retrieve SMTP Server via MX query on domain
  57. $result = getmxrr($this->domain, $hosts);
  58. // last fallback is the original domain
  59. array_push($hosts, $this->domain);
  60.  
  61. $this->debug(print_r($hosts,1 ));
  62.  
  63. $timeout = $this->max_conn_time/count($hosts);
  64.  
  65. // try each host
  66. foreach($hosts as $host) {
  67. // connect to SMTP server
  68. $this->debug("try $host:$this->port\n");
  69. if ($this->sock = fsockopen($host, $this->port, $errno, $errstr, (float) $timeout)) {
  70. socket_set_blocking($this->sock, false);
  71. break;
  72. }
  73. }
  74.  
  75. // did we get a TCP socket
  76. if ($this->sock) {
  77. // say helo
  78. $this->send("HELO ".$this->from_domain);
  79. // tell of sender
  80. $this->send("MAIL FROM: <".$this->from_user.'@'.$this->from_domain.">");
  81. // ask of recepient
  82. $reply = $this->send("RCPT TO: <".$this->user.'@'.$this->domain.">");
  83. // quit
  84. $this->send("quit");
  85. // close socket
  86. fclose($this->sock);
  87. // get code and msg from response
  88. list($code, $msg) = explode(' ', $reply);
  89.  
  90. if ($code == '250') {
  91. // you received 250 so the email address was accepted
  92. return true;
  93. }
  94.  
  95. }
  96.  
  97. return false;
  98. }
  99.  
  100.  
  101. function send($msg) {
  102. fwrite($this->sock, $msg."\n");
  103. $resp = '';
  104. $start = $this->microtime_float();
  105. while(1) {
  106. $reply = fread($this->sock, 2082);
  107. $resp .= $reply;
  108. if (($resp != '' && trim($reply) == '')
  109. || ($this->microtime_float() - $start > $this->max_read_time )) {
  110. break;
  111. }
  112. }
  113.  
  114. $this->debug(">>>\n$msg\n");
  115. $this->debug("<<<\n$resp");
  116.  
  117. return $resp;
  118. }
  119.  
  120. /**
  121. * Simple function to replicate PHP 5 behaviour. http://php.net/microtime
  122. */
  123. function microtime_float() {
  124. list($usec, $sec) = explode(" ", microtime());
  125. return ((float)$usec + (float)$sec);
  126. }
  127.  
  128. function debug($str) {
  129. if ($this->debug) {
  130. echo $str;
  131. }
  132. }
  133.  
  134.  
  135. }

Example Usage:

php Syntax (Toggle Plain Text)
  1. // the email to validate
  2. $email = 'joe@gmail.com';
  3. // an optional sender
  4. $sender = 'user@example.com';
  5. // instantiate the class
  6. $SMTP_Valid = new SMTP_validateEmail();
  7. // turn debugging on to view the SMTP session
  8. $SMTP_Valid->debug = true;
  9. // do the validation
  10. $result = $SMTP_Valid->validate($email, $sender);
  11. // view results
  12. var_dump($result);
  13. echo $email.' is '.($result ? 'valid' : 'invalid')."\n";
  14.  
  15. // send email?
  16. if ($result) {
  17. //mail(...);
  18. }


I've added it to the code snippets:
http://www.daniweb.com/code/snippet940.html
Last edited by digital-ether; Aug 24th, 2008 at 3:29 pm.
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005
Aug 25th, 2008
0

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

k thnaks i will try
Reputation Points: 15
Solved Threads: 21
Posting Whiz in Training
nikesh.yadav is offline Offline
219 posts
since Feb 2008
Aug 26th, 2008
0

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

Be careful with connecting to random/unknown hosts. It can get you into legal trouble. Just a warning, since it happened to me...
Sponsor
Featured Poster
Reputation Points: 546
Solved Threads: 717
Bite my shiny metal ass!
pritaeas is offline Offline
4,143 posts
since Jul 2006
Aug 26th, 2008
0

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

Click to Expand / Collapse  Quote originally posted by pritaeas ...
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?
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005
Aug 27th, 2008
0

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

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.
Sponsor
Featured Poster
Reputation Points: 546
Solved Threads: 717
Bite my shiny metal ass!
pritaeas is offline Offline
4,143 posts
since Jul 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: convert PHP to javascript?
Next Thread in PHP Forum Timeline: combo box problem...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC