Guest User

Untitled

a guest
Jul 28th, 2018
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.94 KB | None | 0 0
  1. <?php
  2. /*
  3. * file: mail_parser.php
  4. * date: 25.04.10
  5. * author: Tyslenko Max
  6. */
  7.  
  8. /*
  9. * This code is partly taken from article: webi.ru/webi_articles/6_12_f.html
  10. */
  11. class MailDecoder
  12. {
  13. function fetch_structure($email) {
  14. $ARemail = Array();
  15. $separador = "\r\n\r\n";
  16. $header = trim(substr($email,0,strpos($email,$separador)));
  17. $bodypos = strlen($header)+strlen($separador);
  18. $body = substr($email,$bodypos,strlen($email)-$bodypos);
  19. $ARemail["header"] = $header;
  20. $ARemail["body"] = $body;
  21. return $ARemail;
  22. }
  23.  
  24. function decode_header($header) {
  25. $headers = explode("\r\n",$header);
  26.  
  27. $decodedheaders = Array();
  28.  
  29. for($i=1;$i<count($headers);$i++) {
  30. $thisheader = trim($headers[$i]);
  31. if(!empty($thisheader))
  32. {
  33. if(!ereg("^[A-Z0-9a-z_-]+:", $thisheader))
  34. {
  35. $decodedheaders[$lasthead] .= " $thisheader";
  36. }
  37. else
  38. {
  39. $dbpoint = strpos($thisheader,":");
  40. $headname = strtolower(substr($thisheader,0,$dbpoint));
  41. $headvalue = trim(substr($thisheader,$dbpoint+1));
  42.  
  43. if (!array_key_exists($headname, $decodedheaders))
  44. {
  45. $decodedheaders[$headname] = "; $headvalue";
  46. }
  47. else
  48. {
  49. $decodedheaders[$headname] = $headvalue;
  50. }
  51. $lasthead = $headname;
  52. }
  53. }
  54. }
  55.  
  56. return $decodedheaders;
  57. }
  58. }
  59.  
  60. class Pop3Worker
  61. {
  62. private static $instance = NULL;
  63. private $pop_conn = NULL;
  64. private $mailDecoder = NULL;
  65.  
  66. // Data for pop3-connection
  67. private $pop3Server = '62.113.86.215'; // mail.roller.ru
  68. private $pop3User = 'mail-robot%roller.ru';
  69. private $pop3Pass = 'amsterdam';
  70.  
  71. private $successAuth = False;
  72. private $letterCount = 0;
  73.  
  74. /*
  75. * Get instance from singleton-class
  76. */
  77. static public function getInstance()
  78. {
  79. if (self::$instance == NULL) self::$instance = new Pop3Worker();
  80. return self::$instance;
  81. }
  82.  
  83. /*
  84. * Class constructor. Private due to singleton-style
  85. */
  86. private function __construct()
  87. {
  88. $this->mailDecoder = new MailDecoder();
  89.  
  90. $this->connectPop3Server();
  91. $this->authPop3Server();
  92. $this->startWork();
  93. $this->processMail();
  94.  
  95. $this->disconnectPop3Server();
  96. }
  97.  
  98. /*
  99. * Class clone constructor. Private due to singleton-style
  100. */
  101. private function __clone() {}
  102.  
  103.  
  104. /*
  105. * Test connection with Pop3-server
  106. */
  107. private function connectPop3Server()
  108. {
  109. $this->pop_conn = fsockopen($this->pop3Server, 110, $errno, $errstr, 30);
  110.  
  111. print '<b>Trying to connect pop3-server...<br /></b>';
  112. $connectResult = fgets($this->pop_conn, 1024);
  113. if (strpos($connectResult, 'OK'))
  114. {
  115. echo 'Successful connect!<br />';
  116. }
  117. else
  118. {
  119. echo 'Failed to connect pop3-sever!<br />';
  120. }
  121. }
  122.  
  123. /*
  124. * Disconnect from pop3 server
  125. */
  126. private function disconnectPop3Server()
  127. {
  128. fputs($this->pop_conn,"QUIT\r\n");
  129.  
  130. echo '<b>Connectin is closed</b>';
  131. }
  132.  
  133. /*
  134. * Process authorization
  135. */
  136. private function authPop3Server()
  137. {
  138. print '<b>Trying to process authorization under pop3-server...<br /></b>';
  139.  
  140. fputs($this->pop_conn, "USER ".$this->pop3User."\r\n");
  141. $connectResult = fgets($this->pop_conn, 1024);
  142. if (strpos($connectResult, 'OK'))
  143. {
  144. fputs($this->pop_conn, "PASS ".$this->pop3Pass."\r\n");
  145. $connectResult = fgets($this->pop_conn, 1024);
  146.  
  147. if (strpos($connectResult, 'OK'))
  148. {
  149. echo 'Success authorization!<br />';
  150. $this->successAuth = True;
  151. }
  152. }
  153. else
  154. {
  155. echo 'Failed to process authorization!<br />';
  156. }
  157. }
  158.  
  159. /*
  160. * Check connection status by achiving mail information
  161. */
  162. private function startWork()
  163. {
  164. print '<b>Receiving mail information from pop3-server...<br /></b>';
  165.  
  166. fputs($this->pop_conn, "STAT\r\n");
  167. $connectResult = fgets($this->pop_conn, 1024);
  168. if (strpos($connectResult, 'OK'))
  169. {
  170. $pattern = '/OK ([0-9]+) [0-9]+/';
  171. preg_match($pattern, $connectResult, $matches);
  172. $this->letterCount = intval($matches[1]);
  173.  
  174. echo 'Information is achived! <b>' . $this->letterCount . '</b> new letters.<br />';
  175. }
  176. else
  177. {
  178. echo 'Failed to get information from pop3-sever!<br />';
  179. }
  180. }
  181.  
  182.  
  183. /*
  184. * Get answer from server
  185. */
  186. private function getData($streamConnection)
  187. {
  188. $data = "";
  189. while (!feof($streamConnection)) {
  190. $buffer = chop(fgets($streamConnection,1024));
  191. $data .= "$buffer\r\n";
  192. if(trim($buffer) == ".") break;
  193. }
  194. return $data;
  195. }
  196.  
  197. /*
  198. * Receive all mails
  199. */
  200. private function processMail()
  201. {
  202. echo '<br />------- BEGIN PARSING -------<br />';
  203. $counter = 0;
  204. for ($i=1; $i <= $this->letterCount; $i++)
  205. {
  206. fputs($this->pop_conn, "RETR ".$i."\r\n");
  207. $text = $this->getData($this->pop_conn);
  208.  
  209. $struct = $this->mailDecoder->fetch_structure($text);
  210.  
  211. $pattern = '/Final-Recipient: RFC822; (.*)/';
  212. $pattern2 = '/\<(.*)\>\: conversation with/';
  213. $pattern3 = '/; originally to rfc822;(.*) \(unrecoverable error\)/';
  214. $pattern4 = '/\" \<(.*)\>/';
  215.  
  216.  
  217. if(preg_match($pattern, $struct['body'], $matches) > 0)
  218. {
  219. echo '1) E-mail to remove: <b>' . $matches[1] . '</b><br />'; $counter++;
  220. }
  221. elseif(preg_match($pattern2, $struct['body'], $matches) > 0)
  222. {
  223. echo '2) E-mail to remove: <b>' . $matches[1] . '</b><br />';$counter++;
  224. }
  225. elseif(preg_match($pattern3, $struct['body'], $matches) > 0)
  226. {
  227. echo '3) E-mail to remove: <b>' . $matches[1] . '</b><br />';$counter++;
  228. }
  229. elseif(preg_match($pattern4, $struct['body'], $matches) > 0)
  230. {
  231. echo '4) E-mail to remove: <b>' . $matches[1] . '</b><br />';$counter++;
  232. }
  233. }
  234. echo 'Number of emails: <b>' . $counter . '</b>';
  235. echo '<br />------- END PARSING -------<br /> <br />';
  236. }
  237. }
  238.  
  239. // You could use this anywhere...
  240. $pop3Handler = Pop3Worker::getInstance();
  241.  
  242. ?>
Add Comment
Please, Sign In to add comment