PHP Classes

How do I parse e-mail attachments?

Recommend this page to a friend!

      PHP MIME Email Message Parser  >  All threads  >  How do I parse e-mail attachments?  >  (Un) Subscribe thread alerts  
Subject:How do I parse e-mail attachments?
Summary:I'm not able to parse e-mail attachnments.
Messages:20
Author:John Doe
Date:2007-03-21 14:26:09
Update:2010-02-16 20:34:30
 
  1 - 10   11 - 20  

  1. How do I parse e-mail attachments?   Reply   Report abuse  
Picture of John Doe John Doe - 2007-03-21 14:26:09
I have been using parser for a couple of days now and I'm find it very handy. But I'm not sure how I parse the attachments of an e-mail. I can't find any documentation that explains this. Please let me know how this is possible.

Regards, Stephen

  2. Re: How do I parse e-mail attachments?   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2007-03-21 18:58:53 - In reply to message 1 from John Doe
The attachments are just message body parts like any others.

You can tell the class to save all message body parts to a given directory using the 'SaveBody' parameter passed to the Decode function.

The decoded array with the message structure will return the names of the files to which each message body part was saved.

Attachments are usually the last parts of a message that is encapsulated with content-type multipart/mixed . Just traverse the decoded message array structure and you will find the attachment parts as the last message parts.

  3. Re: How do I parse e-mail attachments?   Reply   Report abuse  
Picture of John Doe John Doe - 2007-03-22 09:15:00 - In reply to message 2 from Manuel Lemos
Very swift help.

Thank you.

  4. Re: How do I parse e-mail attachments?   Reply   Report abuse  
Picture of John Doe John Doe - 2007-03-22 11:38:59 - In reply to message 2 from Manuel Lemos
1 more question if I may. At the moment it's saving the message in 2 parts, the first file is being named "1" which contains the message body, and secondly is a file named "2" which is the e-mail attachment. Is the MIME parser able to save the attachment as the filename in the e-mail, or does it have to be renamed with my own PHP code?

Regards,

  5. Re: How do I parse e-mail attachments?   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2007-03-22 18:03:53 - In reply to message 4 from John Doe
Yes, you have to rename it if you want to have the attachment with the original file name. Check the FileName property to get the name.

The class does not do that mainly for security reasons. If somebody sends you a message with an attachment with a path that may overwrite important files in your system, it may cause you security problems.

Make sure you validate and filter the FileName property before you use it to save the attachment file.

  6. Re: How do I parse e-mail attachments?   Reply   Report abuse  
Picture of Vlad Mereuta Vlad Mereuta - 2007-06-27 21:28:18 - In reply to message 2 from Manuel Lemos
If somebody needs to parse ALL the parts of an email and get one array with every part, use this recursive function:

function arrayFromEmailParts($parts,&$index){
foreach ($parts as $part) {
$tmp = explode(';',$part['Headers']['content-type:']);
$type = $tmp[0];
// multipart/alternative
if ($type == 'multipart/alternative') {
$ret = array_merge($ret,arrayFromEmailParts($part['Parts'],$index));
} else {
if ($type == 'text/plain' OR $type == 'text/html') {
$ret[$index]['type'] = $type;
$ret[$index]['content'] = file_get_contents($part['BodyFile']);
} else {
$ret[$index]['type'] = $type;
$ret[$index]['file'] = $part['BodyFile'];
foreach($tmp as $data) {
if (strstr($data,'name')) {
$t = explode("=",$data);
$fileName = $t[1];
// outlook (and maybe others?) puts "" around the name, so remove if found
if(substr($fileName,0,1) == '"' AND substr($fileName,strlen($fileName)-1,1) == '"')
$fileName = substr($fileName,1,strlen($fileName)-2);

$ret[$index]['originalFileName'] = $fileName;
}
}
}
$index++;
}
}
return($ret);
}


Use the function like this:

// parameters for the mime parser
$parameters = array(
'Data' => $email,
'SaveBody' => "tmp" // saves every part as a file in tmp directory
);
// Decode
$mime->Decode($parameters, $decoded);
$decoded = $decoded[0];$index = 0;
$parts = $decoded['Parts'];
$result = arrayFromEmailParts($parts,$index);
print_r($result);

It will return an array with every part of the email and a few information about that part ('content' if plain text or html or 'file' and 'originalFileName' if it's something else).

An example of the array you will get:

[0] => Array
(
[type] => text/plain
[content] => mail body. bye bye

)

[1] => Array
(
[type] => text/html
[content] => <b>mail body.</b> bye bye
)

[2] => Array
(
[type] => image/gif
[file] => tmp/3
[originalFileName] => title-signs.gif
)

)


Tested with mails from gmail, outlook and mail (from apple).

One intersting thing is that outlook has some "" around the name of the file while apple does not have that.

Anyway, hope it helps!

  7. Re: How do I parse e-mail attachments?   Reply   Report abuse  
Picture of Vlad Mereuta Vlad Mereuta - 2007-06-27 21:33:35 - In reply to message 6 from Vlad Mereuta
Also please note that this function works and returns an array only if the email has multiple parts.

  8. Re: How do I parse e-mail attachments?   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2007-06-27 22:19:17 - In reply to message 6 from Vlad Mereuta
Thanks. In the future I will try to provide a function that returns the parsed message parts in an easier for common types of messages. I just did not have had the time.

  9. Re: How do I parse e-mail attachments?   Reply   Report abuse  
Picture of Christophe Charron Christophe Charron - 2007-08-21 12:51:29 - In reply to message 6 from Vlad Mereuta
Hi,
I modified a little bit this very good function to make it work with this kind of email

test03.christophe-charron.org/publi ...

I hope it will be usefull and doesn't break anything else.
The initial function did not retreive the two parts (text and html) of the body.

This new one still doesn't get the file name in that case :
["content-type:"]=>string(92) "application/x-zip; name="=?UTF-8?Q?2007-07-19-Cr=C3=A9ation_fichier_adresses_courriel.ods?=""
when names are encoded...


Best regards,
Christophe Charron


function arrayFromEmailParts($parts,&$index){
foreach ($parts as $part) {
$tmp = explode(';',$part['Headers']['content-type:']);
$type = $tmp[0];
// multipart/alternative
if ($type == 'multipart/alternative') {
if (isset($ret)) {
$ret = array_merge($ret,arrayFromEmailParts($part['Parts'],$index));
} else {
$ret = arrayFromEmailParts($part['Parts'],$index);
}
} else {
if ($type == 'text/plain' OR $type == 'text/html') {
$ret[$index]['type'] = $type;
$ret[$index]['content'] = file_get_contents($part['BodyFile']);
} else {
$ret[$index]['type'] = $type;
$ret[$index]['file'] = $part['BodyFile'];
foreach($tmp as $data) {
if (strstr($data,'name')) {
$t = explode("=",$data);
$fileName = $t[1];
// outlook (and maybe others?) puts "" around the name, so remove if found
if(substr($fileName,0,1) == '"' AND substr($fileName,strlen($fileName)-1,1) == '"')
$fileName = substr($fileName,1,strlen($fileName)-2);
$ret[$index]['originalFileName'] = $fileName;
}
}
}
$index++;
}
}
return($ret);
}
Text function is here :
test03.christophe-charron.org/publi ...

  10. Re: How do I parse e-mail attachments?   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2007-08-21 18:36:51 - In reply to message 9 from Christophe Charron
This is odd because file names are not supposed to be encoded that way.

Can you provide a minimal message sample with attached file names encoded that way?

 
  1 - 10   11 - 20