How to check, by PHP, if my script is connecting to SMTP server

前端 未结 3 1757
猫巷女王i
猫巷女王i 2020-12-19 13:04

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.

3条回答
  •  独厮守ぢ
    2020-12-19 13:55

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

提交回复
热议问题