За последние 24 часа нас посетили 17536 программистов и 1310 роботов. Сейчас ищут 1148 программистов ...

Класс для работы с почтой по imap

Тема в разделе "Работа с почтой", создана пользователем unmanner, 2 мар 2013.

  1. unmanner

    unmanner Активный пользователь

    С нами с:
    23 янв 2011
    Сообщения:
    105
    Симпатии:
    0
    Здравствуйте!

    Несколько дней искал в интернете хороший класс для работы с почтой по imap, видел много классов, но все они были сырые, где-то нет перекодировки, где-то нет возможности работать с вложениями, и тп.
    В результате сего практически написал свой (из кусков) и теперь осталось поправить пару ошибок, которые вылетают при выполнении скрипта, если вы мне это положите сделать, то я буду Вам примного благодарен.
    Сам я не силен в ООП, отсюда и непонимание таких ошибок. Прошу внимательно посмотреть на свойство $p. Мне кажется что все дело в нём.

    Мой скрипт:
    Код (Text):
    1. <?php
    2. header('Content-Type: text/html; charset=utf-8');
    3. require_once 'htmltotext.php';
    4. ?>
    5. <html>
    6.   <head>
    7.     <meta charset="utf-8">
    8.     <title>--</title>
    9.     <style>
    10.             body {
    11.                 font: 8pt sans-serif;
    12.             }
    13.     </style>
    14.     <script src="../js/jquery-1.9.1.js"></script>
    15.     <script src="http://code.jquery.com/ui/1.10.0/jquery-ui.js"></script>
    16. </head>
    17. <body>
    18. <?php
    19.  
    20. $box = new MailClient;
    21.  
    22. $box->user("mail@bk.ru","pass");
    23. $box->server("imap.mail.ru","993","imap/ssl/novalidate-cert");
    24.  
    25. //$box->user("mail@gmail.com","pass");
    26. //$box->server("imap.gmail.com","993","imap/ssl/novalidate-cert");
    27.  
    28. $box->open();
    29. $totalCount = $box->count();
    30. echo 'Total '.$totalCount .' messages<br>';
    31.  
    32. for ($i=$totalCount; $i>0; $i--) {
    33.     $box->getmsg($i);
    34.     //$plainmsg= quoted_printable_encode($plainmsg, true);
    35.     //$htmlmsg= iconv('KOI8-R','utf-8',$htmlmsg);
    36.     //$body = quoted_printable_encode_2($body);
    37.     echo 'Text: '.$box->getText().'<br><br>';
    38.     //echo 'HTML: '.convert_html_to_text_rm($box->getHtml()).'<br><br>';
    39. }
    40.  
    41. //echo $plainmsg;
    42.  
    43.  
    44. class MailClient {
    45.     public $server;
    46.     public $port = 110;
    47.     public $type = "pop3";
    48.     public $count;
    49.     public $msg;
    50.     private $user;
    51.     private $pass;
    52.     private $box;
    53.     public  $charset;
    54.     public  $htmlmsg;
    55.     public  $plainmsg;
    56.     public  $attachments;
    57.     public  $p;
    58.    
    59.     public function __construct() {
    60.         if (!extension_loaded("imap"))
    61.         {
    62.             if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {
    63.                 dl('php_imap.dll');
    64.             } else {
    65.                 dl('php_imap.so');
    66.             }
    67.             if (!extension_loaded("imap")) {
    68.                 $this->error("Could not load required extension... Please install extansion.");
    69.             }
    70.         }
    71.     }
    72.     public function getHtml() {
    73.         return $this->htmlmsg;
    74.     }
    75.     public function getText() {
    76.         return $this->plainmsg;
    77.     }
    78.     public function getcharset() {
    79.         return $this->charset;
    80.     }
    81.     public function user($user, $pass) {
    82.         $this->user = $user;
    83.         $this->pass = $pass;
    84.     }
    85.     public function server($server, $port, $type) {
    86.         $this->server = $server;
    87.         $this->port = $port;
    88.         $this->type = $type;
    89.     }
    90.     public function open() {
    91.         $this->box = @imap_open("{".$this->server.":".$this->port."/".$this->type."}INBOX", $this->user, $this->pass);
    92.         if($this->box) {
    93.             return true;
    94.         }
    95.         if (imap_last_error()) {
    96.             $this->error(imap_last_error());
    97.         } else {
    98.             $this->error("Couldn't open stream  ".$this->server.":".$this->port."...");
    99.         }
    100.         return true;
    101.     }
    102.     public function select($id) {
    103.         $this->msg = $id;
    104.         return imap_headerinfo($this->box, $id);
    105.     }
    106.  
    107.     public function count() {
    108.         $this->count = imap_num_msg($this->box);
    109.         return $this->count;
    110.     }
    111.     public function getmsg($id) {
    112.         $this->msg = $id;
    113.        
    114.         $this->htmlmsg = $this->plainmsg = $this->charset = '';
    115.        
    116.         // input $mbox = IMAP stream, $mid = message id
    117.         // output all the following:
    118.         $this->attachments = array();
    119.  
    120.         // HEADER
    121.         $h = imap_header($this->box, $this->msg);
    122.         // add code here to get date, from, to, cc, subject...
    123.  
    124.         // BODY
    125.         $s = imap_fetchstructure($this->box, $this->msg);
    126.         if (!$s->parts) {  // simple
    127.             $this->p      = $s;
    128.             $this->partno = 0;
    129.             $this->getpart();  // pass 0 as part-number
    130.         } else {  // multipart: cycle through each part
    131.             foreach ($s->parts as $partno0=>$p) {
    132.                 $this->p      = $p;
    133.                 $this->partno = $partno0+1;
    134.                 $this->getpart();
    135.             }
    136.         }
    137.     }
    138.     public function getpart() {
    139.         // $partno = '1', '2', '2.1', '2.1.3', etc for multipart, 0 if simple
    140.         // DECODE DATA
    141.         $p = $this->p;
    142.         $data = ($this->partno)?
    143.             imap_fetchbody($this->box, $this->msg, $this->partno):  // multipart
    144.             imap_body($this->box, $this->msg);  // simple
    145.         // Any part may be encoded, even plain text messages, so check everything.
    146.         if ($p->encoding==4)
    147.             $data = quoted_printable_decode($data);
    148.         elseif ($p->encoding==3)
    149.             $data = base64_decode($data);
    150.  
    151.         // PARAMETERS
    152.         // get all parameters, like charset, filenames of attachments, etc.
    153.         $params = array();
    154.         if ($p->parameters)
    155.             foreach ($p->parameters as $x)
    156.                 $params[strtolower($x->attribute)] = $x->value;
    157.         if ($p->dparameters)
    158.             foreach ($p->dparameters as $x)
    159.                 $params[strtolower($x->attribute)] = $x->value;
    160.  
    161.         // ATTACHMENT
    162.         // Any part with a filename is an attachment,
    163.         // so an attached text file (type 0) is not mistaken as the message.
    164.         if ($params['filename'] || $params['name']) {
    165.             // filename may be given as 'Filename' or 'Name' or both
    166.             $filename = ($params['filename'])? $params['filename'] : $params['name'];
    167.             // filename may be encoded, so see imap_mime_header_decode()
    168.             $this->attachments[$filename] = $data;  // this is a problem if two files have same name
    169.         }
    170.  
    171.         // TEXT
    172.         if ($p->type==0 && $data) {
    173.             // Messages may be split in different parts because of inline attachments,
    174.             // so append parts together with blank row.
    175.             if (strtolower($p->subtype)=='plain')
    176.                 $this->plainmsg .= trim($data) ."\n\n";
    177.             else
    178.                 $this->htmlmsg .= $data ."<br><br>";
    179.             $this->charset = $params['charset'];  // assume all parts are same charset
    180.         }
    181.  
    182.         // EMBEDDED MESSAGE
    183.         // Many bounce notifications embed the original message as type 2,
    184.         // but AOL uses type 1 (multipart), which is not handled here.
    185.         // There are no PHP functions to parse embedded messages,
    186.         // so this just appends the raw source to the main message.
    187.         elseif ($p->type==2 && $data) {
    188.             $this->plainmsg .= $data."\n\n";
    189.         }
    190.  
    191.         // SUBPART RECURSION
    192.         if ($p->parts) {
    193.             foreach ($p->parts as $partno0=>$p2) {
    194.                 $this->p      = $p2;
    195.                 $this->partno = $this->partno.'.'.($partno0+1);
    196.                 $this->getpart();  // 1.2, 1.2.1, etc.
    197.             }
    198.         }
    199.     }
    200.     public function delete($id) {
    201.         imap_delete($this->box, $id);
    202.         imap_expunge($this->box);
    203.     }
    204.     public function error($error) {
    205.         echo $error;
    206.         exit();
    207.     }
    208.     public function close() {
    209.         imap_close($this->box);
    210.     }
    211. }
    212.  
    213. ?>
    214.  
    215. </body>
    216. </html>
    Ошибки за 1 выполнение скрипта:
    Код (Text):
    1. 2013/03/02 09:34:02 [error] 12481#0: *3 FastCGI sent in stderr: "PHP message: PHP Notice:  Undefined property: stdClass::$dparameters in /proj/my_service/server/ssl-server/testmail.php on line 157
    2. PHP message: PHP Notice:  Undefined index: filename in /proj/my_service/server/ssl-server/testmail.php on line 164
    3. PHP message: PHP Notice:  Undefined index: name in /proj/my_service/server/ssl-server/testmail.php on line 164
    4. PHP message: PHP Notice:  Undefined property: stdClass::$parts in /proj/my_service/server/ssl-server/testmail.php on line 192
    5. PHP message: PHP Notice:  Undefined property: stdClass::$dparameters in /proj/my_service/server/ssl-server/testmail.php on line 157
    6. PHP message: PHP Notice:  Undefined index: filename in /proj/my_service/server/ssl-server/testmail.php on line 164
    7. PHP message: PHP Notice:  Undefined index: name in /proj/my_service/server/ssl-server/testmail.php on line 164
    8. PHP message: PHP Notice:  Undefined property: stdClass::$parts in /proj/my_service/server/ssl-server/testmail.php on line 192
    9. PHP message: PHP Notice:  Undefined property: stdClass::$dparameters in /proj/my_service/server/ssl-server/testmail.php on line 157
    10. PHP message: PHP Notice:  Undefined index: filename in /proj/my_service/server/ssl-server/testmail.php on line 164
    11. PHP message: PHP Notice:  Undefined index: name in /proj/my_service/server/ssl-server/testmail.php on line 164
    12. PHP message: PHP Notice:  Undefined property: stdClass::$parts in /proj/my_service/server/ssl-server/testmail.php on line 192
    13. PHP message: PHP Notice:  Undefined property: stdClass::$dparameters in /proj/my_service/server/ssl-server/testmail.php on line 157
    14. PHP message: PHP Notice:  Undefined index: filename in /proj/my_service/server/ssl-server/testmail.php on line 164
    15. PHP message: PHP Notice:  Undefined index: name in /proj/my_service/server/ssl-server/testmail.php on line 164
    16. PHP message: PHP Notice:  Undefined property: stdClass::$parts in /proj/my_service/server/ssl-server/testmail.php on line 19
    17. 2013/03/02 09:34:02 [error] 12481#0: *3 FastCGI sent in stderr: "164
    18. PHP message: PHP Notice:  Undefined index: name in /proj/my_service/server/ssl-server/testmail.php on line 164
    19. PHP message: PHP Notice:  Undefined property: stdClass::$parts in /proj/my_service/server/ssl-server/testmail.php on line 192
    20. PHP message: PHP Notice:  Undefined property: stdClass::$dparameters in /proj/my_service/server/ssl-server/testmail.php on line 157
    21. PHP message: PHP Notice:  Undefined index: filename in /proj/my_service/server/ssl-server/testmail.php on line 164
    22. PHP message: PHP Notice:  Undefined index: name in /proj/my_service/server/ssl-server/testmail.php on line 164
    23. PHP message: PHP Notice:  Undefined property: stdClass::$parts in /proj/my_service/server/ssl-server/testmail.php on line 192
    24. PHP message: PHP Notice:  Undefined property: stdClass::$dparameters in /proj/my_service/server/ssl-server/testmail.php on line 157
    25. PHP message: PHP Notice:  Undefined index: filename in /proj/my_service/server/ssl-server/testmail.php on line 164
    26. PHP message: PHP Notice:  Undefined index: name in /proj/my_service/server/ssl-server/testmail.php on line 164
    27. PHP message: PHP Notice:  Undefined property: stdClass::$parts in /proj/my_service/server/ssl-server/testmail.php on line 192
    28. PHP message: PHP Notice:  Undefined property: stdClass::$dparameters in /proj/my_service/server/ssl-server/testmail.php on line 157
    29. PHP message: PHP Notice:  Undefined index: filename in /proj/my_service/server/ssl-server/testmail.php on line 164
    30. PHP message: PHP Notice:  Undefined index: name in /proj/my_service/server/ssl-server/testmail.php on line 164
    31. PHP message: PHP Notice:  Undefined property: stdClass::$parts in /proj/my_service/server/ssl-server/testmail.php on line 192
    32. PHP message: PHP Notice:  Undefined property: stdClass::$dparameters in /proj/my_service/server/ssl-server/testmail.php on line 157
    33. PHP message: PHP Notice:  Undefined index: filename in /proj/my_service/server/ssl-server/testmail.php on lin
    34. 2013/03/02 09:34:04 [error] 12481#0: *3 FastCGI sent in stderr: "e: PHP Notice:  Undefined property: stdClass::$parts in /proj/my_service/server/ssl-server/testmail.php on line 192
    35. PHP message: PHP Notice:  Undefined property: stdClass::$dparameters in /proj/my_service/server/ssl-server/testmail.php on line 157
    36. PHP message: PHP Notice:  Undefined index: filename in /proj/my_service/server/ssl-server/testmail.php on line 164
    37. PHP message: PHP Notice:  Undefined index: name in /proj/my_service/server/ssl-server/testmail.php on line 164
    38. PHP message: PHP Notice:  Undefined property: stdClass::$parts in /proj/my_service/server/ssl-server/testmail.php on line 192
    39. PHP message: PHP Notice:  Undefined property: stdClass::$dparameters in /proj/my_service/server/ssl-server/testmail.php on line 157
    40. PHP message: PHP Notice:  Undefined index: filename in /proj/my_service/server/ssl-server/testmail.php on line 164
    41. PHP message: PHP Notice:  Undefined index: name in /proj/my_service/server/ssl-server/testmail.php on line 164
    42. PHP message: PHP Notice:  Undefined property: stdClass::$parts in /proj/my_service/server/ssl-server/testmail.php on line 192
    43. PHP message: PHP Notice:  Undefined property: stdClass::$dparameters in /proj/my_service/server/ssl-server/testmail.php on line 157
    44. PHP message: PHP Notice:  Undefined index: filename in /proj/my_service/server/ssl-server/testmail.php on line 164
    45. PHP message: PHP Notice:  Undefined index: name in /proj/my_service/server/ssl-server/testmail.php on line 164
    46. PHP message: PHP Notice:  Undefined property: stdClass::$parts in /proj/my_service/server/ssl-server/testmail.php on line 192
    47. PHP message: PHP Notice:  Undefined property: stdClass::$dparameters in /proj/my_service/server/ssl-server/testmail.php on line 157
    48. PHP message: PHP Notice:  Undefined index: filename in /proj/my_service/server/ssl-server/testmail.php on line 164
    49. PHP message: PHP Notice:  Undefined index: name in /proj/my_service/server/ssl-server/testmail.php on line 164
    50. PHP mess
    51. 2013/03/02 09:34:04 [error] 12481#0: *3 upstream sent too big header while reading response header from upstream, client: xxx.xxx.xxx.xxx, server: xxx.xxx.xxx.xxx, request: "GET /testmail.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "xxx.ru"
    Спасибо! :)