title: How to retrieve Attached Messages from attachment in emails in php?
body: <p>I am using the below code to retrieve attachments from an email using the <code>imap_fetchstructure</code>. The code works fine for normal attachments like Images, Files, but not Outlook items. See the code below:</p>
  
  <pre><code>        $this-&gt;msg_cnt = imap_num_msg($this-&gt;conn);
          $this-&gt;TOTAL_MESSAGES = $this-&gt;msg_cnt;
  
          $msgs = imap_sort($this-&gt;conn, SORTDATE, 1, SE_UID);
  
          $in = array();
          $inctr = 0;
          foreach ($msgs as $msguid)
          {
              $msgno = imap_msgno($this-&gt;conn, $msguid);
              $has_attachment = false;
              $ATT_FILES = "";
              $attachment_value = "";
              $file_name = "";
  
              $structure = imap_fetchstructure($this-&gt;conn, $msgno);
             $attachments = array();
             $log = "Attachments:\r\n\r\n";
              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'])
                      {
                          $has_attachment = true;
                          $attachments[$i]['attachment'] = imap_fetchbody($this-&gt;conn, $msgno, $i+1);
                          if($structure-&gt;parts[$i]-&gt;encoding == 3)
                          { // 3 = BASE64
                              $result = "Found file attachment (QUOTED-PRINTABLE).\r\n\r\n";
                              file_put_contents("/var/www/html/attachment_logs.txt", $result, FILE_APPEND);
                              $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
                          }
                          elseif($structure-&gt;parts[$i]-&gt;encoding == 4)
                          { // 4 = QUOTED-PRINTABLE
                              ob_start();
                              echo "QUOTED-PRINTED FOUND. CONTENT:\r\n";
                              var_dump($attachments[$i]);
                              $result = ob_get_clean();
                              file_put_contents("/var/www/html/attachment_logs.txt", $result, FILE_APPEND);
                              $result = "Found email attachment (QUOTED-PRINTABLE).\r\n\r\n";
                              file_put_contents("/var/www/html/attachment_logs.txt", $result, FILE_APPEND);
  
                              $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
                          }
                          $attachment_value = $attachments[$i]['attachment'];
                          if (empty($attachments[$i]['filename']))
                              $attachments[$i]['filename'] = "".time()."";
                          $file_name = $attachments[$i]['filename'];
                          $folder = "/var/www/html/dvxcss/attachments/";
                          if (!file_exists($folder))
                          {
                              mkdir($folder);
                          }
  
                          //WRITE THE ATTACHMENT
                          ob_start();
                          var_dump($attachments);
                          $log .= ob_get_clean();
                          $log .= "\r\n\r\n#################################\r\n";
                          file_put_contents("/var/www/html/attachment_logs.txt", $log, FILE_APPEND);
                          $ATT_FILES .= $file_name.";";
                          file_put_contents($folder.$file_name, $attachment_value);
                      }             
                  }
              }
  
              $in[$inctr] = array (
                  'index'     =&gt; $inctr,
                  'header'    =&gt; imap_headerinfo($this-&gt;conn, $msgno),
                  'body' =&gt; $this-&gt;getBody($msguid, $this-&gt;conn),
                  'structure' =&gt; imap_fetchstructure($this-&gt;conn, $msgno),
                  'hasattachment' =&gt; $has_attachment,
                  'attachment' =&gt; "$file_name",
                  'attfiles' =&gt; $ATT_FILES
              );
              $inctr++;
          }
  </code></pre>
  
  <p>The code gets the filename of the attachment and write it into the server.</p>
  
  <p><strong>Question:</strong></p>
  
  <p>How do I fetch Outlook's Email Attachment using php?</p>
  
  <p>Any help will be so much appreciated. Thanks!</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Found out that they are .eml files. These items are attached as .eml files by outlook. I tried to use gmail to check and it gets a file named "noname.eml".</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p><a href="https://stackoverflow.com/questions/21936983/imap-parsing-original-headers-from-a-bounced-mail">IMAP- Parsing original headers from a bounced mail</a></p>
  
  <p>It is called <strong>message/rfc822</strong> or <strong>text/rfc822</strong> bodypart.</p>