Guest User

Untitled

a guest
Nov 18th, 2017
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. <?php
  2.  
  3. /* connect to gmail */
  4. $hostname = '{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX';
  5. $username = '##YOUR EMAIL ID';
  6. $password = '##YOUR PASSWORD';
  7.  
  8. /* try to connect */
  9. $inbox = imap_open($hostname, $username, $password) or die('Cannot connect to Gmail: ' . imap_last_error());
  10. $emails = imap_search($inbox, 'SUBJECT "##EMAIL SUBJECT"');
  11. foreach ($emails as $email_msg) {
  12. $structure = imap_fetchstructure($inbox, $email_msg);
  13. $msg_info = imap_headerinfo($inbox, $email_msg);
  14. if (isset($structure->parts) && empty($structure->parts) == FALSE) {
  15. foreach ($structure->parts as $key => $part_info) {
  16.  
  17. if (isset($part_info->disposition) && $part_info->disposition == 'ATTACHMENT' && count($part_info->dparameters) > 0) {
  18. foreach ($part_info->dparameters as $file) {
  19. if ($file->attribute == 'FILENAME') {
  20.  
  21. $file_info = pathinfo($file->value);
  22. $fp = fopen('files/' . $msg_info->subject . '.' . $file_info['extension'], 'w+') or exit('Failed to open file');
  23. $msg_body = imap_fetchbody($inbox, $email_msg, $key + 1);
  24. $content = base64_decode($msg_body);
  25. fwrite($fp, $content);
  26. }
  27. }
  28. }
  29. }
  30. }
  31. }
  32. ?>
Add Comment
Please, Sign In to add comment