How to verify an email address?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
seontay
Forum Newbie
Posts: 6
Joined: Thu Aug 01, 2002 1:18 am

How to verify an email address?

Post by seontay »

how do I verify an valid email address without send a message to it? and also url address.

Thank in advance if you can help me.

Best Regards,
Freddie
User avatar
martin
Forum Commoner
Posts: 33
Joined: Fri Jun 28, 2002 12:59 pm
Location: Cambridgeshire

Post by martin »

Here's a function I use to open a socket to the domain ( I took this from someone's post a while back but didn't take the name, so respect to them).

Code: Select all

function validateEmail ( $email ) 
{ 
global $SERVER_NAME; 
$return = array ( false, "" ); 
list ( $user, $domain ) = split ( "@", $email, 2 ); 
$tld = $domain; 
if ( checkdnsrr ( $tld, "MX" ) ) 
{ 
if ( getmxrr ( $tld, $mxhosts, $weight ) ) 
{ 
for ( $i = 0; $i < count ( $mxhosts ); $i++ ) 
&#123; 
$fp = fsockopen ( $mxhosts&#1111;$i], 25 ); 
if ( $fp ) 
&#123; 
$s = 0; 
$c = 0; 
$out = ""; 
set_socket_blocking ( $fp, false ); 
do 
&#123; 
$out = fgets ( $fp, 2500 ); 
if ( ereg ( "^220", $out ) ) 
&#123; 
$s = 0; 
$out = ""; 
$c++; 
&#125; 
else if ( ( $c > 0 ) && ( $out == "" ) ) 
&#123; break; &#125; 
else 
&#123; $s++; &#125; 
if ( $s == 9999 ) &#123; break; &#125; 

&#125; while ( $out == "" ); 
set_socket_blocking ( $fp, true ); 

fputs ( $fp, "HELO $SERVER_NAME\n" ); 
$output = fgets ( $fp, 2000 ); 
fputs ( $fp, "MAIL FROM: $output = fgets ( $fp, 2000 ); 
fputs ( $fp, "RCPT TO: <$email>\n" ); 
$output = fgets ( $fp, 2000 ); 
if ( ereg ( "^250", $output ) ) 
&#123; 
$return&#1111;0] = true; 
&#125; 
else 
&#123; 
$return&#1111;0] = false; 
$return&#1111;1] = $output; 
&#125; 
fputs ( $fp, "QUIT\n" ); 
fclose( $fp ); 

if ( $return&#1111;0] == true ) 
&#123; break; &#125; 
&#125; 
&#125; 
&#125; 
&#125; 
return $return; 
&#125;
Hope it helps
seontay
Forum Newbie
Posts: 6
Joined: Thu Aug 01, 2002 1:18 am

Post by seontay »

this line i think incomplete
fputs ( $fp, "MAIL FROM: $output = fgets ( $fp, 2000 );

can you show me the complete statement

Thank you very much
mester
Forum Newbie
Posts: 5
Joined: Fri Aug 02, 2002 3:40 am

email-validate

Post by mester »

for the whole thing: http://www.ietf.org/rfc/rfc0821.txt


and then the code:

Code: Select all

<?

function validateEmail ( $email )
&#123;
 global $SERVER_NAME;
 $return = array ( false, "" );
 list ( $user, $domain ) = split ( "@", $email, 2 );
 $tld = $domain;
 if ( checkdnsrr ( $tld, "MX" ) )
 &#123;
    if ( getmxrr ( $tld, $mxhosts, $weight ) )
    &#123;
       for ( $i = 0; $i < count ( $mxhosts ); $i++ )
       &#123;
           $fp = fsockopen ( $mxhosts&#1111;$i], 25 );
           if ( $fp )
           &#123;
              $s = 0;$c = 0;$out = "";
              set_socket_blocking ( $fp, false );
              do
              &#123;
                $out = fgets ( $fp, 2500 );
                if ( ereg ( "^220", $out ) )
                   &#123; $s = 0;$out = "";$c++; &#125;
                else if ( ( $c > 0 ) && ( $out == "" ) )
                   &#123; break; &#125;
                else
                   &#123; $s++; &#125;
                if ( $s == 9999 )
                   &#123; break; &#125;
              &#125;while ( $out == "" );
              set_socket_blocking ( $fp, true );
              fputs ( $fp, "HELO $SERVER_NAME\n" );
              $output = fgets ( $fp, 2000 );
              fputs ( $fp, "MAIL FROM:\n" );
              $output = fgets ( $fp, 2000 );
              fputs ( $fp, "RCPT TO: <$email>\n" );
              $output = fgets ( $fp, 2000 );
              if (( ereg ( "^250", $output ) ) || //ok
                  ( ereg ( "^552", $output ) ) || //quota full
                  ( ereg ( "^551", $output ) ))   //ok, but forwarding
                 &#123; $return&#1111;0] = true; &#125;
              else
                 &#123; $return&#1111;0] = false;&#125;
              $return&#1111;1] = $output;
              fputs ( $fp, "QUIT\n" );
              fclose( $fp );
              if ( $return&#1111;0] == true )
                      &#123; break; &#125;
           &#125;
       &#125;
    &#125;
  &#125;
return $return;
&#125;

if (isset ($email))
&#123;
 $ret= validateEmail($email);
 echo "<br>Validated: ";
 if ($ret&#1111;0]) echo "YES. ";
 else echo "NO. ";
 echo "( $ret&#1111;1].)";
&#125;
else
&#123;
    echo "Please give an email addres (email_validate.php?email=xy@somewhere.com)";
&#125;
?>
mester
Forum Newbie
Posts: 5
Joined: Fri Aug 02, 2002 3:40 am

this works with more SMTP servers

Post by mester »

Code: Select all

<?

function validateEmail ( $email )
&#123;
 global $SERVER_NAME;
 $return = array ( false, "" );
 list ( $user, $domain ) = split ( "@", $email, 2 );
 $tld = $domain;

 if ( $return&#1111;0] = checkdnsrr ( $tld, "MX" ) )
 &#123;
    if ( getmxrr ( $tld, $mxhosts, $weight ) )
    &#123;
       for ( $i = 0; $i < count ( $mxhosts ); $i++ )
       &#123;
           $fp = fsockopen ( $mxhosts&#1111;$i], 25 );
           if ( $fp )
           &#123;
              $s = 0;$c = 0;$out = "";
              socket_set_blocking ( $fp, false );
              do
              &#123;
                $out = fgets ( $fp, 2500 );
                if ( ereg ( "^220", $out ) )
                   &#123; $s = 0;$out = "";$c++; &#125;
                else if ( ( $c > 0 ) && ( $out == "" ) )
                   &#123; break; &#125;
                else
                   &#123; $s++; &#125;
                if ( $s == 9999 )
                   &#123; break; &#125;
              &#125;while ( $out == "" );
              socket_set_blocking ( $fp, true );
              fputs ( $fp, "HELO $SERVER_NAME\n" );
              $output = fgets ( $fp, 2000 );

              //method -one-
              fputs ( $fp, "MAIL FROM:<>\n" );
              $output = fgets ( $fp, 2000 );
              fputs ( $fp, "RCPT TO: <$email>\n" );
              $output = fgets ( $fp, 2000 );
              $return&#1111;1] = "RCPT TO: <$email>".$output;
              
              if (( !ereg ( "^250", $output )) &&
                 ( !ereg ( "^550", $output ))  &&
                 ( !ereg ( "^552", $output ))  && //quota full
                 ( !ereg ( "^251", $output ))) //user not known
              &#123;
                 //try this
                 fputs ( $fp, "RSET\n" );
                 $output = fgets ( $fp, 2000 );
                 fputs ( $fp, "VRFY: $user\n" );
                 $output = fgets ( $fp, 2000 );
                 $return&#1111;1] = $return&#1111;1]." VRFY: $user ".$output;
              &#125;
              
              if (( ereg ( "^250", $output ) ) || //ok
                  ( ereg ( "^251", $output ) ) || //not a local user, forwarded
                  ( ereg ( "^552", $output ) )) //quota full
                 &#123; $return&#1111;0] = true; &#125;
              else
                 &#123; $return&#1111;0] = false;&#125;

              fputs ( $fp, "QUIT\n" );
              fclose( $fp );
              if ( $return&#1111;0] == true )
                      &#123; break; &#125;
           &#125;
       &#125;
    &#125;
  &#125;
  else
  &#123;
      $return&#1111;1] = "no such domain!!";
  &#125;
return $return;
&#125;

if (isset ($email))
&#123;
 $ret= validateEmail($email);
 echo "<br>Validated: ";
 if ($ret&#1111;0]) echo "YES. ";
 else echo "NO. ";
 echo "( $ret&#1111;1].)";
&#125;
else
&#123;
    echo "Please give an email addres (email_validate.php?emial=xy@somewhere.com)";
&#125;
?>
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post by JPlush76 »

that rocks, but what should go in the "$SERVER_NAME" variable? 8O
seontay
Forum Newbie
Posts: 6
Joined: Thu Aug 01, 2002 1:18 am

I try it

Post by seontay »

I run the function on my site, but only the email address belong to my site can validate, others site like @hotmail.com @yahoo.com @any.com ... etc. cannot verify. how is it
Post Reply