#!/usr/bin/php Msgno); // If The message have the right subject: if (eregi($subject, $chead->subject)) { // Log $log .= "good subject\n"; // Get the filename $mail_content = imap_body($mail, $mid); $mail_content = substr($mail_content, 0, 500); // This is very ugly, but really faster ;-) eregi(".+[p|P]hotoname=([a-zA-Z]+)", $mail_content, $fn_out); $filename_full = $fn_out[1] . ".jpg"; $filename_thumb = $fn_out[1] . "_s.jpg"; // Log $log .= "filename is $filename_full\n"; // Now, extract the image attachement $struct = imap_fetchstructure($mail, $mid); $contentParts = count($struct->parts); // There should be 2 or more parts in the message if ($contentParts >= 2) { // Getting all the attachements of the mail for ($i=2; $i<=$contentParts; $i++) { $att[$i-2] = imap_bodystruct($mail,$mid,$i); } for ($k=0;$kparameters[0]->value == "us-ascii" || $att[$k]->parameters[0]->value == "US-ASCII") { if ($att[$k]->parameters[1]->value != "") { $selectBoxDisplay[$k] = $att[$k]->parameters[1]->value; } } elseif ($att[$k]->parameters[0]->value != "iso-8859-1" && $att[$k]->parameters[0]->value != "ISO-8859-1") { $selectBoxDisplay[$k] = $att[$k]->parameters[0]->value; } } } if (sizeof($selectBoxDisplay) > 0) { for ($j=0; $j < sizeof($selectBoxDisplay); $j++) { // Catch the right attachement (the jpg one) if (eregi("jpg", $selectBoxDisplay[$j])) { // Only write out the image file if the filename is set if ($fn_out[1]) { // It's the one we need. Save it in the $tmpdir/desiredfilename // Log $log .= "write image on disk\n"; $strFileName = $att[$j]->parameters[0]->value; $fileContent = imap_fetchbody($mail,$mid,$j+2); $handle = fopen("/tmp/$filename_full", 'a'); fwrite($handle, imap_base64($fileContent)); fclose($handle); // Now, we need to create another image, smaller: $image_size = getimagesize("/tmp/$filename_full"); // Get the size of the original image: $image_height = $image_size[1]; $image_width = $image_size[0]; // If the image is landscape: if ($image_height <= $image_width) { // The image will be 300 width $thumb_width = 300; $thumb_height = round($image_height / ($image_width/300)); } // Otherwise else { $thumb_height = 300; $thumb_width = round($image_width / ($image_height/300)); } // While I'm unable to correctly compile gd inside my php, I use ImageMagick // Log $log .= "create thumb\n"; system("/usr/bin/convert /tmp/$filename_full -resize " . $thumb_width . "x" . $thumb_height . " /tmp/$filename_thumb"); // And now, last thing, upload it on my website: // Log $log .= "send files via ftp\n"; $conn_id = ftp_connect("FTP_SERVER"); ftp_login($conn_id, "USERNAME", "PASSWORD"); ftp_put($conn_id, "PATH/$filename_full", "/tmp/$filename_full", FTP_BINARY); ftp_put($conn_id, "PATH/$filename_thumb", "/tmp/$filename_thumb", FTP_BINARY); ftp_close($conn_id); } } } } } else { // Log $log .= "bad subject\n"; } // The mail has been processed. Now, we delete it // Log $log .= "delete mail"; imap_delete($mail, $mid); imap_expunge($mail); } // Log $log .= "finished!\n"; echo $log; // Send me an email with result: if ($mails > 0) { $mail_to = 'MYMAIL'; $subject = 'Moblog photo on nakan.ch'; $headers = 'From: blog.photo@orongo' . "\r\n"; mail($mail_to, $subject, $log, $headers); } ?>