Jump to content

PHP IMAP - Saving email attachemts to files


paul.mearing

Recommended Posts

Hi,

 

Ive been trying for a while to find some code that will parse and save off email attachments to files into a folder. I have created a function that will parse the plain text section of the email (dont need the html part), but was wondering if someone could point me in the right direction for the attachments?? I have tried snippets of code on the php.net site but cant seem to get them working. This was one of the examples I tried and modified to read new mail:


<?php

$mbox = imap_open("{xchurch-svr01:143}INBOX", "test", "test") or die("Connection to server failed");

$emails = imap_search($mbox, 'UNSEEN');
if(!empty($emails)){

echo "emails found";
foreach($emails as $mid)
{


        $struct = imap_fetchstructure($mbox, $mid);
        
        $parts = $struct->parts;
        $i = 0;

        if (!$parts) { /* Simple message, only 1 piece */
          $attachment = array(); /* No attachments */
          $content = imap_body($mbox, $mid);
        } else { /* Complicated message, multiple parts */
        
          $endwhile = false;
        
          $stack = array(); /* Stack while parsing message */
          $content = "";    /* Content of message */
          $attachment = array(); /* Attachments */
        
          while (!$endwhile) {
            if (!$parts[$i]) {
              if (count($stack) > 0) {
                $parts = $stack[count($stack)-1]["p"];
                $i     = $stack[count($stack)-1]["i"] + 1;
                array_pop($stack);
              } else {
                $endwhile = true;
              }
            }
          
            if (!$endwhile) {
              /* Create message part first (example '1.2.3') */
              $partstring = "";
              foreach ($stack as $s) {
                $partstring .= ($s["i"]+1) . ".";
              }
              $partstring .= ($i+1);
            
              if (strtoupper($parts[$i]->disposition) == "ATTACHMENT") { /* Attachment */
                $attachment[] = array("filename" => $parts[$i]->parameters[0]->value,
                                      "filedata" => imap_fetchbody($mbox, $mid, $partstring));
              } elseif (strtoupper($parts[$i]->subtype) == "PLAIN") { /* Message */
                $content .= imap_fetchbody($mbox, $mid, $partstring);
              }
            }

            if ($parts[$i]->parts) {
              $stack[] = array("p" => $parts, "i" => $i);
              $parts = $parts[$i]->parts;
              $i = 0;
            } else {
              $i++;
            }
          } /* while */
        } /* complicated message */

        echo "Analyzed message $mid, result: <br />";
        echo "Content: $content<br /><br />";
        echo "Attachments:"; print_r ($attachment);

} else {

echo "no mail";

}
?>

 

All i get with this is a http 500 error!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.