Colter

GMAIL IMAP Attachment Download

Dec 18th, 2014
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.49 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. /**
  5.  *  Gmail attachment extractor.
  6.  *
  7.  *  Downloads attachments from Gmail and saves it to a file.
  8.  *  Uses PHP IMAP extension, so make sure it is enabled in your php.ini,
  9.  *  extension=php_imap.dll
  10.  *
  11.  */
  12.  
  13.  
  14. set_time_limit(3000);
  15.  
  16.  
  17. /* connect to gmail with your credentials */
  18. $hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
  19. $username = 'YOUR_GMAIL_USERNAME'; # e.g somebody@gmail.com
  20. $password = 'YOUR_GMAIL_PASSWORD';
  21.  
  22.  
  23. /* try to connect */
  24. $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
  25.  
  26.  
  27. /* get all new emails. If set to 'ALL' instead
  28.  * of 'NEW' retrieves all the emails, but can be
  29.  * resource intensive, so the following variable,
  30.  * $max_emails, puts the limit on the number of emails downloaded.
  31.  *
  32.  */
  33. $emails = imap_search($inbox,'ALL');
  34.  
  35. /* useful only if the above search is set to 'ALL' */
  36. $max_emails = 16;
  37.  
  38.  
  39. /* if any emails found, iterate through each email */
  40. if($emails) {
  41.  
  42.     $count = 1;
  43.  
  44.     /* put the newest emails on top */
  45.     rsort($emails);
  46.  
  47.     /* for every email... */
  48.     foreach($emails as $email_number)
  49.     {
  50.  
  51.         /* get information specific to this email */
  52.         $overview = imap_fetch_overview($inbox,$email_number,0);
  53.  
  54.         /* get mail message */
  55.         $message = imap_fetchbody($inbox,$email_number,2);
  56.  
  57.         /* get mail structure */
  58.         $structure = imap_fetchstructure($inbox, $email_number);
  59.  
  60.         $attachments = array();
  61.  
  62.         /* if any attachments found... */
  63.         if(isset($structure->parts) && count($structure->parts))
  64.         {
  65.             for($i = 0; $i < count($structure->parts); $i++)
  66.             {
  67.                 $attachments[$i] = array(
  68.                     'is_attachment' => false,
  69.                     'filename' => '',
  70.                     'name' => '',
  71.                     'attachment' => ''
  72.                 );
  73.  
  74.                 if($structure->parts[$i]->ifdparameters)
  75.                 {
  76.                     foreach($structure->parts[$i]->dparameters as $object)
  77.                     {
  78.                         if(strtolower($object->attribute) == 'filename')
  79.                         {
  80.                             $attachments[$i]['is_attachment'] = true;
  81.                             $attachments[$i]['filename'] = $object->value;
  82.                         }
  83.                     }
  84.                 }
  85.  
  86.                 if($structure->parts[$i]->ifparameters)
  87.                 {
  88.                     foreach($structure->parts[$i]->parameters as $object)
  89.                     {
  90.                         if(strtolower($object->attribute) == 'name')
  91.                         {
  92.                             $attachments[$i]['is_attachment'] = true;
  93.                             $attachments[$i]['name'] = $object->value;
  94.                         }
  95.                     }
  96.                 }
  97.  
  98.                 if($attachments[$i]['is_attachment'])
  99.                 {
  100.                     $attachments[$i]['attachment'] = imap_fetchbody($inbox, $email_number, $i+1);
  101.  
  102.                     /* 4 = QUOTED-PRINTABLE encoding */
  103.                     if($structure->parts[$i]->encoding == 3)
  104.                     {
  105.                         $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
  106.                     }
  107.                     /* 3 = BASE64 encoding */
  108.                     elseif($structure->parts[$i]->encoding == 4)
  109.                     {
  110.                         $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
  111.                     }
  112.                 }
  113.             }
  114.         }
  115.  
  116.         /* iterate through each attachment and save it */
  117.         foreach($attachments as $attachment)
  118.         {
  119.             if($attachment['is_attachment'] == 1)
  120.             {
  121.                 $filename = $attachment['name'];
  122.                 if(empty($filename)) $filename = $attachment['filename'];
  123.  
  124.                 if(empty($filename)) $filename = time() . ".dat";
  125.  
  126.                 /* prefix the email number to the filename in case two emails
  127.                  * have the attachment with the same file name.
  128.                  */
  129.                 $fp = fopen($email_number . "-" . $filename, "w+");
  130.                 fwrite($fp, $attachment['attachment']);
  131.                 fclose($fp);
  132.             }
  133.  
  134.         }
  135.  
  136.         if($count++ >= $max_emails) break;
  137.     }
  138.  
  139. }
  140.  
  141. /* close the connection */
  142. imap_close($inbox);
  143.  
  144. echo "Done";
  145.  
  146. ?>
Add Comment
Please, Sign In to add comment