0

Does anyone how can I check whether the mailserver I use is down or not with php?

2

3 Answers 3

2

Use Net_SMTP to make a connection to the server. It won't be perfect, but if you can't connect then it's probably down.

2
  • 1
    I'd send a HELO and then a QUIT as well :)
    – gnud
    Feb 24, 2010 at 2:34
  • @gnud: Hah. Yes, indeed, I should've mentioned that. Feb 24, 2010 at 2:37
0

most mail servers are on port 25. An example using sockets

$address = gethostbyname('www.somewhere.com');
$service_port="25";
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
    echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
} else {
    echo "OK.\n";
}
echo "Attempting to connect to '$address' on port '$service_port'...";
$result = socket_connect($socket, $address, $service_port);
if ($result === false) {
    echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
} else {
    echo "OK.\n";
}

See the PHP docs for more.

0
function checkSMTPService($hostname, $port)
{
     // Create a socket.  If we fail to create a socket return false
     // This is really more to check that we are able to create a socket
     // than if we are able to check the server
     $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
     if($socket === false) return false;

     // Now we will connect to the server.  If we fail we return false.
     $result = socket_connect($socket, $hostname, $port);
     if($result === false) return false;

     return true;

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.