title: How can I read an XLSX mail attachment file with PHPExcel?
body: <p>I am connecting through IMAP to get the XLSX attachments from a mailbox so I can convert these spreadsheets to PHP array data with PHPExcel, but when I load the file and convert it to array, the array is empty.</p>
  
  <p>This is the function that gets the attachments from the mails matching the criteria:</p>
  
  <pre><code>public function getEmailAttachments($criteria){
    $emails = imap_search($this-&gt;inbox, $criteria);
    $email_attachments = [];
    if($emails) {
      rsort($emails);
      foreach ($emails as $email_number) {
        $attachments = [];
        //get Email structure
        $structure = imap_fetchstructure($this-&gt;inbox, $email_number);
        /* if any attachments found... */
        if(isset($structure-&gt;parts) &amp;&amp; count($structure-&gt;parts))
        {
            for($i = 0; $i &lt; count($structure-&gt;parts); $i++)
            {
                $attachments[$i] = array(
                    'is_attachment' =&gt; false,
                    'filename' =&gt; '',
                    'name' =&gt; '',
                    'attachment' =&gt; ''
                );
  
                if($structure-&gt;parts[$i]-&gt;ifdparameters)
                {
                    foreach($structure-&gt;parts[$i]-&gt;dparameters as $object)
                    {
                        if(strtolower($object-&gt;attribute) == 'filename')
                        {
                            $attachments[$i]['is_attachment'] = true;
                            $attachments[$i]['filename'] = $object-&gt;value;
                        }
                    }
                }
  
                if($structure-&gt;parts[$i]-&gt;ifparameters)
                {
                    foreach($structure-&gt;parts[$i]-&gt;parameters as $object)
                    {
                        if(strtolower($object-&gt;attribute) == 'name')
                        {
                            $attachments[$i]['is_attachment'] = true;
                            $attachments[$i]['name'] = $object-&gt;value;
                        }
                    }
                }
  
                if($attachments[$i]['is_attachment'])
                {
                    $attachments[$i]['attachment'] = imap_fetchbody($this-&gt;inbox, $email_number, $i+1);
                    /* 3 = BASE64 encoding */
                    if($structure-&gt;parts[$i]-&gt;encoding == 3)
                    {
                        $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
                    }
                    /* 4 = QUOTED-PRINTABLE encoding */
                    elseif($structure-&gt;parts[$i]-&gt;encoding == 4)
                    {
                        $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
                    }
                }
            }
        }
        $email_attachments = array_merge($email_attachments, array_filter($attachments, function($attachment){return $attachment['is_attachment'] == 1;}));
      }
    }
    imap_close($this-&gt;inbox);
    return $email_attachments;
  }
  </code></pre>
  
  <p>Then this is the function that writes the XLSX files to the server and loads them with PHPExcel to convert them to Array data:</p>
  
  <pre><code>public function getEmailReports(){
    $this-&gt;loadPhpExcel();
    $attachments = parent::getEmailAttachments('FROM "example@example.com"');
     //iterate through each attachment and save it
    foreach($attachments as $attachment){
      $filename = $attachment['name'];
      if(empty($filename)) $filename = $attachment['filename'];
      $folder = "data/excel";
      $file_path = "./". $folder ."/". time() . "-" . $filename;
      $fp = fopen($file_path, "w");
      fwrite($fp, $attachment['attachment']);
      $excelReader = PHPExcel_IOFactory::createReaderForFile($file_path);
      $excelObj = $excelReader-&gt;load($file_path);
      foreach ($excelObj-&gt;getWorksheetIterator() as $worksheet) {
        $worksheets[$worksheet-&gt;getTitle()] = $worksheet-&gt;toArray();
      }
      fclose($fp);
      print_r($worksheets); //ARRAY IS EMPTY HERE
      //unlink($file_path);
    }
  }
  </code></pre>