PHP - Check if email address is deliverable


SUBMITTED BY: efbee

DATE: Oct. 5, 2016, 6:59 p.m.

FORMAT: PHP

SIZE: 2.5 kB

HITS: 818

  1. Checking if Email address is deliverable
  2. <?php
  3. function checkemail($email){
  4. list($mailbox,$domain) = split('@',$email,2);
  5. $state = 'domain';
  6. // find preferred mailserver
  7. if(getmxrr($domain,$mailhosts,$pref)){
  8. asort($pref);
  9. foreach($pref as $preferred){
  10. $mailserver = $mailhosts[key($pref)];
  11. break;
  12. }
  13. $state = "trying mailserver $mailserver";
  14. $state = mailconnect($mailserver,$email);
  15. }else{
  16. // no mail exchange found try as host
  17. $state = "No MX, trying $domain";
  18. $state = mailconnect($domain,$email);
  19. }
  20. return $state;
  21. }
  22. function mailconnect($mailserver,$email){
  23. $myhostname = $SERVER_NAME;
  24. $connection = fsockopen($mailserver, 25);
  25. if($connection){
  26. $state = "connected to $mailserver";
  27. // Nothing to do with greeting
  28. //$smtpgreeting = fread($connection, 512);
  29. //if($smtpgreeting){
  30. fputs($connection, "HELO $myhostname\r\n");
  31. $hello = fgets($connection, 512);
  32. if($hello){
  33. $state = "chatting to $mailserver: $hello";
  34. fputs($connection, "MAIL FROM: <webserver@$myhostname>\r\n");
  35. $youok = fgets($connection, 512);
  36. if($youok){
  37. $state = "chatting to $mailserver: $youok";
  38. fputs($connection, "RCPT TO: <$newaddress>\r\n");
  39. $recepient = fgets($connection, 512);
  40. $state = "chatting to $mailserver: $recepient";
  41. if(ereg('250',$recepient)){
  42. fputs($connection, "QUIT\r\n");
  43. $deliverable = true;
  44. $state = false;
  45. }elseif(ereg('220',$recepient)){
  46. fputs($connection, "QUIT\r\n");
  47. $deliverable = true;
  48. $state = false;
  49. }else{
  50. $deliverable = false;
  51. $state = "RCPT? $recepient $newaddress";
  52. }
  53. }
  54. }else{
  55. $state = "$mailserver not accepting mail now, please try again.";
  56. }
  57. //}else{
  58. //$state = 'mailserver not greeting me';
  59. //break;
  60. //}
  61. }else{
  62. $state = "$mailserver not listening";
  63. }
  64. return $state;
  65. }
  66. ?>

comments powered by Disqus