Çäðàâñòâóéòå!
Âîò ñåãîäíÿ âçÿëñÿ çà íàïèñàíèå êëàññà ðàáîòû ñ ïî÷òîâûì ÿùèêîì. È íàïèñàë!
Ïðåäñòàâëÿþ åãî âàøåìó âíèìàíèþ:
PHP |
/** * name: popMail Class * reliased: 18 march 2009 * author: KiriK * license: GPL (General Public License) **/ Class popMail { private $thread; private $total_msgs; private $msgs_list; private $msg_txt;
public function login($address, $port, $login, $password) { $this->thread = fsockopen($address, $port, $errno, $errstr);
if(!$this->thread) exit('fsockopen() failed: '.$errstr);
$this->getAnswer();
$this->sendRequest('USER '.$login); $this->getAnswer();
$this->sendRequest('PASS '.$password); $this->getAnswer(); }
public function getAnswer($top=false) { $read = fgets($this->thread);
if($top) { $line = $read; while(!ereg("^\.\r\n", $line)) { $line = fgets($this->thread); $read .= $line; } } if($read[0] != '+') { if(empty($read)) exit('Unknown error'); else exit('POP3 failed: '.$read); } return $read; }
public function sendRequest($msg) { fwrite($this->thread, $msg."\r\n"); }
public function getInfo() { $this->sendRequest('STAT'); preg_match('/(\d+)\s+(\d+)/s', $this->getAnswer(), $out);
$this->total_msgs = $out[1];
return array('total_messages' => $out[1], 'total_size' => $out[2]); }
public function getMessagesList() { if(empty($this->total_msgs)) $this->getInfo();
if($this->total_msgs > 0) { for($i = 1; $i <= $this->total_msgs; $i++) { $this->sendRequest('LIST '.$i); preg_match('/(\d+)\s+(\d+)/s', $this->getAnswer(), $out); $this->msgs_list[$out[1]] = array('msg_id' => $out[1], 'msg_size' => $out[0]); } } else { return false; }
return $this->msgs_list; }
public function getMessage($id) { $this->sendRequest('RETR '.$id); $this->msg_txt = $this->getAnswer(true);
return $this->msg_txt; }
public function deleteMessage($id) { $this->sendRequest('DELE '.$id); $this->getAnswer(); }
public function resetMarks() { $this->sendRequest('RSET'); $this->getAnswer(); }
public function getHeader($name) { if(empty($this->msg_txt)) return false;
preg_match('/'.preg_quote($name, '/').':\s+(.+)/i', $this->msg_txt, $out);
return $out[1]; }
function __destruct() { $this->sendRequest('QUIT'); $this->getAnswer(); } } |
Íåìíîãî î ðàáîòå:
PHP |
// Èíèöèàëèçèðóåì êëàññ $pop = New popMail;
/* * Êîííåêòèìñÿ ê ñåðâåðó, è àâòîðèçóåìñÿ * pop.mail.ru - èìÿ âàøåãî POP3 ñåðâåðà * 110 - ïîðò (÷àùå âñåãî 110) * login - ëîãèí * password - ïàðîëü */ $pop->login('pop.mail.ru', 110, 'login', 'password');
/* * Äëÿ ïîëó÷åíèÿ èíôîðìàöèè î ñîîáùåíèÿõ â ïàïêå "Âõîäÿùèå": * ðåçóëüòàò - ìàññèâ * array( * 'total_messages' => X, - îáùåå êîëè÷åñòâî ñîîáùåíèé * 'total_size' => Y - ðàçìåð âñåõ ñîîáùåíèé â áàéòàõ * ) */ $array = $pop->getInfo();
/* * Äëÿ ïîëó÷åíèÿ âñåõ ñîîáùåíèé ïàïêå "Âõîäÿùèå": * ðåçóëüòàò - ìàññèâ * array( * X => array( * 'msg_id' => X, - id ñîîáùåíèÿ * 'msg_size' => Y - ðàçìåð ñîîáùåíèÿ â áàéòàõ * ), * ... * ) */ $array = $pop->getMessagesList();
/* * Äëÿ ïîëó÷åíèÿ ñîîáùåíèÿ ñ îïðåäåëåííûì id (ïåðåäàåòñÿ â ìåòîä $id): * ðåçóëüòàò - ìàññèâ * array( * 'headers' => X, - çàãîëîâêè ñîîáùåíèÿ * 'body' => Y - òåëî ñîîáùåíèÿ * ) */ $array = $pop->getMessage($id);
/* * Äëÿ ïîëó÷åíèÿ çàãîëîâêà ñ îïðåäåëåííûì èìåíåì äëÿ äàííîãî ñîîáùåíèÿ (ïåðåäàåòñÿ â ìåòîä $name): * ðåçóëüòàò - ñòðîêà ñ ñîäåðæàíèåì çàãîëîâêà * I: åñëè ñîîáùåíèå íå áûëî ïîëó÷åíî çàðàíåå - âîçâðàùàåòñÿ false */ $string = $pop->getHeader($name);
/* * Äëÿ ïîìåòêè ñîîáùåíèÿ îïðåäåëåííûì id ê óäàëåíèþ (ïåðåäàåòñÿ â ìåòîä $id): * I: ñîîáùåíèÿ óäàëÿþòñÿ òîëüêî ïî âûçîâó äåñòðóêòîðà êëàññà, òîåñòü ïî çàâåðøåíèþ POP ñåññèè */ $pop->deleteMessage($id);
/* * Äëÿ ñáðîñà âñåõ îòìåòîê î óäàëåíèè ñîîáùåíèé: * I: ìåòêè ñáðàñûâàþòñÿ äëÿ ÂÑÅÕ ñîîáùåíèé */ $pop->resetMarks(); |
Äîðàáîòêà êëàññà ïðèâåòñòâóåòñÿ!
Äëÿ ìîèõ íóæä åãî âïîëíå õâàòàåò (ÿ çàáèðàþ îòëóïû î âîçâðàùåííûõ ïèñüìàõ î ðåãèñòðàöèè, ÷òîáû óäàëÿòü þçåðîâ ñ ëåâûìè àäðåñàìè èç áàçû).
ÇÛ ïðèêðåïèë ôàéë ïîëíûé ÷òîá íè êîïèïàñòèòü.
Ñïóñòÿ 7 ìèíóò, 14 ñåêóíä (19.03.2009 - 08:04) kirik íàïèñàë(à):
Sylex, ïðîñòî íóæåí áûë íå ñëîæíûé è ëåãêèé. Äà è ñâîÿ ðóáàõà áëèæå ê òåëó, êàê ãîâîðèòñÿ
Òóò âèäèøü, íå äëÿ ðàçáîðà ïî÷òû, à òîëüêî ëèøü äëÿ ïàðñèíãà email àäðåñîâ.
Ñïóñòÿ 49 ìèíóò, 20 ñåêóíä (19.03.2009 - 08:53) Sylex íàïèñàë(à):
Öèòàòà (kirik @ 19.03.2009 - 11:04) |
Äà è ñâîÿ ðóáàõà áëèæå ê òåëó, êàê ãîâîðèòñÿ |
ýòî òî÷íî