SMTP::Connected PHP Method

Connected() public method

Returns true if connected to a server otherwise false
public Connected ( ) : boolean
return boolean
    public function Connected()
    {
        if (!empty($this->smtp_conn)) {
            $sock_status = socket_get_status($this->smtp_conn);
            if ($sock_status['eof']) {
                // the socket is valid but we are not connected
                if ($this->do_debug >= 1) {
                    echo 'SMTP -> NOTICE:' . $this->CRLF . 'EOF caught while checking if connected';
                }
                $this->Close();
                return false;
            }
            return true;
            // everything looks good
        }
        return false;
    }

Usage Example

 function checkSMTP($smtp_server, $smtp_port = 25, $username, $password, $auth_enabled = false, $tls_enabled = true)
 {
     require_once "libs/phpmailer/class.smtp.php";
     $smtp = new SMTP();
     $smtp->Connect($smtp_server, $smtp_port);
     if (!$smtp->Connected()) {
         return array("ERROR" => "Failed to connect to server", "SMTP_ERROR" => $smtp->getError());
     }
     if (!$smtp->Hello()) {
         return array("ERROR" => "Failed to send hello command", "SMTP_ERROR" => $smtp->getError());
     }
     if ($tls_enabled) {
         if (!$smtp->StartTLS()) {
             return array("ERROR" => "Failed to start TLS", "SMTP_ERROR" => $smtp->getError());
         }
     }
     if ($auth_enabled) {
         if (!$smtp->Authenticate($username, $password)) {
             $error = $smtp->getError();
             if (preg_match("/STARTTLS/", $error['smtp_msg'])) {
                 return array("ERROR" => "Authenticate Error, TLS must be activated", "SMTP_ERROR" => $smtp->getError());
             } else {
                 return array("ERROR" => "Authenticate not accepted from server", "SMTP_ERROR" => $smtp->getError());
             }
         }
     }
     return true;
 }
All Usage Examples Of SMTP::Connected