PHPソケットを使用して作成されたPOP3クラス
【摘 要】 查看 POP3/SMTP 协议的时候想尝试一下自己写一个操作类,核心没啥,就是使用 fsockopen ,然后写入/接收数据,只实现了最核心的部分功能,当作是学习 Socket 操作的练手。
查看 POP3/SMTP 协议的时候想尝试一下自己写一个操作类,核心没啥,就是使用 fsockopen ,然后写入/接收数据,只实现了最核心的部分功能,当作是学习 Socket 操作的练手。其中参考了 RFC 2449和一个国外的简单Web邮件系统 Uebimiau 的部分代码,不过绝对没有抄他滴,HOHO,绝对原创。如果你喜欢,请收藏,随便修改,嗯,但是记得不要删除偶类里的声名,毕竟偶也是辛辛苦苦写了好几天呐。
另外,欢迎自由发挥,改善或者修正这个类,希望能够为你所用。代码没有认真仔细的调试,发现bug请自己修正,HOHO!
/**
* 类名:SocketPOPClient
* 功能:POP3 协议客户端的基本操作类
* 作者:heiyeluren
* 时间:2006-7-3
* 参考:RFC 2449, Uebimiau
* 授权:BSD License
*/
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
\n";
echo "[Error Num] : $intErrorNum
\n";
exit;
}
// ホスト情報を出力する
function printHost() [メール]: $ this- & gt; br & gt; ";
エコー": *** ****** & lt; br & gt; n ";
exit;
}
//接続情報を出力
function printConnect()
{
echo "[リクエスト] : $this->strRequest
n";
with with with with / 例: $o = SocketPOP3Client('メールアドレス', 'パスワード', 'POP3サーバー', 'POP3 ポート')
/*
set_time_limit(0);
$o = new SocketPOPClient('abc@126.com' , '123456', 'pop.126.com', '110');
$o->popLogin();
print_r($o->getMailSum (1));
print_r($o->getMailTopMessage(2, 2) , 2));
*/

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック









ログイン画面に「組織から PIN の変更を求められています」というメッセージが表示されます。これは、個人のデバイスを制御できる組織ベースのアカウント設定を使用しているコンピューターで PIN の有効期限の制限に達した場合に発生します。ただし、個人アカウントを使用して Windows をセットアップした場合、エラー メッセージは表示されないのが理想的です。常にそうとは限りませんが。エラーが発生したほとんどのユーザーは、個人アカウントを使用して報告します。私の組織が Windows 11 で PIN を変更するように要求するのはなぜですか?アカウントが組織に関連付けられている可能性があるため、主なアプローチはこれを確認することです。ドメイン管理者に問い合わせると解決できます。さらに、ローカル ポリシー設定が間違っていたり、レジストリ キーが間違っていたりすると、エラーが発生する可能性があります。今すぐ

Windows 11 では、新鮮でエレガントなデザインが前面に押し出されており、最新のインターフェイスにより、ウィンドウの境界線などの細部をカスタマイズして変更することができます。このガイドでは、Windows オペレーティング システムで自分のスタイルを反映した環境を作成するのに役立つ手順について説明します。ウィンドウの境界線の設定を変更するにはどうすればよいですか? + を押して設定アプリを開きます。 Windows [個人用設定] に移動し、[色の設定] をクリックします。ウィンドウの境界線の色の変更設定ウィンドウ 11" width="643" height="500" > [タイトル バーとウィンドウの境界線にアクセント カラーを表示する] オプションを見つけて、その横にあるスイッチを切り替えます。 [スタート] メニューとタスク バーにアクセント カラーを表示するにはスタート メニューとタスク バーにテーマの色を表示するには、[スタート メニューとタスク バーにテーマを表示] をオンにします。

デフォルトでは、Windows 11 のタイトル バーの色は、選択したダーク/ライト テーマによって異なります。ただし、任意の色に変更できます。このガイドでは、デスクトップ エクスペリエンスを変更し、視覚的に魅力的なものにするためにカスタマイズする 3 つの方法について、段階的な手順を説明します。アクティブなウィンドウと非アクティブなウィンドウのタイトル バーの色を変更することはできますか?はい、設定アプリを使用してアクティブなウィンドウのタイトル バーの色を変更したり、レジストリ エディターを使用して非アクティブなウィンドウのタイトル バーの色を変更したりできます。これらの手順を学習するには、次のセクションに進んでください。 Windows 11でタイトルバーの色を変更するにはどうすればよいですか? 1. 設定アプリを使用して + を押して設定ウィンドウを開きます。 Windows「個人用設定」に進み、

Windows インストーラー ページに「問題が発生しました」というメッセージとともに「OOBELANGUAGE」というメッセージが表示されますか?このようなエラーが原因で Windows のインストールが停止することがあります。 OOBE とは、すぐに使えるエクスペリエンスを意味します。エラー メッセージが示すように、これは OOBE 言語の選択に関連する問題です。心配する必要はありません。OOBE 画面自体から気の利いたレジストリ編集を行うことで、この問題を解決できます。クイックフィックス – 1. OOBE アプリの下部にある [再試行] ボタンをクリックします。これにより、問題が発生することなくプロセスが続行されます。 2. 電源ボタンを使用してシステムを強制的にシャットダウンします。システムの再起動後、OOBE が続行されます。 3. システムをインターネットから切断します。 OOBE のすべての側面をオフライン モードで完了する

タスクバーのサムネイルは楽しい場合もありますが、気が散ったり煩わしい場合もあります。この領域にマウスを移動する頻度を考えると、重要なウィンドウを誤って閉じてしまったことが何度かある可能性があります。もう 1 つの欠点は、より多くのシステム リソースを使用することです。そのため、リソース効率を高める方法を探している場合は、それを無効にする方法を説明します。ただし、ハードウェアの仕様が対応可能で、プレビューが気に入った場合は、有効にすることができます。 Windows 11でタスクバーのサムネイルプレビューを有効にする方法は? 1. 設定アプリを使用してキーをタップし、[設定] をクリックします。 Windows では、「システム」をクリックし、「バージョン情報」を選択します。 「システムの詳細設定」をクリックします。 [詳細設定] タブに移動し、[パフォーマンス] の下の [設定] を選択します。 「視覚効果」を選択します

多くのユーザーはスマートウォッチを選ぶときにファーウェイブランドを選択しますが、その中でもファーウェイ GT3pro と GT4 は非常に人気のある選択肢であり、多くのユーザーはファーウェイ GT3pro と GT4 の違いに興味を持っています。 Huawei GT3pro と GT4 の違いは何ですか? 1. 外観 GT4: 46mm と 41mm、材質はガラスミラー + ステンレススチールボディ + 高解像度ファイバーバックシェルです。 GT3pro: 46.6mm および 42.9mm、材質はサファイアガラス + チタンボディ/セラミックボディ + セラミックバックシェルです。 2. 健全な GT4: 最新の Huawei Truseen5.5+ アルゴリズムを使用すると、結果はより正確になります。 GT3pro: ECG 心電図と血管と安全性を追加

Windows 11 のディスプレイ スケーリングに関しては、好みが人それぞれ異なります。大きなアイコンを好む人もいれば、小さなアイコンを好む人もいます。ただし、適切なスケーリングが重要であることには誰もが同意します。フォントのスケーリングが不十分であったり、画像が過度にスケーリングされたりすると、作業中の生産性が大幅に低下する可能性があるため、システムの機能を最大限に活用するためにカスタマイズする方法を知る必要があります。カスタム ズームの利点: これは、画面上のテキストを読むのが難しい人にとって便利な機能です。一度に画面上でより多くの情報を確認できるようになります。特定のモニターおよびアプリケーションにのみ適用するカスタム拡張プロファイルを作成できます。ローエンド ハードウェアのパフォーマンスの向上に役立ちます。画面上の内容をより詳細に制御できるようになります。 Windows 11の使用方法

画面の明るさは、最新のコンピューティング デバイスを使用する上で不可欠な部分であり、特に長時間画面を見る場合には重要です。目の疲れを軽減し、可読性を向上させ、コンテンツを簡単かつ効率的に表示するのに役立ちます。ただし、設定によっては、特に新しい UI が変更された Windows 11 では、明るさの管理が難しい場合があります。明るさの調整に問題がある場合は、Windows 11 で明るさを管理するすべての方法を次に示します。 Windows 11で明るさを変更する方法【10の方法を解説】 シングルモニターユーザーは、次の方法でWindows 11の明るさを調整できます。これには、ラップトップだけでなく、単一のモニターを使用するデスクトップ システムも含まれます。はじめましょう。方法 1: アクション センターを使用する アクション センターにアクセスできる
