validate an email adress

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

User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

Amazing Roja! Nice work. Thanks!
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

Roja wrote:So which method are you using to ask it that? VRFY?
Well here is what the backend does. Try it! It uses fsockopen to contact the remote mail server.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

bokehman wrote:
Roja wrote:So which method are you using to ask it that? VRFY?
Well here is what the backend does. Try it! It uses fsockopen to contact the remote mail server.
Nice script. Haven't had a proper look at the code, but do you use Ajax?
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

XMLHttpRequest object == Microsoft.XMLHTTP == AJAX
All pretty much different names for the same function.

Yes that is what the front end is using. The back end is PHP.
adixtopix
Forum Newbie
Posts: 17
Joined: Thu Jun 23, 2005 7:02 am

Post by adixtopix »

thanck you, Roja

great
works like a charm
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

bokehman wrote:
Roja wrote:So which method are you using to ask it that? VRFY?
Well here is what the backend does. Try it! It uses fsockopen to contact the remote mail server.
You didn't answer the question at all. You just posted another link to another script that also doesn't show what method it uses.

What request is it making via fsock to check if an address is valid? Is it simply checking if the mailserver of record will respond on a given port? Is it sending a VRFY request?

Please, be more specific. Saying "It uses fsockopen" leaves thousands of ports and thousands of commands it could be using.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

bokehman wrote:XMLHttpRequest object == Microsoft.XMLHTTP == AJAX
All pretty much different names for the same function.
Let me specify: did you use any particular library?
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

I'm not sure what you mean by library. Isn't that a place from which you borrow books?

I just put a little bit of javascript in the webpage. Look at the page source and you will see. The code is forked so it works in mozilla and IE. Very simple. And the back end is just a normal php script but with the header "header('Content-Type: text/xml');".
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

bokehman wrote:I'm not sure what you mean by library. Isn't that a place from which you borrow books?
How right you are. It means something slightly different in the context of programming.
What I was referring to was something such as JSpan. If you don't know it yet, have a look.
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

Well thats new to me. I had a look at that site. Lots of interesting stuff.

I only recently heard of the XmlHttpRequest object and decided to try it out just for fun so I wrote that script.
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

Roja wrote:
bokehman wrote:This is nothing to do with REGEX. It is a real time connection with the remote mail server to see if it will accept mail for an address it has authority for.
So which method are you using to ask it that? VRFY?
No! I just connect to the remote mail server and send (amongst other things):

RCPT TO:<your@email.com>

if the remote mailserver replies with:

'250 something...'

it means it is willing to accept mail for that address.
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

bokehman wrote:I just connect to the remote mail server and send (amongst other things)
Can you share the code, or list all the commands you use?
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

reg. Ajax: there's a nice background article over at Sitepoint: http://www.sitepoint.com/article/remote-scripting-ajax
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

Just tried you're script bokehman -- nice work!
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

Roja wrote:Can you share the code, or list all the commands you use?
Of course. I'm assuming you mean my PHP code!

Here it is in a function. In places it may seem like there is unnecessary code but this is because different mailservers give diffent response and this needs to be taken into consideration. Anyway, enjoy!

Code: Select all

function check_email_address_exists($email)
{
   
	$result = true;

    // Extract the mail server name from the email address.
    list ($user, $domain) = split ("@", $email);
    if (getmxrr($domain, $mxhost)){  
        $ConnectAddress = $mxhost[0];
     }else{
        $ConnectAddress = $domain;
    }
    if(gethostbynamel($ConnectAddress) === FALSE){
		return FALSE;
    }
    
    // Try to open a socket at the mail server address.
    if($ConnectAddress == $domain){
    	$connect = @fsockopen($ConnectAddress, 25, $errno, $errstr, 5);
	}else{
		$tries = count($mxhost);
		$i = 0;
		$connect = @fsockopen($mxhost[$i], 25, $errno, $errstr, 5);
		while(!$connect && !$tries == 0){
			$i++;
			$connect = @fsockopen($mxhost[$i], 25, $errno, $errstr, 5);
			$tries--;
			
		}
	}
	
	if ($connect)
    {
        
	    if (ereg("^220", $out = fgets($connect, 1024)))
        {
            while(ereg('220', $out)){
	            $debug[] = 'Remote: '.$out;
		        stream_set_timeout($connect, 0, 5000);	
		        $out = fgets ( $connect, 1024 );
		        stream_set_timeout($connect, 10);
	        }
            $debug[] = "Local: EHLO {$_SERVER['HTTP_HOST']}";
	        fputs ($connect, "EHLO {$_SERVER['HTTP_HOST']}\r\n");
            $out = fgets ( $connect, 1024 );
	        while(!ereg('^250', $out)&&(!empty($out))){
		        $debug[] = 'Remote: '.$out;
		        stream_set_timeout($connect, 0, 5000);	
		        $out = fgets ( $connect, 1024 );
		        stream_set_timeout($connect, 10);
	        }
            for($tries = 15; $tries > 0; $tries--){
		    	if(ereg('^250', $out)){
					$debug[] = 'Remote: '.$out;
					$for_loop_hop = $out;
				}elseif(empty($out)){
					    usleep(100000); 
				}else{
				    $debug[] = 'Remote: '.$out;
			    }
			    stream_set_timeout($connect, 0, 5000);
			    $out = fgets($connect, 1024);
			    stream_set_timeout($connect, 10);
	 	   }
			if(!ereg('^250', $for_loop_hop)){
				$debug[] = 'No 250';
		            if(isset($_GET['debug'])){
                		foreach($debug as $line){
							echo $line.'<br>';
						}
					}
	            	return FALSE;
            }
			$debug[] = htmlspecialchars("Local: MAIL FROM: <{$email}>\r\n");
            fputs ($connect, "MAIL FROM: <{$email}>\r\n");
            $from = fgets ( $connect, 1024 );
            $debug[] = 'Remote: '.$from;
            $debug[] = htmlspecialchars("Local: RCPT TO: <{$email}>\r\n");
            fputs ($connect, "RCPT TO: <{$email}>\r\n");
            $to = fgets ($connect, 1024);
            $debug[] = 'Remote: '.$to;
            $debug[] = "Local: QUIT\r\n";
            fputs ($connect, "QUIT\r\n");
            $quit = fgets ($connect, 1024);
            $debug[] = 'Remote: '.$quit;
            fclose($connect);
            
            // Validate our exchange with the mail server.
            
            // Server rejected address.
            if (!ereg ("^250", $from) || !ereg ("^250", $to))
            {
	   		 	$result = false;
  	     	 }
 	   }
        // Strange or no response from remote mail server.
        else
        {
            $result = false;
        }
    }
    // Can't connect to server.
    else
    {
        $result = 'Failed to connect to remote mail server';
    }
    if(isset($_GET['debug'])){
     	if($debug > 0){
    		foreach($debug as $line){
			    print "$line <br />\r\n";
    		}
		}
	}
	
    return $result;
}
Also if you are using windows I have written a getmxrr() function which would be needed on windows but not on linux. If you need it just ask.
Post Reply