Link to home
Start Free TrialLog in
Avatar of SpinningKASi
SpinningKASi

asked on

Check if E-Mail exists

Is there a chance to check an email-adress with php? Not only if the domain exists by checking the mailservers, also checking if the recipient is real by testing the reply code from the mail server for the 550 (no recipient)?
Avatar of lexxwern
lexxwern
Flag of Netherlands image

<?php

     function verifyemail_validateemail($email)
     {
          if (!preg_match("/^([\w|\.|\-|_]+)@([\w||\-|_]+)\.([\w|\.|\-|_]+)$/i", $email))
               return false;
          return true;
     }
     function verifyemail_validatehost($email, $return_mxhost=0)
     {
          if (!verifyemail_validateemail($email))
               return false;
               list($local,$domain) = explode("@",$email,2);
               $mxhosts = array();
     
          if(!checkdnsrr($domain, "MX") || !getmxrr($domain, $mxhosts))
               return false;
     
          if ($return_mxhost)
               return $mxhosts;
         
          return true;
     }


     function verifyemail_validateexists($email)
     {
          $mxhosts = verifyemail_validatehost($email, true);
         
          if (!is_array($mxhosts))
               return false;

          $found = false;
          $localhost = verifyemail_localhost();

          $mxsize = sizeof($mxhosts);
          for($i=0; $i<$mxsize; $i++)    
          {
               $socket = fsockopen($mxhosts[$i], 25);

               if(!$socket) continue;

               $foo = fgets($socket, 4096);

               # 220 <domain> Service ready
               if(!preg_match("/^220/i", $foo))
               {
                    verifyemail_closesocket($socket);
                    continue;
               }

               fputs($socket, "HELO ".$localhost."\r\n");
               $foo = fgets($socket);
               while (preg_match("/^220/i", $foo))
                    $foo = fgets($socket, 4096);

               fputs($socket, "VRFY ".$email."\r\n");
               $foo = fgets($socket, 4096);

               # 250 Requested mail action okay, completed
               if(preg_match("/^250/i", $foo))
               {
                    $found = true;
                    verifyemail_closesocket($socket);
                    break;
               }

               # 550 Requested action not taken: mailbox unavailable [E.g., mailbox not found, no access]
               if(preg_match("/^550/i", $foo))
               {
                    verifyemail_closesocket($socket);
                    continue;
               }

               fputs($socket, "MAIL FROM: <".$email.">\r\n");
               $foo = fgets($socket, 4096);

               fputs($socket, "RCPT TO: <".$email.">\r\n");
               $foo = fgets($socket, 4096);

               # 250 Requested mail action okay, completed
               # 251 User not local; will forward to <forward-path>
               if(preg_match("/^[250|251]/i", $foo))
               {
                    $found = true;
                    verifyemail_closesocket($socket);

                    break;
               }
         
               verifyemail_closesocket($socket);
          }

          return $found;
     }


     function verifyemail_closesocket($socket)
     {
          fputs($socket, "QUIT\r\n");
          fclose($socket);
         
          return true;
     }
?>
Avatar of SpinningKASi
SpinningKASi

ASKER

Looks good, but the function "verifyemail_localhost()" could not be found :(

Maybe I´m too blind to see it... Which funtion I have to use to check the mail?
ASKER CERTIFIED SOLUTION
Avatar of Zaytsev
Zaytsev

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
In addition to 127.0.0.1, check the email address itself to see if it is you or not.  Spammers love doing this too!
Did any of this help you?  If so, please award a grade to close this question.