login  Naam:   Wachtwoord: 
Registreer je!
 Scripts:

Scripts > PHP > Grote systemen > [PHP5]POP3 class

[PHP5]POP3 class

Auteur: ikkedikke - 11 april 2006 - 15:26 - Gekeurd door: Thomas - Hits: 9594 - Aantal punten: (0 stemmen)



Het is een klasse om een webmail uit op te bouwen.
het bevat de mogelijkheden om berichten uit te lezen, lijsten ophalen en berichten verwijderen.
het is in PHP5 geschreven en daar hou ik het maar bij. als je echt belangstelling hebt wil ik je wel uitleggen hoe je dit ook voor eerdere versies bruikbaar maakt.
Er is een "manual" bijgesloten omdat ik een hekel heb aan enorme blokken commentaar midden in stukken code. (een manual over dit script, niet over het pop3 protocol)

Code:
Voorbeeld hoe te gebruiken:
  1. <pre><?
  2. include('pop3.php');
  3. $rPOP3handle = new pop3('localhost','mijnemail@mijndomijn','wachtwoord');
  4. print_r($rPOP3handle->retrieveMessage(1));
  5. ?>


de klasse:
  1. <?
  2. class pop3
  3. {
  4. private $rConnection, $sUser, $sPass, $sHost, $iPort, $sReturn;
  5. public function __construct($sHost,$sUser,$sPass,$iPort=110)
  6. {
  7. $this->sHost = $sHost;
  8. $this->iPort = $iPort;
  9. $this->sUser = $sUser;
  10. $this->sPass = $sPass;
  11. $this->rConnection = fsockopen($sHost,$iPort,$iErrorNum, $sErrorStr,5);
  12. if(substr(trim($this->sReturn = $this->stream_get_contents() ),0,3) != '+OK')
  13. {
  14. $this->throwError('fsockopen:'.$sErrorStr);
  15. return false;
  16. }
  17. $this->toSock('USER '.$sUser);
  18. $this->sReturn = trim($this->stream_get_contents());
  19. if(substr($this->sReturn,0,3) != '+OK')
  20. {
  21. return false;
  22. }
  23. $this->toSock('PASS '.$sPass);
  24. $this->sReturn = trim($this->stream_get_contents());
  25. if(substr($this->sReturn,0,3) != '+OK')
  26. {
  27. return false;
  28. }
  29. return true;
  30. }
  31. public function __destruct()
  32. {
  33. $this->toSock('QUIT');
  34. fclose($this->rConnection);
  35. }
  36. private function stream_get_contents($iLength = 512)
  37. {
  38. $sReturn = fread($this->rConnection,$iLength);
  39. return str_replace("\r\n","\n",$sReturn);
  40. }
  41. private function toSock($sStr)
  42. {
  43. if($this->rConnection)
  44. {
  45. if(fwrite($this->rConnection,$sStr."\n")===FALSE)
  46. {
  47. return false;
  48. }
  49. else
  50. {
  51. return true;
  52. }
  53. }
  54. else
  55. {
  56. $this->throwError('Geen verbinding');
  57. return false;
  58. }
  59. }
  60. private function throwError($sError)
  61. {
  62. echo '<br>
  63. <hr>
  64. Er is een fout opgetreden.<br>
  65. De volgende fout werd meegegeven:<br>'.$sError.'<hr>
  66. <br>';
  67. }
  68. public function deleteMessage($iBerichtNummer)
  69. {
  70. $this->toSock('DELE '.$iBerichtNummer);
  71. $this->sReturn = trim($this->stream_get_contents());
  72. return (substr($this->sReturn,0,3) == '+OK');
  73. }
  74. public function listMessages($iBerichtNummer = '')
  75. {
  76. if(!empty($iBerichtNummer))
  77. {
  78. if(!is_numeric($iBerichtNummer))
  79. {
  80. $this->throwError('Ongeldig berichtnummer!');
  81. return false;
  82. }
  83. else
  84. {
  85. $this->toSock('LIST '.$iBerichtNummer);
  86. $this->sReturn = trim($this->stream_get_contents());
  87. echo "\n".$this->sReturn."\n";
  88.  
  89. if(substr($this->sReturn,0,3) != '+OK')
  90. {
  91. return false;
  92. }
  93. $aReturnData = explode(' ',$this->sReturn);
  94. return array($aReturnData[1] => $aReturnData[2]);
  95. }
  96. }
  97. else
  98. {
  99. $this->toSock('LIST');
  100. echo "\ntoSock('LIST')\n";
  101. $this->sReturn = trim($this->stream_get_contents());
  102. if(substr($this->sReturn,0,3) != '+OK')
  103. {
  104. return false;
  105. }
  106. ereg("^\+OK ([0-9]*) [aegms]{8} \(([0-9]*) [ceost]{6}\)(.*)\.$",$this->sReturn,$aMatch);
  107. if($aMatch[1] > 0)
  108. {
  109. $aResult[0] = array($aMatch[1] => $aMatch[2]);
  110. $aReturnData = explode("\n",$this->sReturn);
  111. foreach($aReturnData as $sRij)
  112. {
  113. $aRij = explode(' ',$sRij);
  114. if(count($aRij) == 2)
  115. {
  116. $aResult[$aRij[0]] = $aRij[1];
  117. }
  118. }
  119. }
  120. else
  121. {
  122. $aResult = array(0=>array(0=>0));
  123. }
  124. return $aResult;
  125. }
  126. }
  127. public function retrieveMessage($iBerichtNummer)
  128. {
  129. $this->toSock('RETR '.$iBerichtNummer);
  130. $this->sReturn = trim($this->stream_get_contents());
  131. if(substr($this->sReturn,0,3) != '+OK')
  132. {
  133. $aReturn = false;
  134. }
  135. else
  136. {
  137. if(ereg("^\+OK ([0-9]*) [ceost]{6}\n(.*)\n\.",$this->sReturn,$aMatch))
  138. {
  139. //aMatch[1] => totale grootte bericht
  140. //aMatch[2] => totale bericht
  141.  
  142. // is het bericht groter dan 500 bytes
  143. // Ja? dan is nog niet het hele bericht uitgelezen
  144. if($aMatch[1] > 502)
  145. {
  146. $aMatch[2] .= stream_get_contents($aMatch[1] - 512);
  147. }
  148. // Headers+bericht
  149. $aReturn['message'] = $aMatch[2];
  150. // headers worden afgesloten met een dubbele newline.
  151. $aReturnData = explode("\n\n",$aMatch[2]);
  152. $aReturn['headers'] = $aReturnData[0];
  153. $iHeaderLength = strlen($aReturnData[0]);
  154. // lengte die overblijft na het afknippen van de headers
  155. $aReturn['body'] = substr($aMatch[2],$iHeaderLength+2);
  156. }
  157. else
  158. {
  159. // ereg vond geen matchniet.
  160. $this->throwError('System: POP3-Server returned an invalid messageformat!');
  161. echo $this->sReturn;
  162. return false;
  163. }
  164. }
  165. return $aReturn;
  166. }
  167. }
  168. ?>
Download code! Download code (.txt)

 Stemmen
Niet ingelogd.

 Reacties
Post een reactie
Lees de reacties (9)
© 2002-2024 Sitemasters.be - Regels - Laadtijd: 0.038s