Guest User

Untitled

a guest
Jan 13th, 2019
619
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.09 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. ?>
  117.  
  118. /**
  119. * Utility Class
  120. */
  121. class IMAP
  122. {
  123. /**
  124. *
  125. * =?x-unknown?B?
  126. * =?iso-8859-1?Q?
  127. * =?windows-1252?B?
  128. *
  129. * @param string $stringQP
  130. * @param string $base (optional) charset (IANA, lowercase)
  131. * @return string UTF-8
  132. */
  133. public static function decodeToUTF8($stringQP, $base = 'windows-1252')
  134. {
  135. $pairs = array(
  136. '?x-unknown?' => "?$base?"
  137. );
  138. $stringQP = strtr($stringQP, $pairs);
  139. return imap_utf8($stringQP);
  140. }
  141. }
  142. class IMAPMailbox implements IteratorAggregate, Countable
  143. {
  144. private $stream;
  145. public function __construct($hostname, $username, $password)
  146. {
  147. $stream = imap_open($hostname, $username, $password);
  148. if (FALSE === $stream) {
  149. throw new Exception('Connect failed: ' . imap_last_error());
  150. }
  151. $this->stream = $stream;
  152. }
  153. public function getStream()
  154. {
  155. return $this->stream;
  156. }
  157. /**
  158. * @return stdClass
  159. */
  160. public function check()
  161. {
  162. $info = imap_check($this->stream);
  163. if (FALSE === $info) {
  164. throw new Exception('Check failed: ' . imap_last_error());
  165. }
  166. return $info;
  167. }
  168. /**
  169. * @param string $criteria
  170. * @param int $options
  171. * @param int $charset
  172. * @return IMAPMessage[]
  173. * @throws Exception
  174. */
  175. public function search($criteria, $options = NULL, $charset = NULL)
  176. {
  177. $emails = imap_search($this->stream, $criteria, $options, $charset);
  178. if (FALSE === $emails) {
  179. throw new Exception('Search failed: ' . imap_last_error());
  180. }
  181. foreach ($emails as &$email) {
  182. $email = $this->getMessageByNumber($email);
  183. }
  184. return $emails;
  185. }
  186. /**
  187. * @param int $number
  188. * @return IMAPMessage
  189. */
  190. public function getMessageByNumber($number)
  191. {
  192. return new IMAPMessage($this, $number);
  193. }
  194. public function getOverview($sequence = NULL)
  195. {
  196. if (NULL === $sequence) {
  197. $sequence = sprintf('1:%d', count($this));
  198. }
  199. return new IMAPOverview($this, $sequence);
  200. }
  201. /**
  202. * Retrieve an external iterator
  203. * @link http://php.net/manual/en/iteratoraggregate.getiterator.php
  204. * @return Traversable An instance of an object implementing Iterator or
  205. * Traversable
  206. */
  207. public function getIterator()
  208. {
  209. return $this->getOverview()->getIterator();
  210. }
  211. /**
  212. * @return int
  213. */
  214. public function count()
  215. {
  216. return $this->check()->Nmsgs;
  217. }
  218. }
  219. class IMAPOverview extends ArrayObject
  220. {
  221. private $mailbox;
  222. public function __construct(IMAPMailbox $mailbox, $sequence)
  223. {
  224. $result = imap_fetch_overview($mailbox->getStream(), $sequence);
  225. if (FALSE === $result) {
  226. throw new Exception('Overview failed: ' . imap_last_error());
  227. }
  228. $this->mailbox = $mailbox;
  229. foreach ($result as $overview)
  230. {
  231. if (!isset($overview->subject)) {
  232. $overview->subject = '';
  233. } else {
  234. $overview->subject = IMAP::decodeToUTF8($overview->subject);
  235. }
  236. }
  237. parent::__construct($result);
  238. }
  239. /**
  240. * @return IMAPMailbox
  241. */
  242. public function getMailbox()
  243. {
  244. return $this->mailbox;
  245. }
  246. }
  247. class IMAPMessage
  248. {
  249. private $mailbox;
  250. private $number;
  251. private $stream;
  252. public function __construct(IMAPMailbox $mailbox, $number)
  253. {
  254. $this->mailbox = $mailbox;
  255. $this->number = $number;
  256. $this->stream = $mailbox->getStream();
  257. }
  258. public function getNumber()
  259. {
  260. return $this->number;
  261. }
  262. /**
  263. * @param int $number
  264. * @return string
  265. */
  266. public function fetchBody($number)
  267. {
  268. return imap_fetchbody($this->stream, $this->number, $number);
  269. }
  270. /**
  271. * @return stdClass
  272. * @throws Exception
  273. */
  274. public function fetchOverview()
  275. {
  276. $result = imap_fetch_overview($this->stream, $this->number);
  277. if (FALSE === $result) {
  278. throw new Exception('FetchOverview failed: ' . imap_last_error());
  279. }
  280. list($result) = $result;
  281. foreach ($result as &$prop) {
  282. $prop = imap_utf8($prop);
  283. }
  284. return $result;
  285. }
  286. public function fetchStructure()
  287. {
  288. $structure = imap_fetchstructure($this->stream, $this->number);
  289. if (FALSE === $structure) {
  290. throw new Exception('FetchStructure failed: ' . imap_last_error());
  291. }
  292. return $structure;
  293. }
  294. /**
  295. * @return IMAPAttachments
  296. */
  297. public function getAttachments()
  298. {
  299. return new IMAPAttachments($this);
  300. }
  301. public function __toString()
  302. {
  303. return (string)$this->number;
  304. }
  305. }
  306. class IMAPAttachment
  307. {
  308. private $attachment;
  309. private $message;
  310. public function __construct(IMAPMessage $message, $attachment)
  311. {
  312. $this->message = $message;
  313. $this->attachment = $attachment;
  314. }
  315. /**
  316. * @return string;
  317. */
  318. public function getBody()
  319. {
  320. return $this->message->fetchBody($this->attachment->number);
  321. }
  322. /**
  323. * @return int
  324. */
  325. public function getSize()
  326. {
  327. return (int)$this->attachment->bytes;
  328. }
  329. /**
  330. * @return string
  331. */
  332. public function getExtension()
  333. {
  334. return pathinfo($this->getFilename(), PATHINFO_EXTENSION);
  335. }
  336. public function getFilename()
  337. {
  338. $filename = $this->attachment->filename;
  339. NULL === $filename && $filename = $this->attachment->name;
  340. return $filename;
  341. }
  342. public function __toString()
  343. {
  344. $encoding = $this->attachment->encoding;
  345. switch ($encoding) {
  346. case 0: // 7BIT
  347. case 1: // 8BIT
  348. case 2: // BINARY
  349. return $this->getBody();
  350. case 3: // BASE-64
  351. return base64_decode($this->getBody());
  352. case 4: // QUOTED-PRINTABLE
  353. return imap_qprint($this->getBody());
  354. }
  355. throw new Exception(sprintf('Encoding failed: Unknown encoding %s (5: OTHER).', $encoding));
  356. }
  357. }
  358. class IMAPAttachments extends ArrayObject
  359. {
  360. private $message;
  361. public function __construct(IMAPMessage $message)
  362. {
  363. $array = $this->setMessage($message);
  364. parent::__construct($array);
  365. }
  366. private function setMessage(IMAPMessage $message)
  367. {
  368. $this->message = $message;
  369. return $this->parseStructure($message->fetchStructure());
  370. }
  371. private function parseStructure($structure)
  372. {
  373. $attachments = array();
  374. if (!isset($structure->parts)) {
  375. return $attachments;
  376. }
  377. foreach ($structure->parts as $index => $part)
  378. {
  379. if (!$part->ifdisposition) continue;
  380. $attachment = new stdClass;
  381. $attachment->isAttachment = FALSE;
  382. $attachment->number = $index + 1;
  383. $attachment->bytes = $part->bytes;
  384. $attachment->encoding = $part->encoding;
  385. $attachment->filename = NULL;
  386. $attachment->name = NULL;
  387. $part->ifdparameters
  388. && ($attachment->filename = $this->getAttribute($part->dparameters, 'filename'))
  389. && $attachment->isAttachment = TRUE;
  390. $part->ifparameters
  391. && ($attachment->name = $this->getAttribute($part->parameters, 'name'))
  392. && $attachment->isAttachment = TRUE;
  393. $attachment->isAttachment
  394. && $attachments[] = new IMAPAttachment($this->message, $attachment);
  395. }
  396. return $attachments;
  397. }
  398. private function getAttribute($params, $name)
  399. {
  400. foreach ($params as $object)
  401. {
  402. if ($object->attribute == $name) {
  403. return IMAP::decodeToUTF8($object->value);
  404. }
  405. }
  406. return NULL;
  407. }
  408. }
  409.  
  410. $savedir = __DIR__ . '/imap-dump/';// Указываем директорию куда хотим сохранять файлы
  411.  
  412. $hostname = '{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX';//
  413. $username = 'somebody@gmail.com';
  414. $password = 'XXXXXXXX';
  415. $inbox = new IMAPMailbox($hostname, $username, $password);
  416.  
  417. $emails = $inbox->search('ALL');
  418. if ($emails) {
  419. rsort($emails);
  420. foreach ($emails as $email) {
  421. //.........
  422. //.........
  423. foreach ($email->getAttachments() as $attachment) {
  424. $savepath = $savedir . $attachment->getFilename();
  425. file_put_contents($savepath, $attachment);
  426. }
  427. }
  428. }
Add Comment
Please, Sign In to add comment