Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
|
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 31st, 2007, 04:49 PM
Registered User
 
Join Date: Aug 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Save Email attachments

Hi,

I'm trying to create a website which creates a user gallery from photos that they send via email (via mobile phones / pda's etc). I've pretty much sorted out the security issues and logistics of the site but I'm currently having problems with extracting attachments and putting them into a folder somewhere locally on my server.

I have sorted out the coding to extract details from imap including the emails and the attachments but I can't find anywhere which tells me how to save the attachments - some of the tutorials I've read just dump the attachments as raw code.

I have this so far:

Code:
<?

$server = "localhost"; 
$user = "*******"; 
$pass = "*******";
$imap = @imap_open("\{$server:143/imap/notls}INBOX", $user, $pass)
or die("Connection to the server failed");

// Set UK time variable
putenv("TZ=Europe/London");


$message_count = imap_num_msg($imap);

for ($mid = 1; $mid <= $message_count; ++$mid) {
  $header = imap_header($imap, $mid);
  $prettydate = date("jS F Y h:m", $header->udate);


  $email = "{$header->from[0]->mailbox}@{$header->from[0]->host}";
  $subject = $header->subject;
  echo "<b>Message:</b> $mid <br />
        <b>Date:</b> $prettydate <br/> 
        <b>Email address:</b> $email <br/>
        <b>Subject:</b> $subject<br/>";

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

        if (!$parts) { /* Simple message, only 1 piece */
          $attachment = array(); /* No attachments */
          $content = imap_body($imap, $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 */
                $filedata = imap_fetchbody($imap, $mid, $partstring);
                $filedata = "<textarea rows=5 cols=50>".base64_decode($filedata)."</textarea>";
                $attachment[] = array("filename" => $parts[$i]->dparameters[0]->value,
                                      "filedata" => $filedata);
              } elseif (strtoupper($parts[$i]->subtype) == "PLAIN") { /* Message */
                $content .= imap_fetchbody($imap, $mid, $partstring);
              }
            }

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

        echo "<b>Content:</b> $content<br />";
        echo "<b>Attachments:</b><pre>"; print_r ($attachment);
        echo "</pre><br /><p>";
}



imap_close($imap);
?>
As you notice I've put the data from the attachments into a textarea - this is because it just fills up the browser window with a load of characters. The start of this I've noticed as the EXIF data from the photos so I know thats the data I need to be able to plant into a file somehow - just need to know how to do it!!!

 
Old September 1st, 2007, 01:43 PM
Registered User
 
Join Date: Aug 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I've found a way around my issue myself!! I decided to search for how to save 32-bit encoded data into files and found a snippet of code on how to do it!

For those who need to know how it was solved in just 3 lines!:

Code:
$ifp = fopen( $filename, "wb" );
fwrite( $ifp, base64_decode( $rawdata ) );
fclose( $ifp );
$filename is the filename (well duh)
$rawdata is the data direct from the email (un-decoded)

Don't you just hate it when 3 weeks of pulling your hair out results in just 3 simple lines of code!!






Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Copy/Save Email Attachments in OE6 Draffus Pro VB 6 2 June 7th, 2006 11:03 PM
Email with Attachments echovue Access 2 March 22nd, 2005 10:36 AM
Email attachments tucker Excel VBA 1 June 10th, 2004 03:25 AM
Email Attachments Doug Riede Classic ASP Basics 0 October 24th, 2003 11:11 AM
Email w/Attachments aingalsbe PHP How-To 1 June 25th, 2003 04:48 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.