TCP 110 → USER/PASS 認證 → STAT/LIST 統計 → RETR 下載 → QUIT 登出。需處理多行回應(以 .\r\n 結尾)。
class POP3 {
var $host, $port = 110, $timeout = 30;
var $socket, $errno, $errstr;
function connect() {
$this->socket = fsockopen($this->host, $this->port, $this->errno, $this->errstr, $this->timeout);
return $this->socket && $this->getResponse(); // +OK
}
function login($user, $pass) {
$this->send("USER $user");
$this->send("PASS $pass");
return $this->checkOK();
}
function getList() {
$this->send("LIST");
$lines = $this->getMultiline();
// 解析 "1 750" 格式
return $lines;
}
function getMessage($num) {
$this->send("RETR $num");
return $this->getMultiline(); // 完整郵件原始內容
}
function quit() {
$this->send("QUIT");
fclose($this->socket);
}
// 內部:發送指令、讀取單行/多行回應、檢查 +OK
}
fgets($socket, 1024) 逐行讀取.\r\n 結束,需循環讀取直到遇到單獨的點+OK / -ERRstream_set_timeout()下一篇將整合 MIME 解碼類別,完成完整收信程式。