View POP3/SMTP protocol when you want to try to write an operation class, the core is nothing, is to use Fsockopen, and then write/Receive data, only to achieve the most core part of the function, as a learning Socket operation of the practiced hand. It refers to RFC 2449 and a foreign simple web mail system uebimiau part of the code, but absolutely no copy of his drip, HOHO, absolutely original.
Copy CodeThe code is as follows:
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 ();
//---------------
Basic operations
//---------------
constructor function
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 ();
}
Connecting to a server
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;
}
Close connection
function Closehost ()
{
if ($this->reshandler)
{
Fclose ($this->reshandler);
}
return true;
}
Send instructions
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;
}
Extract response information First line
function Getlineresponse ()
{
if (! $this->getisconnect ())
{
return false;
}
$this->strresponse = fgets ($this->reshandler, $this->intbuffsize);
$this->arrresponse[] = $this->strresponse;
return $this->strresponse;
}
//Extracts several response information, $intReturnType is the return value type, 1 is a string, 2 is an array
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;
}
}
Whether the fetch request was successful
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;
}
Gets whether the connection is connected
function Getisconnect ()
{
if (! $this->reshandler)
{
$this->setmessage ("Nonexistent availability Connection Handler", 2002);
return false;
}
return true;
}
Set Message
function Setmessage ($strMessage, $intErrorNum)
{
if (Trim ($strMessage) = = "| | $intErrorNum = =")
{
return false;
}
$this->strmessage = $strMessage;
$this->interrornum = $intErrorNum;
return true;
}
Get message
function GetMessage ()
{
return $this->strmessage;
}
Get error number
function Geterrornum ()
{
return $this->interrornum;
}
GET request Information
function Getrequest ()
{
return $this->strrequest;
}
Get response information
function GetResponse ()
{
return $this->strresponse;
}
//---------------
Mail Atomic operations
//---------------
Login Mailbox
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;
}
Sign Out
function Poplogout ()
{
if (! $this->getisconnect () && $this->bolislogin)
{
return false;
}
$this->sendcommand ("QUIT");
$this->getlineresponse ();
if (! $this->getrestissucceed ())
{
return false;
}
return true;
}
//Get online
Function getisonline ()
{
if (! $this->getisconnect () && $this->bolislogin)
{
return false;
}
$this->sendcommand ("NOOP");
$this->getlineresponse ();
if (! $this->getrestissucceed ())
{
return false;
}
return true;
}
//Get the number of messages and bytes (return array)
function getmailsum ($intReturnType =2)
{
if (! $this->getisconnect () && $t His->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 me Ssage is error ', 2006);
return false;
}
Return Array ($arrResponse [1], $arrResponse [2]);
}
}
//Gets the session Id of the specified message
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 me Ssage is error ', 2006);
return false;
}
Return Array ($arrResponse [1], $arrResponse [2]);
}
}
Get the size of a message
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]);
}
}
Get the message base list array
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);
}
//Gets all information for the specified message, Intreturntype is the return value type, 1 is a string, 2 is an array
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);
}
//Get a message before the specified line, $intReturnType return a value type, 1 is a string, 2 is an array
function Getmailtopmessage ($intMailId, $intTopLines = 10, $ intreturntype=1)
{
if (! $this->getisconnect () && $this->bolislogin)
{
return false;
if (! $intMailId =intval ($intMailId) | |! $intTopLines =int ($intTopLines))
{
$this->setmessage (' Mail mes Sage 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);
}
Delete message
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;
}
Reset deleted messages marked as not deleted
function Resetdelemail ()
{
if (! $this->getisconnect () && $this->bolislogin)
{
return false;
}
$this->sendcommand ("RSET");
$this->getlineresponse ();
if (! $this->getrestissucceed ())
{
return false;
}
return true;
}
//---------------
Debugging operations
//---------------
Output object Information
function PrintObject ()
{
Print_r ($this);
Exit
}
Output error message
function Printerror ()
{
echo "[Error MSG]: $strMessage
\ n ";
echo "[Error Num]: $intErrorNum
\ n ";
Exit
}
Output host Information
function Printhost ()
{
echo "[Host]: $this->strhost
\ n ";
echo "[Port]: $this->intport
\ n ";
echo "[Email]: $this->stremail
\ n ";
echo "[PASSWD]: ********
\ n ";
Exit
}
Output connection Information
function Printconnect ()
{
echo "[Connect]: $this->reshandler
\ n ";
echo "[Request]: $this->strrequest
\ n ";
echo "[Response]: $this->strresponse
\ n ";
Exit
}
}
?>
Test code
For example: $o = socketpop3client (' Email address ', ' password ', ' POP3 server ', ' POP3 Port ')
/*
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 ();
*/
?>
http://www.bkjia.com/PHPjc/824944.html www.bkjia.com true http://www.bkjia.com/PHPjc/824944.html techarticle View POP3/SMTP protocol when you want to try to write an operation class, the core is nothing, is to use Fsockopen, and then write/receive data, only realize the most core part of the function, ...