Advertisement
Guest User

Untitled

a guest
Oct 29th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. $server = '{imap.hostingname.ru}';
  2. $username = 'test@sitename.ru';
  3. $password = 'password';
  4.  
  5. function getFileExtension($fileName) {
  6. $parts = explode(".",$fileName);
  7. return $parts[count($parts)-1];
  8. }
  9.  
  10. $imap = imap_open($server, $username, $password) or die("imap connection error");
  11. $message_count = imap_num_msg($imap);
  12.  
  13. for ($m = 1; $m <= $message_count; ++$m) {
  14. $header = imap_header($imap, $m);
  15. $email[$m]['from'] = $header->from[0]->mailbox.'@'.$header->from[0]->host;
  16. $email[$m]['fromaddress'] = $header->from[0]->personal;
  17. $email[$m]['to'] = $header->to[0]->mailbox;
  18. $email[$m]['subject'] = $header->subject;
  19. $email[$m]['message_id'] = $header->message_id;
  20. $email[$m]['date'] = $header->udate;
  21.  
  22. $from = $email[$m]['fromaddress'];
  23. $from_email = $email[$m]['from'];
  24. $to = $email[$m]['to'];
  25. $subject = $email[$m]['subject'];
  26.  
  27. $structure = imap_fetchstructure($imap, $m);
  28.  
  29. $attachments = array();
  30.  
  31. if(isset($structure->parts) && count($structure->parts)) {
  32.  
  33. for($i = 0; $i < count($structure->parts); $i++) {
  34.  
  35. $attachments[$i] = array(
  36. 'is_attachment' => false,
  37. 'filename' => '',
  38. 'name' => '',
  39. 'attachment' => ''
  40. );
  41.  
  42. if($structure->parts[$i]->ifdparameters) {
  43. foreach($structure->parts[$i]->dparameters as $object) {
  44. if(strtolower($object->attribute) == 'filename') {
  45. $attachments[$i]['is_attachment'] = true;
  46. $attachments[$i]['filename'] = $object->value;
  47. }
  48. }
  49. }
  50.  
  51. if($structure->parts[$i]->ifparameters) {
  52. foreach($structure->parts[$i]->parameters as $object) {
  53. if(strtolower($object->attribute) == 'name') {
  54. $attachments[$i]['is_attachment'] = true;
  55. $attachments[$i]['name'] = $object->value;
  56. }
  57. }
  58. }
  59.  
  60. if($attachments[$i]['is_attachment']) {
  61. $attachments[$i]['attachment'] = imap_fetchbody($imap, $m, $i+1);
  62. if($structure->parts[$i]->encoding == 3) {
  63. $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
  64. }
  65. elseif($structure->parts[$i]->encoding == 4) {
  66. $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
  67. }
  68. }
  69. }
  70. }
  71.  
  72. foreach ($attachments as $key => $attachment) {
  73. $name = $attachment['name'];
  74. $contents = $attachment['attachment'];
  75.  
  76. $resp = imap_utf8(trim($name));
  77.  
  78. if(preg_match("/=?/", $resp))
  79. $resp = iconv_mime_decode($name, ICONV_MIME_DECODE_CONTINUE_ON_ERROR, "ISO-8859-15");
  80.  
  81. if(json_encode($resp) == 'null')
  82. $resp = utf8_encode($resp);
  83.  
  84. file_put_contents($resp, $contents);
  85. echo $resp;
  86. }
  87.  
  88. }
  89.  
  90. imap_close($imap);
  91.  
  92. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement