POP3 class _php instance written using PHP socket

Source: Internet
Author: User
Tags explode session id

Look at the POP3/SMTP protocol when you want to try to write a class, the core is nothing, is the use of fsockopen, and then write/Receive data, only to achieve the most core part of the function, as a learning Socket operation of the practicing. 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 Code code as follows:



<?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 ();


//---------------
Basic operations
//---------------

Constructors
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 instruction
    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 a number of response information, $intReturnType is the return value type, 1 is a string, and 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;


}

   //Get Connected
    function getisconnect ()
    {
         if (! $this->reshandler)
        {
            $this->setmessage ("nonexistent Availability Connection Handler ", 2002);
            return false;
       }
        return true;
   }


Setting up messages
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 operation
//---------------

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;
   }

   //Exit login
    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 () && $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]);


}


}

Gets the session Id for 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 message 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 mail 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, and 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);


}

Specifies the line before getting a message, $intReturnType return value type, 1 is a string, and 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 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);


}

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 ()
    {
& nbsp;       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 <br>\n";
echo "[Error Num]: $intErrorNum <br>\n";
Exit
}

   /Output host information
    function printhost ()
    {
  & nbsp;     echo "[host] : $this->strhost <br>\n";
        echo "[port] : $this->intport <br>\n";
        Echo [Email]: $this->stremail <br>\n ";
        Echo [Passwd]: ******** <br>\n ";
        exit;
   }

Output connection Information
function Printconnect ()
{
echo "[Connect]: $this->reshandler <br>\n";
echo "[Request]: $this->strrequest <br>\n";
echo "[Response]: $this->strresponse <br>\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 ();
*/
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.