Page 1 of 1

capture fsockopen refusal

Posted: Sat Nov 05, 2005 10:20 pm
by bladecatcher
G'day All,
I'm using the following code to check email addresses.

Code: Select all

//---Connect to mail server and check e-mail address------------------
	$Connect = fsockopen ( $ConnectAddress, 25 );
	//Gives this error when domain exists but no mx:
	//Warning: fsockopen() [function.fsockopen]: unable to connect to mx.somewhere.com:25 
	//(Connection refused) in /xxx/requestcontact.php on line 26
	if ($Connect) {
		if (ereg("^220", $Out = fgets($Connect, 1024))) {
			fputs ($Connect, "HELO $HTTP_HOST\r\n");
			$Out = fgets ( $Connect, 1024 );
			fputs ($Connect, "MAIL FROM: <{$Email}>\r\n");
			$From = fgets ( $Connect, 1024 );
			fputs ($Connect, "RCPT TO: <{$Email}>\r\n");
			$To = fgets ($Connect, 1024);
			fputs ($Connect, "QUIT\r\n");
			fclose($Connect);
			if (!ereg ("^250", $From) || !ereg ( "^250", $To )) {
				$result[0]=false;
				$result[1]="Address not valid; Server rejected address";
				return $result;
			}
		} else {
			$result[0] = false;
			$result[1] = "No response from server";
			return $result;
		}
	}  else {
		$result[0]=false;
		$result[1]="Can not connect E-Mail server.";
		return $result;
	}
However I get an error as noted in the code.

If I encapsulate that code in an if statement like this

Code: Select all

If (fsockopen ( $ConnectAddress, 25 )) {
		$Connect = fsockopen ( $ConnectAddress, 25 );

                //rest of code here

	} else {
		$result[0]=false;
		$result[1]="Can not connect E-Mail server.";
		return $result;
	}
it catches refusal but is doubling up and clumsy.

Edit: Correction that does not work, Why?????

How can I get around the connection refusal? so the code continues to the next if statement?
All explainations gratefully received as I don't really understand what's happening.

Thanking you in anticipation,
blade

Posted: Sat Nov 05, 2005 10:30 pm
by feyd
look up the error control operator: http://php.net/language.operators.errorcontrol

you may want to use getmxrr() first to get the mail server addresses ;)

Posted: Sat Nov 05, 2005 11:25 pm
by bladecatcher
feyd wrote:look up the error control operator: http://php.net/language.operators.errorcontrol
(@) just exactly what was needed. Some very useful links to general error handling off that page too, thanks.
feyd wrote:you may want to use getmxrr() first to get the mail server addresses ;)

Code: Select all

//---Find the address of the mail server----------------
	list ( $Username, $Domain ) = split ("@",$Email);
	if (getmxrr($Domain, $MXHost))  {
		$ConnectAddress = $MXHost[0];
	} else {
		$ConnectAddress = $Domain;
	}
Thank you.

Excellent reply (as usual).
Thanks very much again,
blade