如何使用 php 检查 smtp 是否已连接。我是 php 的新手

how to check smtp connected or not using php. i am newbie to php

include("../confi.php");
require 'PHPMailerAutoload.php';

//echo $select_smtp="SELECT * FROM `smtp_connection`";
$select_smtp=mysql_query("SELECT * FROM `smtp_connection` WHERE `status`=1",$database);
while($result_select = mysql_fetch_array($select_smtp))
{

    $hostname= $result_select['host_name'];
    $username= $result_select['user_name'];
    $userpass= $result_select['user_pass'];
    $status= $result_select['status'];
    $time= $result_select['check_time'];
    $port="25/pop3";
    $mbox = imap_open( "{".$hostname.":".$port."/novalidate-cert}" , $username, $userpass);

    if ($mbox) {
        echo "connected";
        imap_close($mbox);
    } else {
        echo "not connected :<br>" . imap_last_error();
    }
}

如果你的问题真的是"how to check smtp connected or not",那么答案是:你做得很好!

imap_open() 的文档明确指出函数 returns 是一个资源或 FALSE 以防失败。因此,您的条件 if ($mbox) 可靠地检查连接是否成功。

您可以使条件更易读和明确:if (FALSE === $mbox)