Guest User

Untitled

a guest
Jun 28th, 2018
837
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. <?php
  2.  
  3. $gmail_username = 'username@gmail.com';
  4. $gmail_password = 'mypassword';
  5.  
  6. $imap = imap_open ("{imap.gmail.com:993/imap/ssl}INBOX", $gmail_username, $gmail_password) or die("can't connect: " . imap_last_error());
  7. $savefilepath = 'path/to/images_folder/'; //absolute path to images directory
  8. $imagefilepath = 'images/'; //relative path to images directory
  9.  
  10. $headers = imap_headers($imap);
  11. foreach ($headers as $mail) {
  12. $flags = substr($mail, 0, 4);
  13. //Check for unread msgs, get their UID, and queue them up
  14. if (strpos($flags, "U")) {
  15. preg_match('/[0-9]+/',$mail,$match);
  16. $new_msg[] = implode('',$match);
  17. }
  18. }
  19.  
  20. if ($new_msg) {
  21. foreach ($new_msg as $result) {
  22. $structure = imap_fetchstructure($imap,$result);
  23. $parts = $structure->parts;
  24. foreach ($parts as $part) {
  25. if ($part->parameters[0]->attribute == "NAME") {
  26. //Generate a filename with format DATE_RANDOM#_ATTACHMENTNAME.EXT
  27. $savefilename = date("m-d-Y") . '_' . mt_rand(rand(), 6) . '_' . $part->parameters[0]->value;
  28. save_attachment(imap_fetchbody($imap,$result,2),$savefilename,$savefilepath,$savethumbpath);
  29. imap_fetchbody($imap,$result,2); //This marks message as read
  30. }
  31. }
  32. }
  33. }
  34.  
  35. imap_close($imap);
  36.  
  37. function save_attachment( $content , $filename , $localfilepath, $thumbfilepath ) {
  38. if (imap_base64($content) != FALSE) {
  39. $file = fopen($localfilepath.$filename, 'w');
  40. fwrite($file, imap_base64($content));
  41. fclose($file);
  42. }
  43. }
  44. ?>
Add Comment
Please, Sign In to add comment