...

View Full Version : Read text into variable



treeleaf20
10-13-2009, 08:18 PM
All,
I can find a string in a set of data by using the following code:


if(strpos($data, "Subject:")){
echo "Subject is found";
}


Now that I can find it, I want to be able to read the rest of the line into a $subject variable. How can I do this?

Thanks in advance.

SystemJay
10-13-2009, 08:31 PM
1) One way
http://php.net/manual/en/function.substr.php
With substr() you can get rest of the string from an index.

2) Another way
http://php.net/manual/en/function.explode.php
use explode() to deliver the string into an array which has in first index the
string Subject: and in another index the rest of the string. Then you can
access the rest of the string with $arrayName[1] for example.

These came up to my mind first :)

Fou-Lu
10-13-2009, 08:37 PM
$data sounds to me like its a multi-line string. So, use a pattern match for it instead:


$matches = array();
if (preg_match('/Subject:(.*)\n|$/', $data, $matches))
{
print $matches[1]; // That should be the rest.
}


But I guess that all depends on what $data is.

treeleaf20
10-13-2009, 09:16 PM
That worked perfect. Thanks.

treeleaf20
10-13-2009, 09:22 PM
Another question, if I have to find another thing and then go to EOF how would I do that?

I tried something like:


$matches_string = array();
if (preg_match('/X-Attachment-Id:(.*)EOF|$/', $data, $matches_string))
{
$filename = $matches_string[1];
print "<br><br>The non decoded string is: $filename";
$filename = base64_decode($filename);
print "<br><br>The decoded string is: $filename"; // That should be the rest.
}


But it didn't work. Thanks.

treeleaf20
10-13-2009, 11:05 PM
$filetype2 = array();
if (preg_match('/CX-Attachment-Id:(.*)\n|.EOF', $data, $filetype2))
{
$filetypeuse2 = $filetype2[1];
echo "The filetypeuser2 is $filetypeuse2";
}

I also tried this but get an error saying the EOF isn't an ending delimiter.

Fou-Lu
10-14-2009, 02:12 AM
In pcre (perl compatible regular expressions, the patterns used in preg), $ denotes end of content line. Using what is above will handle any entry that has the string provided, match the given criteria, and finish with either a new line or end of content.
If you're using a file read (fopen with fread), you need to retrieve one line at a time in order to use the match, but can jump to the end of the file using fseek($fileHandle, 0, SEEK_END);. feof() is used to determine if the fread should continue, but I don't believe that PHP has a supported EOF constant.

With what you are doing here, you can actually look at using a preg_match_all, with pattern of '/(.*):(.*)\n|$/mU' with PREG_SET_ORDER to capture all of you're combinations and do later comparisons. That would look like so:


$matches = array();
preg_match_all('/(.*):(.*)\n|$/mU', $input, $matches, PREG_SET_ORDER);

