Jin Yong
Jin Yong

Reputation: 43768

how to check mail server is down in php

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

Upvotes: 0

Views: 2089

Answers (3)

thenetimp
thenetimp

Reputation: 1

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;

}

Upvotes: 0

ghostdog74
ghostdog74

Reputation: 342333

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.

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798556

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.

Upvotes: 2

Related Questions