I'm trying to create a php project where I extract images from emails and add them to users albums. I have searched the net high and low on the net and have found lots of different scripts which are either overly complicated or display the images incorrectly.
I need a script which will connect to my mailbox, Check each email in turn, go through each segment of the email pulling out any images stored in the email. Save all the images in a folder and input all the details include email address, subject heading, and image(s) location into a database. I know there are lots of security implications regarding anyone being able to send emails to the email address and the photos being published on the website but I'm not to worried about that at the moment while its a private project (i have planned random alphanumerics that need to be placed in the email subject to get round that issue)
So far I have managed to connect to the mailbox and extract the email address, subject and text content and I have managed to get a while loop to go through each segment of the email in search for attachments but I cant find anyway to check if the attachments are images or display the image - it seems to keep coming out in a large block of random text - I know that it probably needs encoding but how do I go about doing that and saving it into a folder?
Code as close as I can go without giving me headaches:
<?
$server = "localhost";
$user = "*******";
$pass = "*******";
$imap = @imap_open("\{$server:143/imap/notls}INBOX", $user, $pass)
or die("Connection to the server failed");
$message_count = imap_num_msg($imap);
for ($mid = 1; $mid <= $message_count; ++$mid) {
$header = imap_header($imap, $mid);
$prettydate = date("jS F Y", $header->udate);
$email = "{$header->from[0]->mailbox}@{$header->from[0]->host}";
echo "$prettydate, $email <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 = base64_encode($filedata);
$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 "Analyzed message $mid, result: <br />";
echo "Content: $content<br /><br />";
echo "Attachments:"; print_r ($attachment);
echo "<br /><p>";
}
imap_close($imap);
?>
This picks up each email okay but then just dumps the raw text of an image!