Page 2 of 3

Posted: Tue Jul 12, 2005 11:26 pm
by neophyte
Amazing Roja! Nice work. Thanks!

Posted: Wed Jul 13, 2005 3:18 am
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.

Posted: Wed Jul 13, 2005 3:29 am
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?

Posted: Wed Jul 13, 2005 3:59 am
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.

Posted: Wed Jul 13, 2005 4:16 am
by adixtopix
thanck you, Roja

great
works like a charm

Posted: Wed Jul 13, 2005 4:16 am
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.

Posted: Wed Jul 13, 2005 4:28 am
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?

Posted: Wed Jul 13, 2005 4:45 am
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');".

Posted: Wed Jul 13, 2005 4:55 am
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.

Posted: Wed Jul 13, 2005 5:35 am
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.

Posted: Wed Jul 13, 2005 5:53 am
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.

Posted: Wed Jul 13, 2005 6:41 am
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?

Posted: Wed Jul 13, 2005 7:51 am
by patrikG
reg. Ajax: there's a nice background article over at Sitepoint: http://www.sitepoint.com/article/remote-scripting-ajax

Posted: Wed Jul 13, 2005 7:56 am
by neophyte
Just tried you're script bokehman -- nice work!

Posted: Wed Jul 13, 2005 8:00 am
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.