open inbox and extract picture out of mail..
Moderator: General Moderators
-
seeker2921
- Forum Contributor
- Posts: 120
- Joined: Sat Mar 22, 2003 7:10 pm
- Location: Wiesbaden Germany
- Contact:
open inbox and extract picture out of mail..
My web host has it setup that my e-mail's inbox is /home/stephen/mail/stephen/inbox (inbox isn't I directory its a file) and all the e-mails are stored in this one file, how would I go about getting the e-mails out of this inbox file?
-
Illusionist
- Forum Regular
- Posts: 903
- Joined: Mon Jan 12, 2004 9:32 pm
-
seeker2921
- Forum Contributor
- Posts: 120
- Joined: Sat Mar 22, 2003 7:10 pm
- Location: Wiesbaden Germany
- Contact:
Okay, I sent one e-mail to it and opened the file in CuteFTP Pro for editing and this is what it gave me.. (at the bottom is some of the image info I'm only displaying part of it to show the setup of the file theres alot more to it..)
I have a script that I want to edit to work with my inbox setup, Would it help if I posted to script as well?
Code: Select all
From mye-mail@mydomain.com Thu Apr 08 21:19:31 2004
Return-path: <mye-mail@mydomain.com>
Envelope-to: emailrecipeant@mydomain.com
Delivery-date: Thu, 08 Apr 2004 21:19:31 -0400
Received: from ї209.225.28.219] (helo=mxsf19.cluster1.charter.net)
by eclipse.mydomain.net with esmtp (Exim 4.24)
id 1BBkg7-0002N6-If
for emailrecipeant@maydomain.com; Thu, 08 Apr 2004 21:19:31 -0400
Received: from lucifer (xxxxxxx.xxxxx.xxxx.charter.com ї68.186.xx.xxx])
by mxsf19.cluster1.charter.net (8.12.11/8.12.11) with SMTP id i391EtQr061260
for <emailrecipeant@maydomain.com>; Thu, 8 Apr 2004 21:14:56 -0400 (EDT)
Message-ID: <000901c41dd0$117ffca0$6401a8c0@lucifer>
From: "Stephen Gunn" <MYe-mail@mydomain.com>
To: <emailrecipeant@maydomain.com>
Subject: testing
Date: Thu, 8 Apr 2004 18:14:54 -0700
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_NextPart_000_0005_01C41D95.6438E780"
X-Priority: 3
X-MSMail-Priority: Normal
X-Mailer: Microsoft Outlook Express 6.00.2720.3000
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2727.1300
This is a multi-part message in MIME format.
------=_NextPart_000_0005_01C41D95.6438E780
Content-Type: multipart/alternative;
boundary="----=_NextPart_001_0006_01C41D95.6438E780"
------=_NextPart_001_0006_01C41D95.6438E780
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Testing..
------=_NextPart_001_0006_01C41D95.6438E780
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2737.800" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#dfecf7>
<DIV><FONT face=3DArial size=3D2>Testing..</FONT></DIV></BODY></HTML>
------=_NextPart_001_0006_01C41D95.6438E780--
------=_NextPart_000_0005_01C41D95.6438E780
Content-Type: image/jpeg;
name="Natash2.jpg"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="Natash2.jpg"
/9j/4AAQSkZJRgABAQAAZABkAAD/4gIsSUNDX1BST0ZJTEUAAQEAAAIcQURCRQIQAABtbnRyUkdC
IFhZWiAH0AAEAAoACwAHAARhY3NwQVBQTAAAAABub25lAAAAAAAAAAAAAAAAAAAAAAAA9tYAAQAA
AADTLUFEQkUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAApj
cHJ0AAAA/AAAACRkZXNjAAABIAAAAGZ3dHB0AAABiAAAABRia3B0AAABnAAAABRyVFJDAAABsAAA
AA5nVFJDAAABwAAAAA5iVFJDAAAB0AAAAA5yWFlaAAAB4AAAABRnWFlaAAAB9AAAABRiWFlaAAAC
CAAAABR0ZXh0AAAAAChjKSAyMDAwIEFkb2JlIFN5c3RlbXMgSW5jLgBkZXNjAAAAAAAAAAtDdXN0
b20gUkdCAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFhZWiAAAAAAAADzHQABAAAAAWfyWFlaIAAA
AAAAAAAAAAAAAAAAAABjdXJ2AAAAAAAAAAEBzQAAY3VydgAAAAAAAAABAc0AAGN1cnYAAAAAAAAA
AQHNAABYWVogAAAAAAAAbSkAADniAAAC+VhZWiAAAAAAAABemgAAr78AABhcWFlaIAAAAAAAACsS
//Rest of image code then this
------=_NextPart_000_0005_01C41D95.6438E780--Image Extraction
This should be no problem and I would be happy to help you with it.
Notice the line in your e-mail source that says "boundary=" followed by a somewhat random-looking string of characters (more or less so, depending upon the e-mail client that composed the message). That boundary string separates each portion of a multi-part message, including the part that contains your image (or parts that contain your images). Knowing this, you can extract each part of the message for individual inspection.
Now notice the part of your e-mail that says "Content-Type: image/jpeg;". As you already know, that part represents your image. The key thing to notice here is the "Content-Transfer-Encoding" line, which tells you that the image is base64-encoded. Now you know where your content is and how it is encoded, so one way to accomplish what you want to do is to match the part of the e-mail you want, match any relevant data within that--file name, the image source (/9j/ ... /2Q==), etc.--then decode the image source using PHP's built-in base64_decode function and do what you will with your decoded image data from there (save it to a file or database, display it, etc.).
Here are some reference links for you:
PHP Manual: base64_decode
RFC 2045: Multipurpose Internet Mail Extensions (MIME)
Have fun!
Notice the line in your e-mail source that says "boundary=" followed by a somewhat random-looking string of characters (more or less so, depending upon the e-mail client that composed the message). That boundary string separates each portion of a multi-part message, including the part that contains your image (or parts that contain your images). Knowing this, you can extract each part of the message for individual inspection.
Now notice the part of your e-mail that says "Content-Type: image/jpeg;". As you already know, that part represents your image. The key thing to notice here is the "Content-Transfer-Encoding" line, which tells you that the image is base64-encoded. Now you know where your content is and how it is encoded, so one way to accomplish what you want to do is to match the part of the e-mail you want, match any relevant data within that--file name, the image source (/9j/ ... /2Q==), etc.--then decode the image source using PHP's built-in base64_decode function and do what you will with your decoded image data from there (save it to a file or database, display it, etc.).
Here are some reference links for you:
PHP Manual: base64_decode
RFC 2045: Multipurpose Internet Mail Extensions (MIME)
Have fun!
-
seeker2921
- Forum Contributor
- Posts: 120
- Joined: Sat Mar 22, 2003 7:10 pm
- Location: Wiesbaden Germany
- Contact:
Thanks! But I'm having problems with it.. heres my code that I'm trying to do this with..
the script creates the file with the date info and an image but the image doesn't contain any data (its 0 bites in size) What am I doing wrong?
I e-mailed to writer of the code with all the chnages I have made to it but they never responded to me..
Thanks for any help you can provide me with..
the script creates the file with the date info and an image but the image doesn't contain any data (its 0 bites in size) What am I doing wrong?
I e-mailed to writer of the code with all the chnages I have made to it but they never responded to me..
Code: Select all
<?
//Function for finding strings
function strpos2($haystack, $needle, $match) {
$offset = 0;
$needlelen = strlen($needle);
for($i=0; $i<$match; ++$i) {
$thispos = strpos($haystack, $needle, $offset);
if(!$thispos) return false;
$offset = $thispos + $needlelen;
}
return $thispos;
}
// Open the mailbox
system('rm /home/stephen/public_html/site/moblog/'); //full path to scratch directory
$inbox = '/home/stephen/mail/cam/inbox'; //full path to inbox
$open = fopen($inbox, "r");
$input = fread($open, filesize($inbox));
fclose($open);
function Remove($string, $sep1, $sep2, $count)
{
$check = strpos2($string, $sep2, $count); //starts cropping inbox files
$string = strrev(substr($string, 0, strpos2($string, $sep2, $count)));
$string = strrev(substr($string,0,strpos($string, strrev($sep1))));
if ($check == 'FALSE') $string = "ended";
return $string;
}
$maxcount = 2 * substr_count($input, 'From stephengunn@charter.net'); //in all cases, replace xxxxxxxxx with phone number
for ($count=2;$count<=$maxcount;$count=$count+2){
$predecode = remove($input, 'filename=Natash2.jpg', '--Boundary_', $count);
$text = remove($input, 'From stephengunn@charter.net', 'Return-path: <stephengunn@charter.net>', $count/2);
$date = strtotime($text);
$dateout = date ('D d/m/Y g:ia', mktime (date("H", $date)+8 , date("i", $date), date("s", $date), date("m", $date), date("d", $date), date("y", $date)));
$output = base64_decode($predecode);
$textname = "$count";
$fptext = fopen("/home/stephen/public_html/site/moblog/$textname", "a");
$write = fputs($fptext, $dateout);
fclose ($fptext);
$filename = "$count.jpg";
$fp = fopen("/home/stephen/public_html/site/moblog/$filename", "a");
$write = fputs($fp, $output);
fclose($fp);
}
//counts number of existing files
function countFiles($dir)
{
$count = 0;
if (file_exists($dir))
{
$handle = opendir($dir);
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != "..")
{
$count++;
}
}
closedir($handle);
}
return $count;
}
//finishes counting, and writes files and dates
$count = 1 + (countfiles('/site/moblog/images/camera'))/2;
$newfiles = countfiles('/site/moblog/images');
if ($newfiles != 0){
for ($repeat = 2;$repeat<=$newfiles;$repeat = $repeat+2) {
copy ("/site/moblog/images/$repeat.jpg","/site/moblog/images/camera/$count.jpg");
copy ("/site/moblog/images/$repeat","/site/moblog/images/camera/$count");
$count++;
}}
$count--;
system('rm /home/stephen/mail/cam/inbox');
//copy ("/home/stephen/mail/cam/empty", "/home/stephen/mail/stephen/inbox");
$donefiles = $newfiles/2;
print(''.$donefiles.' files were added to make a total of '.$count.' files. Thankyou.');
print('<br><br>Please click <a href="phonecam.php">here</a> to return to the phonecam.');
?>Something To Get You Started
Here is a little something to get you started:
I wrote something longer first, but this pares it down to the most vital things you need to do.
Code: Select all
<?php
/* An Image Extraction Demonstration
by Brian Sexton
Thursday, April 8th, 2004
License: None Required -- Use Freely, But at Your Own Risk!
Please Note:
What follows is merely a quick and dirty demonstration. A complete
solution would need to inspect each e-mail part to verify the content type
and the content transfer encoding before attempting to extract the image
source (or image sources). Otherwise, it is possible to match something
that is not, in fact, the source of a JPEG image. Also, what follows
matches only the first string of characters that appears to be a base64-
encoded JPEG image, so multiple JPEG images and JPEG images that are
otherwise encoded are not matched.
*/
if (preg_match("/\/9j\/.+\/2Q==/is", file_get_contents('./file.ext'), $imageSourceMatches))
{
header('Content-Type: image/jpeg');
print base64_decode($imageSourceMatches[0]);
}
else
{
header('Content-Type: text/plain');
print "<P>A base64-encoded JPEG image was not found.</P>\n";
}
?>
Last edited by Brian on Thu Apr 08, 2004 10:44 pm, edited 1 time in total.
Your Inbox File Format
Of course, if your inbox file format stores multiple messages within a single file, you will need to extract the desired message before extracting the image source.