Guest User

Untitled

a guest
Mar 4th, 2018
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. set_time_limit(3000);
  2.  
  3. $hostname = '{imap.ukr.net:993/imap/ssl/novalidate-cert}INBOX';
  4. $username = ''; # например somebody@gmail.com
  5. $password = '';
  6.  
  7. $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
  8.  
  9. $emails = imap_search($inbox,'ALL');
  10. $max_emails = 16;
  11.  
  12. if($emails) {
  13.  
  14. $count = 1;
  15. rsort($emails);
  16. foreach($emails as $email_number)
  17. {
  18.  
  19. /* get information specific to this email */
  20. $overview = imap_fetch_overview($inbox,$email_number,0);
  21.  
  22. /* get mail message */
  23. $message = imap_fetchbody($inbox,$email_number,2);
  24.  
  25. /* get mail structure */
  26. $structure = imap_fetchstructure($inbox, $email_number);
  27.  
  28. $attachments = array();
  29.  
  30. /* if any attachments found... */
  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. {
  44. foreach($structure->parts[$i]->dparameters as $object)
  45. {
  46. if(strtolower($object->attribute) == 'filename')
  47. {
  48. $attachments[$i]['is_attachment'] = true;
  49. $attachments[$i]['filename'] = $object->value;
  50. }
  51. }
  52. }
  53.  
  54. if($structure->parts[$i]->ifparameters)
  55. {
  56. foreach($structure->parts[$i]->parameters as $object)
  57. {
  58. if(strtolower($object->attribute) == 'name')
  59. {
  60. $attachments[$i]['is_attachment'] = true;
  61. $attachments[$i]['name'] = $object->value;
  62. }
  63. }
  64. }
  65.  
  66. if($attachments[$i]['is_attachment'])
  67. {
  68. $attachments[$i]['attachment'] = imap_fetchbody($inbox, $email_number, $i+1);
  69.  
  70. /* 4 = QUOTED-PRINTABLE encoding */
  71. if($structure->parts[$i]->encoding == 3)
  72.  
  73. {
  74. $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
  75. }
  76. /* 3 = BASE64 encoding */
  77. elseif($structure->parts[$i]->encoding == 4)
  78. {
  79. $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
  80. }
  81. }
  82.  
  83.  
  84. }
  85. }
  86.  
  87. /* iterate through each attachment and save it */
  88. foreach($attachments as $attachment)
  89. {
  90. if($attachment['is_attachment'] == 1)
  91. {
  92. $filename = $attachment['name'];
  93. if(empty($filename)) $filename = $attachment['filename'];
  94. if(empty($filename)) $filename = time() . "dat";
  95.  
  96. /* prefix the email number to the filename in case two emails
  97. * have the attachment with the same file name.
  98. */
  99. $fp = fopen('/mnt/c/ubuntu/pochta/'. $filename, 'w+');
  100. fwrite($fp, $attachment['attachment']);
  101. fclose($fp);
  102. }
  103.  
  104. }
  105.  
  106. if($count++ >= $max_emails) break;
  107. }
  108.  
  109. }
  110.  
  111. /* close the connection */
  112. imap_close($inbox);
  113.  
  114. echo "Donen";
  115.  
  116. ?>
Add Comment
Please, Sign In to add comment