verify existence of 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
Jim from Oakland
Forum Newbie
Posts: 3
Joined: Sun May 11, 2003 4:58 pm
Location: Oakland CA

verify existence of an email address?

Post by Jim from Oakland »

PHP folk

I would like to validate email addesses. I should note that I am barely an intermediate PHPer.

I have searched the web some and have found several ways to check the "syntax" of an email address but I would like to actually verify that the address actually exists/is functional.

Now, I have found code on the net that seems to do what I want but it is too complicated for me to understand/implement, and, I am not really that familiar with internet protocols.

So, can someone suggest code thast will test the existence of an email address?

Thanks a billion.

Jim From Oakland
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

what about sending an email with an confirmation code to proceed?
Jim from Oakland
Forum Newbie
Posts: 3
Joined: Sun May 11, 2003 4:58 pm
Location: Oakland CA

1. found email verification code 2. confirmation code

Post by Jim from Oakland »

Thanks volka

>what about sending an email with an confirmation code to proceed?
>You have to state a reason in your icq authorization requests or they will >be declined (all thanks to spam)

I do not know what a confirmation code is.

Any way, I found and cleaned up some great code (shown below) for a function which verifies an e-mail address. Unfortunately it only works on Unix systems. I'm stuck on MS XP machines.

Will the confirmation code approach work for them?

Any suggested readings?

re:
http://www.catb.org/~esr/faqs/smart-questions.html

did I misbehave?

Thanks

Note: I did not test the code below heavily.

/*

By: Jon S. Stevens jon@clearink.com
Copyright 1998 Jon S. Stevens, Clear Ink

This code has all the normal disclaimers.
It is free for any use, just keep the credits intact.

Enacements and modifications:
By: Shane Y. Gibson shane@tuna.org
Organization: The Unix Network Archives http://www.tuna.org./
Date: November 16th, 1998
Changes: Added **all** comments, as original code lacked them.
Added some return codes to include a bit more description
for useability.

I disclaim nothing...nor do I claim anything...but
it would be nice if you included this disclaimer...


* This function takes in an email address (say shane@tuna.org)
* and tests to see if it's a valid email address.
*
* An array with the results is passed back to the caller.
*
* Possible result codes for the array items are:
*
* Item 0: [true|false] true for valid email address
* false for NON-valid email address
*
* Item 1: [SMTP Code] if a valid MX mail server found, then
* fill this array in with failed SMTP
* reply codes
*
* Item 2: [true|false] true for valid mail server found for
* host/domain false if no valid mail server found
*
* Item 3: [MX server] if a valid MX host was found and
* connected to then fill in this
* with the MX server hostname
*
* EXAMPLE code for use is at the very end of this function.
*/

/*
**************************************************
Begin Function
**************************************************
*/

