1

I am using a script that I found here: Downloading attachments to directory with IMAP in PHP, randomly works

To connect to a gmail inbox and download certain attachments. The script is this:

$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'XX@XX.com';
$password = 'XX';

/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to 
Gmail: ' . imap_last_error());

/* grab emails */
$emails = imap_search($inbox, 'FROM "xxx@gmail.com"');

/* if emails are returned, cycle through each... */
if($emails) {

/* begin output var */
$output = '';

/* put the newest emails on top */
rsort($emails);

foreach($emails as $email_number) {

/* get information specific to this email */
$overview = imap_fetch_overview($inbox,$email_number,0);
$message = imap_fetchbody($inbox,$email_number,2);
$structure = imap_fetchstructure($inbox,$email_number);

 $attachments = array();
   if(isset($structure->parts) && count($structure->parts)) {
     for($i = 0; $i < count($structure->parts); $i++) {
       $attachments[$i] = array(
          'is_attachment' => false,
          'filename' => '',
          'name' => '',
          'attachment' => '');

       if($structure->parts[$i]->ifdparameters) {
         foreach($structure->parts[$i]->dparameters as $object) {
           if(strtolower($object->attribute) == 'filename') {
             $attachments[$i]['is_attachment'] = true;
             $attachments[$i]['filename'] = $object->value;
           }
         }
       }

       if($structure->parts[$i]->ifparameters) {
         foreach($structure->parts[$i]->parameters as $object) {
           if(strtolower($object->attribute) == 'name') {
             $attachments[$i]['is_attachment'] = true;
             $attachments[$i]['name'] = $object->value;
           }
         }
       }

       if($attachments[$i]['is_attachment']) {
         $attachments[$i]['attachment'] = imap_fetchbody($inbox, $email_number, $i+1);
         if($structure->parts[$i]->encoding == 3) { // 3 = BASE64
           $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
         }
         elseif($structure->parts[$i]->encoding == 4) { // 4 = QUOTED-PRINTABLE
           $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
         }
       }             
     } // for($i = 0; $i < count($structure->parts); $i++)
   } // if(isset($structure->parts) && count($structure->parts))

if(count($attachments)!=0){


    foreach($attachments as $at){

        if($at[is_attachment]==1){

            file_put_contents($at[filename], $at[attachment]);

            }
        }

    }

  }

 // echo $output;
} 

/* close the connection */
imap_close($inbox);

It works correctly for downloading the attahments however it seems to be corrupting zip files, at first I thought that it was because the emails I am trying to access each have two zip files attached and the script might be trying to combine them into one. But I tested by sending a single file over which still got corrupted.

I don't completely understand how the script is working to know how to fix it or what is causing the problem, so could anyone help?

EDIT:

I have just tested this by sending files that are not ZIPs and it seems to be corrupting all file types or saving them as empty, as they have 0 file size. I also considered it may be an issue with running the script from a Raspberry Pi, but I have also tested it on a Debian Virtualbox machine with the same results.

Community
  • 1
  • 1
Edward1442
  • 143
  • 2
  • 2
  • 12
  • Have you opened the file in hex editor or something to see if the corruption is anything obvious, eg, maybe it didn't get decoded from base64? – Max May 12 '17 at 14:54
  • I have just tried that, it doesn't seem to be the case. However the file is completely empty so i'm wondering if the script is failing to download the content and just saving an empty file with the name and extension – Edward1442 May 16 '17 at 15:05
  • This is the perfect answer, go with this https://stackoverflow.com/questions/2649579/downloading-attachments-to-directory-with-imap-in-php-randomly-works/34593248#34593248 – GuRu Aug 18 '17 at 07:15

0 Answers0