中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何使用PHP Socket寫的POP3類

發布時間:2021-10-08 16:37:53 來源:億速云 閱讀:125 作者:iii 欄目:開發技術

本篇內容介紹了“如何使用PHP Socket寫的POP3類”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!

復制代碼 代碼如下:

<?php
class SocketPOPClient
{
    var $strMessage        = '';
    var $intErrorNum    = 0;
    var $bolDebug        = false;

    var $strEmail        = '';
    var $strPasswd        = '';
    var $strHost        = '';
    var $intPort        = 110;
    var $intConnSecond    = 30;
    var $intBuffSize    = 8192;

    var $resHandler        = NULL;
    var $bolIsLogin        = false;
    var $strRequest        = '';
    var $strResponse    = '';
    var $arrRequest        = array();
    var $arrResponse    = array();


    //---------------
    // 基礎操作
    //---------------

    //構造函數
    function SocketPOP3Client($strLoginEmail, $strLoginPasswd, $strPopHost='', $intPort='')
    {
        $this->strEmail        = trim(strtolower($strLoginEmail));
        $this->strPasswd    = trim($strLoginPasswd);
        $this->strHost        = trim(strtolower($strPopHost));

        if ($this->strEmail=='' || $this->strPasswd=='')
        {
            $this->setMessage('Email address or Passwd is empty', 1001);
            return false;
        }
        if (!PReg_match("/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/i", $this->strEmail))
        {
            $this->setMessage('Email address invalid', 1002);
            return false;
        }
        if ($this->strHost=='')
        {
            $this->strHost = substr(strrchr($this->strEmail, "@"), 1);
        }
        if ($intPort!='')
        {
            $this->intPort = $intPort;
        }
        $this->connectHost();
    }

    //連接服務器
    function connectHost()
    {
        if ($this->bolDebug)
        {
            echo "Connection ".$this->strHost." ...\r\n";
        }
        if (!$this->getIsConnect())
        {
            if ($this->strHost=='' || $this->intPort=='')
            {
                $this->setMessage('POP3 host or Port is empty', 1003);
                return false;            
            }
            $this->resHandler = @fsockopen($this->strHost, $this->intPort, &$this->intErrorNum, &$this->strMessage, $this->intConnSecond);
            if (!$this->resHandler)
            {
                $strErrMsg = 'Connection POP3 host: '.$this->strHost.' failed';
                $intErrNum = 2001;
                $this->setMessage($strErrMsg, $intErrNum);
                return false;
            }
            $this->getLineResponse();
            if (!$this->getRestIsSucceed())
            {
                return false;
            }
        }
        return true;
    }

    //關閉連接
    function closeHost()
    {
        if ($this->resHandler)
        {
            fclose($this->resHandler);
        }
        return true;
    }

    //發送指令
    function sendCommand($strCommand)
    {
        if ($this->bolDebug)
        {
            if (!preg_match("/PASS/", $strCommand))
            {
                echo "Send Command: ".$strCommand."\r\n";
            }
            else
            {
                echo "Send Command: PASS ******\r\n";
            }

        }
        if (!$this->getIsConnect())
        {
            return false;
        }
        if (trim($strCommand)=='')
        {
            $this->setMessage('Request command is empty', 1004);
            return false;
        }
        $this->strRequest = $strCommand."\r\n";
        $this->arrRequest[] = $strCommand;
        fputs($this->resHandler, $this->strRequest);
        return true;
    }

    //提取響應信息第一行
    function getLineResponse()
    {
        if (!$this->getIsConnect())
        {
            return false;
        }
        $this->strResponse = fgets($this->resHandler, $this->intBuffSize);
        $this->arrResponse[] = $this->strResponse;

        return $this->strResponse;        
    }

    //提取若干響應信息,$intReturnType是返回值類型, 1為字符串, 2為數組
    function getRespMessage($intReturnType)
    {
        if (!$this->getIsConnect())
        {
            return false;
        }
        if ($intReturnType == 1)
        {
            $strAllResponse = '';
            while(!feof($this->resHandler))
            {
                $strLineResponse = $this->getLineResponse();
                if (preg_match("/^\+OK/", $strLineResponse))
                {
                    continue;
                }
                if (trim($strLineResponse)=='.')
                {
                    break;
                }
                $strAllResponse .= $strLineResponse;
            }
            return $strAllResponse;
        }
        else
        {
            $arrAllResponse = array();
            while(!feof($this->resHandler))
            {
                $strLineResponse = $this->getLineResponse();
                if (preg_match("/^\+OK/", $strLineResponse))
                {
                    continue;
                }
                if (trim($strLineResponse)=='.')
                {
                    break;
                }
                $arrAllResponse[] = $strLineResponse;
            }
            return $arrAllResponse;            
        }
    }

