how to check smtp server is working or not using php

左心房为你撑大大i 提交于 2019-12-12 18:29:33

问题


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.


回答1:


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) ; 



回答2:


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:

accessing my gmail inbox via php code




回答3:


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...



来源:https://stackoverflow.com/questions/16169295/how-to-check-smtp-server-is-working-or-not-using-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!