function validateEmail ( $email )
{
// used for SMTP HELO argument
global $SERVER_NAME;

// initialize our return array, populating with default values
$return = array ( false, "", "", "" );

/* assign our user part and domain parts respectively to seperate
variables
*/
list ( $user, $domain ) = split ( "@", $email, 2 );

// split up the domain name into sub-parts
$arr = explode ( ".", $domain );

/*
figure out how many parts to the host/domain name portion
there are
*/
$count = count ( $arr );

// get our Top-Level Domain portion (i.e. foobar.org)
$tld = $arr[$count - 2] . "." . $arr[$count - 1];

/* ------------------------------------------------
Check MX Record Exists
------------------------------------------------
*/

// check that an MX record exists for Top-Level Domain, and if so
// start our email address checking
if ( checkdnsrr ( $tld, "MX" ) )
{ // BEGIN NEST A

/* ------------------------------------------------
Test MX record for Host Exists
------------------------------------------------

Okay...valid dns reverse record; test that MX record for
host exists, and then fill the 'mxhosts' and 'weight'
arrays with the correct information
*/

if ( getmxrr ( $tld, $mxhosts, $weight ) )
{ // BEGIN NEST B

/* ------------------------------------------------
Loop mxhosts for Testing ------------------------------------------------

sift through the 'mxhosts' connecting to each host
*/

for ( $i = 0; $i < count ( $mxhosts ); $i++ )
{ // BEGIN NEST C

/*
open socket on port 25 to mxhosts, setting
returned file pointer to the variable 'fp'
*/
$fp = fsockopen ( $mxhosts[$i], 25 );

// if the 'fp' was set, then goto work
if ( $fp )
{ // BEGIN NEST D

// work variables
$s = 0;
$c = 0;
$out = "";

/*
set our created socket for 'fp' to
non-blocking mode so our fgets() calls
will return right away
*/
set_socket_blocking ( $fp, false );
/*
------------------------------------------------
Do-While Loop look for valid response
------------------------------------------------

as long as our 'out' variable has a null value ("")
keep looping (DO) until we get something
*/
do
{ // BEGIN NEST E

// output of the stream assigned to 'out' variable
$out = fgets ( $fp, 2500 );

/* IF we get an "220" code (service ready code
= (i.e greeting)) then increment our work
(code (c)) variable, and = null
out our output variable for a later loop test
*/
if ( ereg ( "^220", $out ) )
{ // BEGIN NEST F
$s = 0;
$out = "";
$c++;
$return[2] = true;
$return[3] = $mxhosts[$i];
}
/*
ELSE IF c is greater than 0 and 'out' is null (""),
we got a code back from some server, and we've passed
through this loop at least once
*/
else if ( ( $c > 0 ) && ($out == "") )
{
$return[2] = true;
break;
}

// ELSE increment 's' counter
else
{
$s++;
}//END NEST F -- END IF ( ereg ( "^220", $out ) )

// and if 's' is 9999, break, to
// keep from looping infinetly
if ( $s == 9999 ) { break; }

} //END NEST E -- END WHILE LOOP
while ( $out = "" );


/* reset our file pointer to blocking mode,
so we wait for communication to finish before
moving on...
*/
set_socket_blocking ( $fp, true );

// talk to the MX mail server,
// validating ourself (HELO)
fputs ( $fp, "HELO $SERVER_NAME\n" );

// get the mail servers reply, assign to
// 'output' (ignored)
$output = fgets ( $fp, 2000 );

// give a bogus "MAIL FROM:" header to the server

fputs ( $fp, "MAIL FROM: <info@" . $tld . ">\n" );

// get output again (ignored)
$output = fgets ( $fp, 2000 );

// give RCPT TO: header for the email address we are testing
fputs ( $fp, "RCPT TO: <$email>\n" );

// get final output for validity testing (used)
$output = fgets ( $fp, 2000 );
/*
test the reply code from the mail server for the 250
(okay) code
*/
if ( ereg ( "^250", $output ) )
{ // BEGIN NEST H

/* set our true/false(ness)
array item for testing
*/
$return[0] = true;
}
else
{
/* otherwise, bogus address, fillin the 2nd array
item with the mail servers reply code for user
to test if they want
*/
$return[0] = false;
$return[1] = $output;
} // END NEST H -- END IF ( ereg ( "^250", $output ) )

// tell the mail server we are done talking to it
fputs ( $fp, "QUIT\n" );

// close the file pointer
fclose( $fp );
/*
if we got a good value break, otherwise, we'll keep
trying MX records until we get a good value, or we
exhaust our possible MX servers
*/
if ( $return[0] == true )
{ break; }

} // END NEST D: END IF ( $fp )

} // END NEST C: -- END FOR LOOP getting mx hosts

} // END NEST B: END IF ( getmxrr ( $tld, $mxhosts, $weight ) )

} else {

/*
-----------------------------------------------------
No MX Record Found
-----------------------------------------------------

No MX record appears for the specified Top-Level Domain;
possibly an invalid host/domain name was specified.
*/
$return[0] = false;
$return[1] = "Invalid email address (bad domain name)";
$return[2] = false;

} // NEST A: END IF ( checkdnsrr ( $tld, "MX" ) )

// return the array for the user to test against
return $return;
}
Linkjames
Forum Commoner
Posts: 90
Joined: Tue Sep 16, 2003 8:39 am

Post by Linkjames »

The conformation code system simply means that when someone signs up to your site, they recive an email with a randomly produced code in it. They then enter this code into your website, which checks it is correct. this proves that the address is valid and active, as they have to have recived and read the mail to get the code.

I'm afraid I don't know the code behind it, but I'm sure some kind person will point you in the right direction.
Post Reply