i want to check whether my websites smtp is down or up using php . I tried to connect to port 25 on my server using fsockopen, then it returns true when there is smtp service running. Which is the best method to test whether smtp or ftp running using php script.
Asked
Active
Viewed 1.2k times
3 Answers
2
you are talking of a smtp server , so you must have the pear framework enabled. I had a link http://www.authsmtp.com/php-pear-mail/ which can be of help. or you can do this:
$f = fsockopen('smtp host', 25) ;
if ($f !== false) {
$res = fread($f, 1024) ;
if (strlen($res) > 0 && strpos($res, '220') === 0) {
echo "Success!" ;
}
else {
echo "Error: " . $res ;
}
}
fclose($f) ;

argentum47
- 2,385
- 1
- 18
- 20
0
I would use the PHP mail function:
http://php.net/manual/en/function.mail.php
to actually send an email to a different, and stable, account (possibly gmail?). The script would have to monitor this account and verify that the expected message was received. For reference read:
-
I want to check ftp and other services running on the server,so which is the best method. – user2307392 Apr 23 '13 at 12:29
-
I would use the same methodology, and actually test each component of the server in a practical way, and then look for a successful result. PHP has many ftp functions. http://php.net/manual/en/book.ftp.php – will Apr 23 '13 at 12:31
0
For smtp, you can use the stream_socket_client() this way:
<?php
$timeout = 30; // your own timeout value
$connection_type = 'tcp'; // may be ssl, sslv2, sslv3 or tls
$host = 'myserver';
$port = 25;
$fp = stream_socket_client("{$connection_type}://{$host}:{$port}", $errno, $errstr, $timeout);
if (!$fp) {
echo "$errstr ($errno)";
}
But for FTP, i'm not sure.
It's surely not the best solution but if it may help...

Charles-Édouard Coste
- 1,604
- 12
- 24