如何通过PHP检查我的脚本是否连接到SMTP服务器
问题描述:
就是标题所说的. 想知道如何检查连接是否正常,如果不正常,那是什么错误. 顺便说一句,SMTP服务器是Exchange 2007.
Simply what the title says. Want to know how to check if the connection is working and if not, what is the error. Btw the SMTP server is exchange 2007.
答
如果您想知道是否可以从运行PHP的任何地方访问SMTP服务器,则只需在适当的端口上连接它即可(默认25),看看结果中是否返回了"220"代码.
If you want to know if you can access the SMTP server from wherever you are running PHP, then you just need to connect to it on the appropriate port (default 25) and see if you get back a "220" code in the result.
$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) ;