Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- * file: mail_parser.php
- * date: 25.04.10
- * author: Tyslenko Max
- */
- /*
- * This code is partly taken from article: webi.ru/webi_articles/6_12_f.html
- */
- class MailDecoder
- {
- function fetch_structure($email) {
- $ARemail = Array();
- $separador = "\r\n\r\n";
- $header = trim(substr($email,0,strpos($email,$separador)));
- $bodypos = strlen($header)+strlen($separador);
- $body = substr($email,$bodypos,strlen($email)-$bodypos);
- $ARemail["header"] = $header;
- $ARemail["body"] = $body;
- return $ARemail;
- }
- function decode_header($header) {
- $headers = explode("\r\n",$header);
- $decodedheaders = Array();
- for($i=1;$i<count($headers);$i++) {
- $thisheader = trim($headers[$i]);
- if(!empty($thisheader))
- {
- if(!ereg("^[A-Z0-9a-z_-]+:", $thisheader))
- {
- $decodedheaders[$lasthead] .= " $thisheader";
- }
- else
- {
- $dbpoint = strpos($thisheader,":");
- $headname = strtolower(substr($thisheader,0,$dbpoint));
- $headvalue = trim(substr($thisheader,$dbpoint+1));
- if (!array_key_exists($headname, $decodedheaders))
- {
- $decodedheaders[$headname] = "; $headvalue";
- }
- else
- {
- $decodedheaders[$headname] = $headvalue;
- }
- $lasthead = $headname;
- }
- }
- }
- return $decodedheaders;
- }
- }
- class Pop3Worker
- {
- private static $instance = NULL;
- private $pop_conn = NULL;
- private $mailDecoder = NULL;
- // Data for pop3-connection
- private $pop3Server = '62.113.86.215'; // mail.roller.ru
- private $pop3User = 'mail-robot%roller.ru';
- private $pop3Pass = 'amsterdam';
- private $successAuth = False;
- private $letterCount = 0;
- /*
- * Get instance from singleton-class
- */
- static public function getInstance()
- {
- if (self::$instance == NULL) self::$instance = new Pop3Worker();
- return self::$instance;
- }
- /*
- * Class constructor. Private due to singleton-style
- */
- private function __construct()
- {
- $this->mailDecoder = new MailDecoder();
- $this->connectPop3Server();
- $this->authPop3Server();
- $this->startWork();
- $this->processMail();
- $this->disconnectPop3Server();
- }
- /*
- * Class clone constructor. Private due to singleton-style
- */
- private function __clone() {}
- /*
- * Test connection with Pop3-server
- */
- private function connectPop3Server()
- {
- $this->pop_conn = fsockopen($this->pop3Server, 110, $errno, $errstr, 30);
- print '<b>Trying to connect pop3-server...<br /></b>';
- $connectResult = fgets($this->pop_conn, 1024);
- if (strpos($connectResult, 'OK'))
- {
- echo 'Successful connect!<br />';
- }
- else
- {
- echo 'Failed to connect pop3-sever!<br />';
- }
- }
- /*
- * Disconnect from pop3 server
- */
- private function disconnectPop3Server()
- {
- fputs($this->pop_conn,"QUIT\r\n");
- echo '<b>Connectin is closed</b>';
- }
- /*
- * Process authorization
- */
- private function authPop3Server()
- {
- print '<b>Trying to process authorization under pop3-server...<br /></b>';
- fputs($this->pop_conn, "USER ".$this->pop3User."\r\n");
- $connectResult = fgets($this->pop_conn, 1024);
- if (strpos($connectResult, 'OK'))
- {
- fputs($this->pop_conn, "PASS ".$this->pop3Pass."\r\n");
- $connectResult = fgets($this->pop_conn, 1024);
- if (strpos($connectResult, 'OK'))
- {
- echo 'Success authorization!<br />';
- $this->successAuth = True;
- }
- }
- else
- {
- echo 'Failed to process authorization!<br />';
- }
- }
- /*
- * Check connection status by achiving mail information
- */
- private function startWork()
- {
- print '<b>Receiving mail information from pop3-server...<br /></b>';
- fputs($this->pop_conn, "STAT\r\n");
- $connectResult = fgets($this->pop_conn, 1024);
- if (strpos($connectResult, 'OK'))
- {
- $pattern = '/OK ([0-9]+) [0-9]+/';
- preg_match($pattern, $connectResult, $matches);
- $this->letterCount = intval($matches[1]);
- echo 'Information is achived! <b>' . $this->letterCount . '</b> new letters.<br />';
- }
- else
- {
- echo 'Failed to get information from pop3-sever!<br />';
- }
- }
- /*
- * Get answer from server
- */
- private function getData($streamConnection)
- {
- $data = "";
- while (!feof($streamConnection)) {
- $buffer = chop(fgets($streamConnection,1024));
- $data .= "$buffer\r\n";
- if(trim($buffer) == ".") break;
- }
- return $data;
- }
- /*
- * Receive all mails
- */
- private function processMail()
- {
- echo '<br />------- BEGIN PARSING -------<br />';
- $counter = 0;
- for ($i=1; $i <= $this->letterCount; $i++)
- {
- fputs($this->pop_conn, "RETR ".$i."\r\n");
- $text = $this->getData($this->pop_conn);
- $struct = $this->mailDecoder->fetch_structure($text);
- $pattern = '/Final-Recipient: RFC822; (.*)/';
- $pattern2 = '/\<(.*)\>\: conversation with/';
- $pattern3 = '/; originally to rfc822;(.*) \(unrecoverable error\)/';
- $pattern4 = '/\" \<(.*)\>/';
- if(preg_match($pattern, $struct['body'], $matches) > 0)
- {
- echo '1) E-mail to remove: <b>' . $matches[1] . '</b><br />'; $counter++;
- }
- elseif(preg_match($pattern2, $struct['body'], $matches) > 0)
- {
- echo '2) E-mail to remove: <b>' . $matches[1] . '</b><br />';$counter++;
- }
- elseif(preg_match($pattern3, $struct['body'], $matches) > 0)
- {
- echo '3) E-mail to remove: <b>' . $matches[1] . '</b><br />';$counter++;
- }
- elseif(preg_match($pattern4, $struct['body'], $matches) > 0)
- {
- echo '4) E-mail to remove: <b>' . $matches[1] . '</b><br />';$counter++;
- }
- }
- echo 'Number of emails: <b>' . $counter . '</b>';
- echo '<br />------- END PARSING -------<br /> <br />';
- }
- }
- // You could use this anywhere...
- $pop3Handler = Pop3Worker::getInstance();
- ?>
Add Comment
Please, Sign In to add comment