    //提取請求是否成功
    function getRestIsSucceed($strRespMessage='')
    {
        if (trim($responseMessage)=='')
        {
            if ($this->strResponse=='')
            {
                $this->getLineResponse();
            }
            $strRespMessage = $this->strResponse;
        }
        if (trim($strRespMessage)=='')
        {
            $this->setMessage('Response message is empty', 2003);
            return false;
        }
        if (!preg_match("/^\+OK/", $strRespMessage))
        {
            $this->setMessage($strRespMessage, 2000);
            return false;
        }
        return true;
    }

    //獲取是否已連接
    function getIsConnect()
    {
        if (!$this->resHandler)
        {
            $this->setMessage("Nonexistent availability connection handler", 2002);
            return false;
        }
        return true;
    }


    //設置消息
    function setMessage($strMessage, $intErrorNum)
    {
        if (trim($strMessage)=='' || $intErrorNum=='')
        {
            return false;
        }
        $this->strMessage    = $strMessage;
        $this->intErrorNum    = $intErrorNum;
        return true;
    }

    //獲取消息
    function getMessage()
    {
        return $this->strMessage;
    }

    //獲取錯誤號
    function getErrorNum()
    {
        return $this->intErrorNum;
    }

    //獲取請求信息
    function getRequest()
    {
        return $this->strRequest;        
    }

    //獲取響應信息
    function getResponse()
    {
        return $this->strResponse;
    }


    //---------------
    // 郵件原子操作
    //---------------

    //登錄郵箱
    function popLogin()
    {
        if (!$this->getIsConnect())
        {
            return false;
        }
        $this->sendCommand("USER ".$this->strEmail);
        $this->getLineResponse();
        $bolUserRight = $this->getRestIsSucceed();

        $this->sendCommand("PASS ".$this->strPasswd);
        $this->getLineResponse();
        $bolPassRight = $this->getRestIsSucceed();

        if (!$bolUserRight || !$bolPassRight)
        {
            $this->setMessage($this->strResponse, 2004);
            return false;
        }        
        $this->bolIsLogin = true;
        return true;
    }

    //退出登錄
    function popLogout()
    {
        if (!$this->getIsConnect() && $this->bolIsLogin)
        {
            return false;
        }
        $this->sendCommand("QUIT");
        $this->getLineResponse();
        if (!$this->getRestIsSucceed())
        {
            return false;
        }
        return true;
    }

    //獲取是否在線
    function getIsOnline()
    {
        if (!$this->getIsConnect() && $this->bolIsLogin)
        {
            return false;
        }
        $this->sendCommand("NOOP");
        $this->getLineResponse();
        if (!$this->getRestIsSucceed())
        {
            return false;
        }
        return true;        
    }

    //獲取郵件數量和字節數(返回數組)
    function getMailSum($intReturnType=2)
    {
        if (!$this->getIsConnect() && $this->bolIsLogin)
        {
            return false;
        }
        $this->sendCommand("STAT");
        $strLineResponse = $this->getLineResponse();
        if (!$this->getRestIsSucceed())
        {
            return false;
        }
        if ($intReturnType==1)
        {
            return     $this->strResponse;
        }
        else
        {
            $arrResponse = explode(" ", $this->strResponse);
            if (!is_array($arrResponse) || count($arrResponse)<=0)
            {
                $this->setMessage('STAT command response message is error', 2006);
                return false;
            }
            return array($arrResponse[1], $arrResponse[2]);
        }
    }

    //獲取指定郵件得session Id
    function getMailSessId($intMailId, $intReturnType=2)
    {
        if (!$this->getIsConnect() && $this->bolIsLogin)
        {
            return false;
        }
        if (!$intMailId = intval($intMailId))
        {
            $this->setMessage('Mail message id invalid', 1005);
            return false;
        }
        $this->sendCommand("UIDL ". $intMailId);
        $this->getLineResponse();
        if (!$this->getRestIsSucceed())
        {
            return false;
        }
        if ($intReturnType == 1)
        {
            return     $this->strResponse;
        }
        else
        {
            $arrResponse = explode(" ", $this->strResponse);
            if (!is_array($arrResponse) || count($arrResponse)<=0)
            {
                $this->setMessage('UIDL command response message is error', 2006);
                return false;
            }
            return array($arrResponse[1], $arrResponse[2]);
        }
    }

    //取得某個郵件的大小
    function getMailSize($intMailId, $intReturnType=2)
    {
        if (!$this->getIsConnect() && $this->bolIsLogin)
        {
            return false;
        }
        $this->sendCommand("LIST ".$intMailId);
        $this->getLineResponse();
        if (!$this->getRestIsSucceed())
        {
            return false;
        }
        if ($intReturnType == 1)
        {
            return $this->strResponse;
        }
        else
        {
            $arrMessage = explode(' ', $this->strResponse);
            return array($arrMessage[1], $arrMessage[2]);
        }
    }