Something along that lines (I can't do any testing atm).

treeleaf20
10-14-2009, 02:17 AM
I am using fread. Here is the rest of the code:


fwrite($fp, "RETR 1\n", 32);

$octets = fgets($fp, 64); // read the first line
$parts = explode(' ', $octets); // split the first line by spaces
$octets = $parts[1]; // we want the data after the second space
settype($octets, 'int'); // make sure its a number

$bytesread = 0; // we havent read any bytes of the actual email yet
$data = 'Email Message Follows: <br /><br />';
while($bytesread < $octets){

$new_data = fgets($fp, 4096); // read the data
$data .= '<br />'.$new_data; // add it onto the existing data
//$data .= $new_data; // add it onto the existing data
$byteslength = strlen($new_data);
$bytesread += $byteslength;
}

fread($fp, 64);

echo $data;


When I subsituted my $data variable inplace of the $input variable I got the following error:
Fatal error: Only variables can be passed by reference in /home/content/94/5043494/html/emailtest.php on line 56

Thanks.

treeleaf20
10-14-2009, 02:21 AM
Sorry that was my fault, I updated it to the following:


$files = array();
preg_match_all('/(.*):(.*)\n|$/mU', $data, $files, PREG_SET_ORDER);
echo "The files are ".$files[3];


I get the output:
The files are Array

Why wouldn't it display what is in $files[3]?

Fou-Lu
10-14-2009, 02:30 AM
$files[3] is a collection of every thing that matches each other. I'm wondering if the set order would be better than pattern order or not...
If you print_r($files[3]), you should get back an array containing all of the 'equals' parts (as in: Subject: THIS VALUE HERE IS OFFSET 3) for every line in the $data. Data of course is only useful if its representable as a string - a resource will fail, so there would be no purpose to seek to end of file. If you're still wanting to abort once the proper line is matched, just use fgets to get the line, use a standard preg_match, see if you got what you need, and seek to end. Assuming of course that the fgets is in a while loop that stops when feof is true.

Also, you can use named subpatterns, check out the last example here: http://php.ca/manual/en/function.preg-match-all.php


Y'know what, I think I lied on that one. The set order will be the third matches (ie: to, from subject, it will be the entire collection of subject), followed by the offsets within subject. Man, y'know its been too long since you've used patterns when :P

treeleaf20
10-14-2009, 02:32 AM
Here is an example of the data:

--0016e64617d6b55eb30475d6fb3e--
--0016e64617d6b55ebb0475d6fb40
Content-Type: image/jpeg; name="psu_logo.jpg"
Content-Disposition: attachment; filename="psu_logo.jpg"
Content-Transfer-Encoding: base64
X-Attachment-Id: f_g0r43rin0

/9j/4AAQSkZJRgABAgEASABIAAD/4ROrRXhpZgAATU0AKgAAAAgABwESAAMAAAABAAEAAAEaAAUA
AAABAAAAYgEbAAUAAAABAAAAagEoAAMAAAABAAIAAAExAAIAAAAcAAAAcgEyAAIAAAAUAAAAjodp
AAQAAAABAAAApAAAANAACvyAAAAnEAAK/IAAACcQQWRvYmUgUGhvdG9zaG9wIENTMiBXaW5kb3dz
ADIwMDg6MTI6MTEgMTE6Mzg6MzAAAAAAA6ABAAMAAAAB//8AAKACAAQAAAABAAABLKADAAQAAAAB
AAAAyAAAAAAAAAAGAQMAAwAAAAEABgAAARoABQAAAAEAAAEeARsABQAAAAEAAAEmASgAAwAAAAEA
AgAAAgEABAAAAAEAAAEuAgIABAAAAAEAABJ1AAAAAAAAAEgAAAABAAAASAAAAAH/2P/gABBKRklG
AAECAABIAEgAAP/tAAxBZG9iZV9DTQAC/+4ADkFkb2JlAGSAAAAAAf/bAIQADAgICAkIDAkJDBEL

For some reason, even in the example you provided I can't get the following data:
/9j/4AAQSkZJRgABAgEASABIAAD/4ROrRXhpZgAATU0AKgAAAAgABwESAAMAAAABAAEAAAEaAAUA
AAABAAAAYgEbAAUAAAABAAAAagEoAAMAAAABAAIAAAExAAIAAAAcAAAAcgEyAAIAAAAUAAAAjodp
AAQAAAABAAAApAAAANAACvyAAAAnEAAK/IAAACcQQWRvYmUgUGhvdG9zaG9wIENTMiBXaW5kb3dz
ADIwMDg6MTI6MTEgMTE6Mzg6MzAAAAAAA6ABAAMAAAAB//8AAKACAAQAAAABAAABLKADAAQAAAAB
AAAAyAAAAAAAAAAGAQMAAwAAAAEABgAAARoABQAAAAEAAAEeARsABQAAAAEAAAEmASgAAwAAAAEA
AgAAAgEABAAAAAEAAAEuAgIABAAAAAEAABJ1AAAAAAAAAEgAAAABAAAASAAAAAH/2P/gABBKRklG
AAECAABIAEgAAP/tAAxBZG9iZV9DTQAC/+4ADkFkb2JlAGSAAAAAAf/bAIQADAgICAkIDAkJDBEL

I can get the rest of it however. Any ideas?

Fou-Lu
10-14-2009, 02:39 AM
That doesn't match the pattern.
What happens if you use this:


'/(.*):(.*)/'

in conjuntion with preg_match_all? Does that give everything associated with the X-Attachment-Id? If not, pattern matching prbbably isn't what you want. That inline content doesn't appear to be base64 btw.

treeleaf20
10-14-2009, 02:44 AM
I added the following to display the output:

$files = array();
preg_match_all('/(.*):(.*)/', $data, $files, PREG_SET_ORDER);
foreach ($files as $val) {
echo "<br>matched: " . $val[0] . "\n";
}


It does give me the attachment id:
X-Attachment-Id: f_g0r43rin0

However still doesn't give me anything with the encoded file. I've been told that everything after the X-Attachment-Id: f_g0r43rin0 is the file that I sent to my POP email account and then it got encode to base64. Is there anyway not to encode the attachment?

Fou-Lu
10-14-2009, 03:03 AM
Try two more potential changes to the pattern, these just involve modifiers:


'/(.*):(.*)/s'

// And if that doesn't work, try this:
'/(.*):(.*)/sm'

I don't think the multi-line will matter, methinks that the dotall will suffice.

As for you're question, yes PHP has a built in base64 encoder and decoder (of course, you can always remap if necessary). The problem is, it doesn't appear as base64. Its been awhile since I've encoded anything, but I'm pretty certain that base64 identifies itself by ending with ==.
Also, if you add more to you're pattern, you may be able to capture it with a third pattern match using: (.*)?. I don't know if I'd recommend that though, since it may start getting greedy after that, so I'd go with splitting the results after with an explode.

Try that code out post back - sadly I'm at work so I can't test anything out for you.

treeleaf20
10-14-2009, 03:35 AM
That works great, I did the following:


$filetype2 = array();
if (preg_match('/(X-Attachment-Id:)(.*)/s', $data, $filetype2))
{
$filetypeuse3 = $filetype2[2];
print "<br><br>The subject is: $filetypeuse3"; // That should be the rest.
}


When this gets output it gives me:
The subject is: f_g0r43rin0

/9j/4AAQSkZJRgABAgEASABIAAD/4ROrRXhpZgAATU0AKgAAAAgABwESAAMAAAABAAEAAAEaAAUA
AAABAAAAYgEbAAUAAAABAAAAagEoAAMAAAABAAIAAAExAAIAAAAcAAAAcgEyAAIAAAAUAAAAjodp
AAQAAAABAAAApAAAANAACvyAAAAnEAAK/IAAACcQQWRvYmUgUGhvdG9zaG9wIENTMiBXaW5kb3dz
ADIwMDg6MTI6MTEgMTE6Mzg6MzAAAAAAA6ABAAMAAAAB//8AAKACAAQAAAABAAABLKADAAQAAAAB
AAAAyAAAAAAAAAAGAQMAAwAAAAEABgAAARoABQAAAAEAAAEeARsABQAAAAEAAAEmASgAAwAAAAEA
AgAAAgEABAAAAAEAAAEuAgIABAAAAAEAABJ1AAAAAAAAAEgAAAABAAAASAAAAAH/2P/gABBKRklG

Is there anyway I can get rid of the f_g0r43rin0 followed by the two <br><br>?

By the way, I only copied some of the output and the encoding. It's huge so I didn't want to take up the whole space. There are no == but I think it's probably encoded correctly.

Fou-Lu
10-14-2009, 03:41 AM
Is it actually <br /> or is it \r\n? I'm thinking its just line feeds, you can split them with $parts = explode(PHP_EOL, $filetypeuse3); which will give you (from the looks of it) three parts: f_g0r43rin0, \r\n, and the encoded string.

treeleaf20
10-14-2009, 03:50 AM
I think the code you just gave me should work but the problem is for every line it inserts a <br> from the following code:


while($bytesread < $octets){

$new_data = fgets($fp, 4096); // read the data
$data .= '<br />'.$new_data; // add it onto the existing data
//$data .= $new_data; // add it onto the existing data
$byteslength = strlen($new_data);
$bytesread += $byteslength;
}

fread($fp, 64);
echo $data;


Once I have the filetypeuse3 can I remove the <br> from it??


$filetype2 = array();
if (preg_match('/(X-Attachment-Id:)(.*)/s', $data, $filetype2))
{
$filetypeuse3 = $filetype2[2];
//After I get the $filetypeuse3 remove all the <br>
$parts = explode(PHP_EOL, $filetypeuse3);
print "<br><br>The subject is: $parts[2]"; // That should be the rest.
}


If I would remove all of these would the code you just provided still work? I don't think it would because it wouldn't split it by /r/n or <br>.

treeleaf20
10-14-2009, 03:52 AM
So for my example, using the code you provided I got:

The subject is:
/9j/4AAQSkZJRgABAgEASABIAAD/4ROrRXhpZgAATU0AKgAAAAgABwESAAMAAAABAAEAAAEaAAUA

Instead of all of it, because it inserts a <br> after every line feed.

Fou-Lu
10-14-2009, 04:17 AM
Ah, scan the entire string into a variable, and use chunk_split on it instead. Chunk_split has three parameters, two of which are optional and set up to handle base64 encoded data. Sweet huh?
I would include the entire string first, then explode to separate than use the chunk_split. This will save a headache from the linefeeds later.

treeleaf20
10-14-2009, 04:27 AM
I tried to do this:

$filetype2 = array();
if (preg_match('/(X-Attachment-Id:)(.*)/s', $data, $filetype2))
{
$filetypeuse3 = $filetype2[2];
$parts = explode(PHP_EOL, $filetypeuse3);
$new_string = chunk_split($parts);
print "<br><br>The subject is: $new_string[2]"; // That should be the rest.
}


But I don't think it's how you had it designed...

Fou-Lu
10-14-2009, 05:17 AM
Yes, but that's only half, you need to do this part too:


while($bytesread < $octets){

$new_data = fgets($fp, 4096); // read the data
$data .= '<br />'.$new_data; // add it onto the existing data
//$data .= $new_data; // add it onto the existing data
$byteslength = strlen($new_data);
$bytesread += $byteslength;
}

change that to:


while($bytesread < $octets){

$data .= fgets($fp, 4096); // read the data
$byteslength = strlen($new_data);
$bytesread += $byteslength;
}

Not sure if you need to actually keep track of the length read (since the ftell will tell you this in bytes, minus 1). Instead, you could just use a !feof() on that to read the data in.

Then in you're match, you would perform the split like what you have there.

treeleaf20
10-14-2009, 03:33 PM
Ok, so I got this to work...kind of:

$filetype = array();
if (preg_match('/Content-Disposition: attachment; filename="(.*)\n|$/', $data, $filetype))
{
$filetypeuse = $filetype[1];
$len=strlen($filetypeuse);
$filetypeuse=substr($filetypeuse,0,($len-2));
}
$file_id = array();
if (preg_match('/X-Attachment-Id:(.*)\n|$/', $data, $file_id))
{
$file_id_new = $file_id[1];
print "<br><br>The file_id is: $file_id_new"; // That should be the rest.
}


$filetype2 = array();
if (preg_match('/X-Attachment-Id:(.*)/s', $data, $filetype2))
{
$filetypeuse3 = $filetype2[1];
$npos = strpos("\n", $filetypeuse3);
$npos = strpos("\n", $filetypeuse3, $npos + 1);
$attachment = substr($filetypeuse3, ++$npos);

$pattern = '/'.$file_id_new.'(.*)/s';
echo "<br><br>The pattern is: $pattern<br><br>";
$final_attach = array();
if (preg_match($pattern, $data, $final_attach))
{
$attachment_final = $final_attach[1];
$attachment_final = strtr($attachment_final, "\n", "");
print "<br><br>The final attachment is: $attachment_final"; // That should be the rest.
}
}

$now = time();
$filename=$now.'-'.$filetypeuse;
while(file_exists($uploadFilename = $uploadsDirectory.$filename))
{
$now++;
$filename=$now.'-'.$filename;
}

if(preg_match('/[.](jpg)|(gif)|(png)|(JPG)$/', $filename)) {
print "<br><br>The filetype is: $filename";

$attachment_encoded = $attachment_final; //base64 data from email
$attachment_raw = base64_decode($attachment_encoded);
$uploadplace = $uploadsDirectory.$filename;
file_put_contents($uploadplace,$attachment_raw); //Give full path to $filename

echo "<img src=\"uploaded_files/$filename\">";


I think there are a couple \n in my $attachment_final. I try and get rid of them with the following line:

$attachment_final = strtr($attachment_final, "\n", "");
The file moves and I think it's close because it uploads with the close to the correct size that it was uploaded but it doesn't show up yet. Any ideas?

treeleaf20
10-14-2009, 08:13 PM
I guess a better question is to decode the base64, what do I need to decode? Is it just the text after the attachment Id, or is it all of the image information?

treeleaf20
10-14-2009, 11:03 PM
Ok, so I found a solution to that answer. However I do the following:

file_put_contents('./test/'.$filename,$attachment_raw); //Give full path to $filename


Then I try to do the following:

if(preg_match('/[.](jpg)|(JPG)|(jpeg)|(JPEG)$/', $filename)) {
$im = imagecreatefromjpeg($path_to_image_directory . $filename);
} else if (preg_match('/[.](gif)|(GIF)$/', $filename)) {
$im = imagecreatefromgif($path_to_image_directory . $filename);
} else if (preg_match('/[.](png)|(PNG)$/', $filename)) {
$im = imagecreatefrompng($path_to_image_directory . $filename);
}


However I get an error saying that it's not a valid jpg image. Is this something with file_put_contents or something else??

Fou-Lu
10-15-2009, 03:15 AM
Did you decode the value of $attachment_raw first? It need to be decoded before it can be interpreted as an image. You can use base64_decode for that, but you'll probably need to chunk_split it first.
As for file_put_contents, its not my favorite route with writing, but the api does list that its binary safe, so once you've decoded the image data you can write the binary to the file with file_put_contents.

BTW, you shouldn't need to use an imagecreate* unless you're planning on manipulating the image. Writing it to the file is sufficient for creating the file.

treeleaf20
10-15-2009, 03:20 AM
ok, so I did some more research it needs to be in this format to work:

$filetype3 =
"Content-Type: image/jpeg; name=\"linds_me_aruba.jpg\"
Content-Disposition: attachment; filename=\"linds_me_aruba.jpg\"
Content-Transfer-Encoding: base64
X-Attachment-Id: f_g0sidr630


/9j/4AAQSkZJRgABAQAAAQABAAD//gA7Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcg SlBFRyB2NjIpLCBxdWFsaXR5ID0gODUK/9sAQwAFAwQEBAMFBAQEBQUFBgcMCAcHBwcPCwsJDBEP EhIRDxERExYcFxMUGhURERghGBodHR8fHxMXIiQiHiQcHh8e/9sAQwEFBQUHBgcOCAgOHhQRFB4e Hh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4e/8AAEQgBaQDI AwEiAAIRAQMRAf/EAB8AAAEFAQEBAQEBAAAAAAAAAAABAgMEBQYHCAkKC//EALUQAAIBAwMCBAMF BQQEAAABfQECAwAEEQUSITFBBhNRYQcicRQygZGhCCNCscEVUtHwJDNicoIJChYXGBkaJSYnKCkq NDU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6g4SFhoeIiYqSk5SVlpeYmZqi o6Slpqeoqaqys7S1tre4ubrCw8TFxsfIycrS09TV1tfY2drh4uPk5ebn6Onq8fLz9PX29/j5+v/E AB8BAAMBAQEBAQEBAQEAAAAAAAABAgMEBQYHCAkKC//EALURAAIBAgQEAwQHBQQEAAECdwABAgMR BAUhMQYSQVEHYXETIjKBCBRCkaGxwQkjM1LwFWJy0QoWJDThJfEXGB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xGRLJGwx3Ncvq9z ZvdWiWcYKoxPTgntXfleBSqpyuYVqnKjxnX/AAmlr4lje2LbLk+awY5K88111pDHZ2olZGkKfcRe po8Szvc+KLmRkChEUJ9Mdq1tH06e9SNFcqDjnNfcS2SR4cIJzbKEF7qNzkG1S2UAsr7ivvjNdJp/ iyGxsGgu/MmeMZOTnA+tTPoNpZQOZpDK4GSm7jP1rl7rT0uLXUH85Im2FFBQgAk8AetZytI05LM1 rzxQLtiLe2SRWHCt8rGuY0nTxrHxIsVWBjHATNKh6AD/AOvii8tJY7OCO4QpIsYEbZwG44H4Z/Sr XhK9udMknuWIF0UWMuRnjvWdSL9m+QjTnVz0bUXktfmt9KigUckqMl/pVXU/Fd9bQ7bewEbFeW28 1S0rV3uWNxcSOUQfNkcE1na58QNFtLtY7ie2Hb5vmYfgK8iNGbaUo3Opzh0di7beIdfuchF2jGTl a6rwvqF/IjyTxRK2euADXnv/AAn+g3SGCzv7aENwfMbYSfYGiy1PVIZWNuXdH53L8y4+tbVMI6kO W1iY1Unvc9envY54cOFc+hqqJ0jgZLbZDIe/XFea2uualGeI+nJLHrV2a/vLmHzYwIpHHzHd/L0r z45bKErXOj26fQv+LbCbU3Ob5JZsYx0xXDjS76KZ8zLhDgsDV+DUJbK4fzm3MeGyc5rds7iC4ti1 vZ/fHJNetC9CNrXRySjGozG/4TLVNOW3giuWCQjHHeitFtJ0a4f99b5kxyM9KKnmwrd5R1H7Gqtm abX2f4s1G92pPOCa5M65Z9rlMfWga3Znn7Un50lgLO6R2PGRZ1Zul7ACmPcKDvGC3ua5ddctAObq Pk+tP/tmzP8Ay9Rj/gVX9UktRfWoPQ6N76XAwQv0qUX7iIDeCfU1yv8AbFoT/wAfMX/fVOOr2vQ3 ";

treeleaf20
10-15-2009, 03:20 AM
Once again the data is:


Received: (qmail 22148 invoked from network); 14 Oct 2009 20:02:49 -0000 Received: from unknown (HELO p3pismtp01-012.prod.phx3.secureserver.net) ([10.6.12.12]) (envelope-sender ) by smtp11-02.prod.mesa1.secureserver.net (qmail-1.03) with SMTP for ; 14 Oct 2009 20:02:49 -0000 Received: from mail-vw0-f203.google.com ([209.85.212.203]) by p3pismtp01-012.prod.phx3.secureserver.net with ESMTP; 14 Oct 2009 13:02:44 -0700 Received: by vws41 with SMTP id 41so61844vws.4 for ; Wed, 14 Oct 2009 13:02:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:date:message-id:subject :from:to:content-type; bh=sZxSvdfd6iaW/nkPk7xDS24Bw/wnh0N/v70yYsNHrB4=; b=ddgDNN92b9i7EZm9eOerbiiLXggwUWCcDmydUvGT0nWV0/J0q7eNmaAMg5sXSlGPkH zSRqIz3KWNHQxb1g/JQ+o006BpKsllrff6oOxY7mZlKBX8Hr/gQUM2JFAu9enYwrAadi u5qEhp1vrW3gY0BnShdPCWC8/oqyUD7IfT1q0= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:content-type; b=vzTFMjPyydWrmYXyRa6mOCYyuXaMBeTr1hf6xFqG1dynvERnUcyJ8eMTADvfTFJN9i ppIJXZmWjTVf0xMggj5U93/9IHbaa6tUfMxSl2FzY+j7LwJ/XEwvx/nSf7sGpkFv6636 QOfSbNPzaJ8q8efycq6cv0gB8KfP0q5208ByY= MIME-Version: 1.0 Received: by 10.220.79.105 with SMTP id o41mr13303915vck.70.1255550563166; Wed, 14 Oct 2009 13:02:43 -0700 (PDT) Date: Wed, 14 Oct 2009 16:02:43 -0400 Message-ID: Subject: More testing From: User User1To: email@email.com Content-Type: multipart/mixed; boundary=00163631073750ac1b0475eaa507 X-Nonspam: None --00163631073750ac1b0475eaa507 Content-Type: multipart/alternative; boundary=00163631073750ac120475eaa505 --00163631073750ac120475eaa505 Content-Type: text/plain; charset=ISO-8859-1 This is some test in the body of the email. --00163631073750ac120475eaa505 Content-Type: text/html; charset=ISO-8859-1 This is some test in the body of the email. --00163631073750ac120475eaa505-- --00163631073750ac1b0475eaa507 Content-Type: image/jpeg; name="flip_the_bird.jpg" Content-Disposition: attachment; filename="flip_the_bird.jpg" Content-Transfer-Encoding: base64 X-Attachment-Id: f_g0sidr630 /9j/4AAQSkZJRgABAQAAAQABAAD//gA7Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcg SlBFRyB2NjIpLCBxdWFsaXR5ID0gODUK/9sAQwAFAwQEBAMFBAQEBQUFBgcMCAcHBwcPCwsJDBEP EhIRDxERExYcFxMUGhURERghGBodHR8fHxMXIiQiHiQcHh8e/9sAQwEFBQUHBgcOCAgOHhQRFB4e Hh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4e/8AAEQgBaQDI AwEiAAIRAQMRAf/EAB8AAAEFAQEBAQEBAAAAAAAAAAABAgMEBQYHCAkKC//EALUQAAIBAwMCBAMF BQQEAAABfQECAwAEEQUSITFBBhNRYQcicRQygZGhCCNCscEVUtHwJDNicoIJChYXGBkaJSYnKCkq NDU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6g4SFhoeIiYqSk5SVlpeYmZqi o6Slpqeoqaqys7S1tre4ubrCw8TFxsfIycrS09TV1tfY2drh4uPk5ebn6Onq8fLz9PX29/j5+v/E AB8BAAMBAQEBAQEBAQEAAAAAAAABAgMEBQYHCAkKC//EALURAAIBAgQEAwQHBQQEAAECdwABAgMR BAUhMQYSQVEHYXETIjKBCBRCkaGxwQkjM1LwFWJy0QoWJDThJfEXGBkaJicoKSo1Njc4OTpDREVG R0hJSlNUVVZXWFlaY2RlZmdoaWpzdHV2d3h5eoKDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKz tLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uLj5OXm5+jp6vLz9PX29/j5+v/aAAwDAQACEQMRAD8A 93hYnBZsfSrKsqjO4fia8rOsasg/4+TSNrupBeZifxr5Rwfc+iVN9j1F54Rwzrn61DNdW6/xrn61 5ZLrd8/8XPrmo21i+Jznp701T8xeyfY9TNzAy5LLj60C6tSeZFx25ry4azej+JvzpRrNznofzo9m x+zfY9QlurJcneDmqst9ar0YV5u+s3WD8p/OmjWJxjeD+dHs2J032PSUvbZh1Aoae1bOZFrz9Naw O9PTV0Y8k0cjTDkZ3EghYcMuPrVSQxbyqyAn0rzTxR40FiohtSWkPU1yth4tvI7w3QlZiR0JrpjQ m0ZaLc9yWby2A8wg+maspdSIPv5x0rwtPEmq3l6JhdbXzwM/0rsdJ8STPCgnZmmJwfaoqUJR3Gve 2PTFu2OAxqZZc9W6VxkOqS5BO41YXVZs8frXNqWoM6zzVJ+9mmvKvfP51zYv5QAWdaqz6pdujBOA Dx700m0HKzqJJ4h2JP1qGS4iCksuB71zE2rPbx5mbLkcAVh6jrWpagfskEbJzwfarjF9SXFnS6j4 q0yzlMTsCw6YrG1TxtDCQIow5Pp2rmpPDd6ZPPuZg3dsmql/NaxbUWPeVHPvXRCnF6EOLR3en6nq V7AsptxCjDIOcgirlneq0gS

Fou-Lu
10-15-2009, 04:09 AM
Have you tried using the imap functions in PHP? I found this article as well, which was posted on the usernotes for imap_bodystruct: http://www.linuxscope.net/articles/mailAttachmentsPHP.html
It is specifically for retreiving attachments in the emails.
Also, it looks like chunk_split already exists in that, though I haven't counted the characters to line feeds, it looks like its about 76. Except the last line, but that could be because of having more capital letters in a non-monospace format.

treeleaf20
10-15-2009, 04:11 AM
I wish I could but I'm using godaddy as my hosting service and they don't have it enabled on a shared hosting account because of security so I'm stuck with doing it this way. I got it to work when I did it manually using the example above. If I can just get the data in that format it works good. Just not good enough with regular expressions to get it that way though.

Fou-Lu
10-15-2009, 04:28 AM
Are you referring to this format:


$filetype3 =
"Content-Type: image/jpeg; name=\"linds_me_aruba.jpg\"
Content-Disposition: attachment; filename=\"linds_me_aruba.jpg\"
Content-Transfer-Encoding: base64
X-Attachment-Id: f_g0sidr630


/9j/4AAQSkZJRgABAQAAAQABAAD....

?
If so, can you expand on what you mean that it requires this format (a method? Function? etc)? You should only reaquire the data presented in the base64 to create a file, every thing else is irrelevant (though to display it through the data you'd need to pass headers - not if you're using something like the <img src=... /> that you have above there since that will interpret the image).
I'll need to think/write some stuff, but that will have to wait until I'm off of work to do it. Methinks I should do some work tonight o.O

treeleaf20
10-15-2009, 04:39 AM
No, the only way I got the image to show is by having it in that format and then use the following code:


$p = '#\r?\n\r?\n#';
$parts = preg_split($p, $filetype3, 2);

// The image is in the body part
$body = $parts[1];

$attachment_encoded = $body; //base64 data from email
$attachment_raw = base64_decode($body);

file_put_contents('./test/'.$filename,$attachment_raw); //Give full path to $filename

echo "<img src='./test/$filename'>";


The image is displayed that way. If I take anything out of the format I showed you earlier the image doesn't get displayed. Even though the image is displayed I'm still a little worried that it's not necessarily saving the file correctly. When I scroll over it in my control panel on the website it just shows a black image (ones that I upload to the control panel without this actually show the image) but it's displayed via the web in the IMG tag that I show above. Then when I try to do the createimagefromjpeg it gives me an error saying the file isn't a valid jpeg file.

If your wondering where I get the $filename from I get it from the following code:

$p = '#content-disposition:\s*attachment\s*;\s*filename=[\'"](.*)[\'"]#i';
preg_match($p, $filetype3, $match);
$header_filename = $match[1];
echo "<br><br>The header filename is ".$header_filename;

// Here I go with a preset name. Did you want the $header_filename? Then use it here.
$now = time();
$filename = $now.'-'.$header_filename;


Once again this is dependent on getting the right format from the original $data to put it in the format from your last post.

Thanks again for your help, it's greatly appreciated.

Fou-Lu
10-15-2009, 10:28 AM
I'll need to study up on the rfc first, than get back to this. Emails support more than one attachments, so I'll need to determine what requirements exist, what constitutes body, and how the boundaries are in play. Then I'll see if I can throw a quick class together for this.

treeleaf20
10-15-2009, 02:26 PM
That'd be great. Once again thanks for all the help.

treeleaf20
10-15-2009, 04:28 PM
To try and make life a little easier, I found this:

http://scripts.top4download.com/pop3-message-handler-for-php/sbiuq.html

I'm not sure if this will do the same thing but it looks like it has potential. Maybe this could give you an idea??

treeleaf20
10-16-2009, 04:19 PM
So I found a new class. It looks close. The class itself is:


class parseMail {
var $from="";
var $to="";
var $subject="";
var $received="";
var $date="";
var $message_id="";
var $content_type="";
var $part =array();

// decode a mail header
function parseMail($text="") {
$start=0;
$lastheader="";
while (true) {
$end=strpos($text,"\n",$start);
$line=substr($text,$start,$end-$start);
$start=$end+1;
if ($line=="") break; // end of headers!
if (substr($line,0,1)=="\t") {
$$last.="\n".$line;
}
if (preg_match("/^(From:)\s*(.*)$/",$line,$matches)) {
$last="from";
$$last=$matches[2];
}
if (preg_match("/^(Received:)\s*(.*)$/",$line,$matches)) {
$last="received";
$$last=$matches[2];
}
if (preg_match("/^(To:)\s*(.*)$/",$line,$matches)) {
$last="to";
$$last=$matches[2];
}
if (preg_match("/^(Subject:)\s*(.*)$/",$line,$matches)) {
$last="subject";
$$last=$matches[2];
}
if (preg_match("/^(Date:)\s*(.*)$/",$line,$matches)) {
$last="date";
$$last=$matches[2];
}
if (preg_match("/^(Content-Type:)\s*(.*)$/",$line,$matches)) {
$last="content_type";
$$last=$matches[2];
}
if (preg_match("/^(Message-Id:)\s*(.*)$/",$line,$matches)) {
$last="message_id";
$$last=$matches[2];
}
}
$this->from=$from;
$this->received=$received;
$this->to=$to;
$this->subject=$subject;
$this->date=$date;
$this->content_type=$content_type;
$this->message_id=$message_id;

if (preg_match("/^multipart\/mixed;/",$content_type)) {
$b=strpos($content_type,"boundary=");
$boundary=substr($content_type,$b+strlen("boundary="));
$boundary=substr($boundary,1,strlen($boundary)-2);
$this->multipartSplit($boundary,substr($text,$start));

} else {
$this->part[0]['Content-Type']=$content_type;
$this->part[0]['content']=substr($text,$start);
}
}
// decode a multipart header
function multipartHeaders($partid,$mailbody) {
$text=substr($mailbody,$this->part[$partid]['start'],
$this->part[$partid]['ende']-$this->part[$partid]['start']);

$start=0;
$lastheader="";
while (true) {
$end=strpos($text,"\n",$start);
$line=substr($text,$start,$end-$start);
$start=$end+1;
if ($line=="") break; // end of headers!
if (substr($line,0,1)=="\t") {
$$last.="\n".$line;
}
if (preg_match("/^(Content-Type:)\s*(.*)$/",$line,$matches)) {
$last="c_t";
$$last=$matches[2];
}
if (preg_match("/^(Content-Transfer-Encoding:)\s*(.*)$/",$line,$matches)) {
$last="c_t_e";
$$last=$matches[2];
}
if (preg_match("/^(Content-Description:)\s*(.*)$/",$line,$matches)) {
$last="c_desc";
$$last=$matches[2];
}
if (preg_match("/^(Content-Disposition:)\s*(.*)$/",$line,$matches)) {
$last="c_disp";
$$last=$matches[2];
}
}
if ($c_t_e=="base64") {
$this->part[$partid]['content']=base64_decode(substr($text,$start));
$c_t_e="8bit";
} else {
$this->part[$partid]['content']=substr($text,$start);
}
$this->part[$partid]['Content-Type']=$c_t;
$this->part[$partid]['Content-Transfer-Encoding']=$c_t_e;
$this->part[$partid]['Content-Description']=$c_desc;
$this->part[$partid]['Content-Disposition']=$c_disp;
unset($this->part[$partid]['start']);
unset($this->part[$partid]['ende']);
}
// we have a multipart message body
// split the parts
function multipartSplit($boundary,$text) {
$start=0;
$b_len=strlen("--".$boundary);
$partcount=0;
while (true) { // should have an emergency exit...
$end=strpos($text,"--".$boundary,$start);
if (substr($text,$end+$b_len,1)=="\n") {
// '\n' => part boundary
$this->part[$partcount]['start']=$end+$b_len+1;
if ($partcount) {
$this->part[$partcount-1]['ende']=$end-1;
$this->multipartHeaders($partcount-1,$text);
}
$start=$end+$b_len+1;
$partcount++;
} else {
// '--' => end boundary
$this->part[$partcount-1]['ende']=$end-1;
$this->multipartHeaders($partcount-1,$text);
break;
}
}
}
}


I pass it the data that I got earlier. I'm just not sure from the class how to get the content that is actually decoded so I can add the image to the web server. Any ideas?

Fou-Lu
10-16-2009, 09:23 PM
Its close, but not quite right. It doesn't gracefully handle the attachment data.
I was in the hospital all day yesterday, and so long as I'm not too tired I'll try to get this going today.

treeleaf20
10-18-2009, 08:13 PM
No problem, hope you feel better. Thanks again.

treeleaf20
10-20-2009, 02:03 AM
Any luck on this?

Fou-Lu
10-21-2009, 12:47 AM
No work on it yesterday, I hope to finish up tonight.
There's an odd condition that I've come across in some of my testing which essentially starts a boundary but doesn't terminate it. So I'm making the assumption that we are to assume that boundaries can be mismatched, though I cannot find that in the rfc for it o.O


Ok so an update. I decided to take the complicated route (as usual ;)), so I'm not done yet. 9 classes and counting, but I'll focus on parsing first - I figured I didn't have a mail handler in my library so I'd make one :P

Also, interestingly enough, rfc2822 doesn't specify a 'to' field as being a requirement, only orig-date and from. I wonder where the 'bare minimum' emails would end up....

treeleaf20
10-22-2009, 03:21 AM
Haha I hope I can figure it out. Thanks again. Just a little question, would that work with an MMS message as well as an email?

Fou-Lu
10-22-2009, 02:19 PM
Haha I hope I can figure it out. Thanks again. Just a little question, would that work with an MMS message as well as an email?

I'd have to confirm against the rfc, but best I know the MMS is a similar format so I'd suspect that it would work.
I'll try to get this done tonight; too tired to work on it last night.

treeleaf20
10-22-2009, 02:26 PM
Ok great. Are you also deleting the email message then? What happens if you get another one in when your processing would it accidentally delete the wrong one? Thanks again.

Fou-Lu
10-22-2009, 02:31 PM
It won't be configured to a host, it will be configured to a stream.
The overall idea is to create a two purpose class, one purpose to write email and MIME from scratch and the other to parse. How it gets or is put there is irrelevant, it can be through imap, pop/smtp, simple file reading, stdin etc.
The parsing is similar to the class you found, though more object oriented. So it will be expecting a stream that is openable.


btw, I'm focusing on the parsing first. This will be an ongoing class build; sadly I'm just not familiar enough with all the email handling to do a full system in a reasonably short period of time.

treeleaf20
10-28-2009, 12:36 AM
No problem. Appreciate the help. I doubt I could be much help but let me know if you need anything. Thanks again.

Fou-Lu
10-28-2009, 05:07 PM
Yep, I'm training a new guy at work who's doing really well, so methinks I'll have some extra time tonight and tomorrow night. Kinda spent the weekend playing demon's souls o.O

treeleaf20
11-10-2009, 04:28 AM
Hey, just checking in to see how this is going?

Fou-Lu
11-10-2009, 01:11 PM
i kinda forgot about this. :o
I'll look back into this tonight after work.

treeleaf20
01-05-2011, 02:46 AM
Hey, sorry I've gotten away from this for awhile but was just wondering if anyone had made any progress on something like this or found something that would be able to support this? Thanks for your help in advance!

Fou-Lu
01-05-2011, 05:51 AM
I need a refresher without going though it again. Obviously I've forgotten what this was again :$

treeleaf20
01-05-2011, 06:02 AM
I was basically trying to be able to receive a file through email and then have a script read the email and get the file from the email and save it to the server and save the filename to a mysql database.

treeleaf20
01-05-2011, 03:40 PM
I did find something like this but not sure how to make something like this work or really where to start:

http://stackoverflow.com/questions/2860585/script-to-parse-emails-for-attachments

There is also this link:
http://www.evolt.org/incoming_mail_and_php

Which makes reference to the PEAR class of Mail_mimeDecode but I'm not sure how to use that as well.

treeleaf20
01-05-2011, 04:10 PM
Don't want anyone to reinvent the wheel so I found this nice little tutorial. I haven't tried it out yet but it seems to be pretty effective:

http://www.damnsemicolon.com/php/php-parse-emails-email-piping-attachments-part-3



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum