如何通过PHP检查我的脚本是否连接到SMTP服务器

时间:2010-09-09 11:00:46

标签: php exchange-server

简单的标题是什么。 想知道如何检查连接是否正常,如果没有,那么错误是什么。 顺便说一句,SMTP服务器是Exchange 2007。

3 个答案:

答案 0 :(得分:14)

如果您想知道是否可以从运行PHP的任何地方访问SMTP服务器,那么您只需要在相应的端口(默认为25)上连接它,并查看是否返回“220”代码结果。

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

答案 1 :(得分:0)

由于这很可能与您的其他问题有关:Configure mail server to work with PHP,我也会在这里提出答案:

您正在使用PEAR Mail包。 send()方法在成功时返回TRUE,否则返回PEAR_Error对象,其中包含失败的详细信息。很可能你想要$ PEAR_Error :: message。完整详情:Configure mail server to work with PHP

答案 2 :(得分:0)

可能是由于你的SMTP端口不允许在防火墙上所以试试这个php脚本来检查开放端口,它有最大的流行端口检查:

<?php

$ports[] = array('host'=>'interspire.smtp.com','number'=>25);
$ports[] = array('host'=>'interspire.smtp.com','number'=>2525);
$ports[] = array('host'=>'interspire.smtp.com','number'=>25025);
$ports[] = array('host'=>'helpme.interspire.smtp.com','number'=>80);

$ports[] = array('host'=>'google.com','number'=>80);
$ports[] = array('host'=>'smtp.gmail.com','number'=>587);
$ports[] = array('host'=>'smtp.gmail.com','number'=>465);
$ports[] = array('host'=>'pop.gmail.com','number'=>995);
$ports[] = array('host'=>'imap.gmail.com','number'=>993);

$ports[] = array('host'=>'ftp.mozilla.org','number'=>21);
$ports[] = array('host'=>'smtp2go.com','number'=>8025);

$ports[] = array('host'=>'relay.dnsexit.com','number'=>25);
$ports[] = array('host'=>'relay.dnsexit.com','number'=>26);
$ports[] = array('host'=>'relay.dnsexit.com','number'=>940);
$ports[] = array('host'=>'relay.dnsexit.com','number'=>8001);
$ports[] = array('host'=>'relay.dnsexit.com','number'=>2525);
$ports[] = array('host'=>'relay.dnsexit.com','number'=>80);

$ports[] = array('host'=>'mail.authsmtp.com','number'=>23);
$ports[] = array('host'=>'mail.authsmtp.com','number'=>25);
$ports[] = array('host'=>'mail.authsmtp.com','number'=>26);
$ports[] = array('host'=>'mail.authsmtp.com','number'=>2525);

foreach ($ports as $port)
{
    //$connection = @fsockopen($port['host'], $port['number']);
    $connection = @fsockopen($port['host'], $port['number'], $errno, $errstr, 5); // 5 second timeout for each port.

    if (is_resource($connection))
    {
        echo '<h2>' . $port['host'] . ':' . $port['number'] . ' ' . '(' . getservbyport($port, 'tcp') . ') is open.</h2>' . "\n";

        fclose($connection);
    }

    else
    {
        echo '<h2>' . $port['host'] . ':' . $port['number'] . ' is not responding.</h2>' . "\n";
    }
}


?>

来自:https://www.interspire.com/support/kb/getattachment.php?data=MTA2OHxwb3J0Y2hlY2sucGhw