    //獲取郵件基本列表數組
    function getMailBaseList($intReturnType=2)
    {
        if (!$this->getIsConnect() && $this->bolIsLogin)
        {
            return false;
        }
        $this->sendCommand("LIST");
        $this->getLineResponse();
        if (!$this->getRestIsSucceed())
        {
            return false;
        }
        return $this->getRespMessage($intReturnType);
    }

    //獲取指定郵件所有信息,intReturnType是返回值類型,1是字符串,2是數組
    function getMailMessage($intMailId, $intReturnType=1)
    {
        if (!$this->getIsConnect() && $this->bolIsLogin)
        {
            return false;
        }
        if (!$intMailId = intval($intMailId))
        {
            $this->setMessage('Mail message id invalid', 1005);
            return false;
        }
        $this->sendCommand("RETR ". $intMailId);
        $this->getLineResponse();
        if (!$this->getRestIsSucceed())
        {
            return false;
        }
        return $this->getRespMessage($intReturnType);
    }

    //獲取某郵件前指定行, $intReturnType 返回值類型,1是字符串,2是數組
    function getMailTopMessage($intMailId, $intTopLines=10, $intReturnType=1)
    {
        if (!$this->getIsConnect() && $this->bolIsLogin)
        {
            return false;
        }
        if (!$intMailId=intval($intMailId) || !$intTopLines=int($intTopLines))
        {
            $this->setMessage('Mail message id or Top lines number invalid', 1005);
            return false;
        }
        $this->sendCommand("TOP ". $intMailId ." ". $intTopLines);
        $this->getLineResponse();
        if (!$this->getRestIsSucceed())
        {
            return false;
        }
        return $this->getRespMessage($intReturnType);
    }

    //刪除郵件
    function delMail($intMailId)
    {
        if (!$this->getIsConnect() && $this->bolIsLogin)
        {
            return false;
        }
        if (!$intMailId=intval($intMailId))
        {
            $this->setMessage('Mail message id invalid', 1005);
            return false;
        }
        $this->sendCommand("DELE ".$intMailId);
        $this->getLineResponse();
        if (!$this->getRestIsSucceed())
        {
            return false;
        }
        return true;
    }

    //重置被刪除得郵件標記為未刪除
    function resetDeleMail()
    {
        if (!$this->getIsConnect() && $this->bolIsLogin)
        {
            return false;
        }
        $this->sendCommand("RSET");
        $this->getLineResponse();
        if (!$this->getRestIsSucceed())
        {
            return false;
        }
        return true;        
    }

    //---------------
    // 調試操作
    //---------------

    //輸出對象信息
    function printObject()
    {
        print_r($this);
        exit;
    }

    //輸出錯誤信息
    function printError()
    {
        echo "[Error Msg] : $strMessage     <br>\n";
        echo "[Error Num] : $intErrorNum <br>\n";
        exit;
    }

    //輸出主機信息
    function printHost()
    {
        echo "[Host]  : $this->strHost <br>\n";
        echo "[Port]  : $this->intPort <br>\n";
        echo "[Email] : $this->strEmail <br>\n";
        echo "[Passwd] : ******** <br>\n";
        exit;
    }

    //輸出連接信息
    function printConnect()
    {
        echo "[Connect] : $this->resHandler <br>\n";
        echo "[Request] : $this->strRequest <br>\n";
        echo "[Response] : $this->strResponse <br>\n";
        exit;
    }
}

?>
<?
//測試代碼
//例如:$o = SocketPOP3Client('郵箱地址', '密碼', 'POP3服務器', 'POP3端口')

/*
set_time_limit(0);
$o = new SocketPOPClient('abc@126.com', '123456', 'pop.126.com', '110');
$o->popLogin();
print_r($o->getMailBaseList());
print_r($o->getMailSum(1));
print_r($o->getMailTopMessage(2, 2, 2));
$o->popLogout();
$o->closeHost();
$o->printObject();
*/
?>

“如何使用PHP Socket寫的POP3類”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

php
AI

抚顺市| 盐城市| 东兴市| 涞源县| 南康市| 临泉县| 枣庄市| 乐清市| 满洲里市| 兴和县| 藁城市| 三明市| 太仆寺旗| 怀仁县| 凉城县| 无棣县| 壶关县| 大渡口区| 汪清县| 叙永县| 新竹县| 鸡东县| 土默特右旗| 邵阳县| 临安市| 聊城市| 延边| 宣化县| 四平市| 保山市| 台北市| 延庆县| 陵水| 波密县| 石渠县| 深圳市| 九江市| 寻甸| 平顺县| 蒙自县| 汶川县|