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!!!