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  

  11. Re: How do I parse e-mail attachments?   Reply   Report abuse  
Picture of Christophe Charron Christophe Charron - 2007-08-22 13:00:29 - In reply to message 10 from Manuel Lemos
Aie Aie !!!
Here is a today gmail made email.

test03.christophe-charron.org/publi ...
And file names are encoded that way !

Best regards,
Christophe Charron

  12. working with messages   Reply   Report abuse  
Picture of Jay Jay - 2008-03-26 08:55:31 - In reply to message 10 from Manuel Lemos
hi i have tried parsing and decoding the mail attachments, and its been workin for sometime now using a not-so-good custom code, LoL :D.

but i got problem when trying to decode a message... it could not display properly the foreign characters, say for example Japanese.

though i was able to decode Japanese from its header (e.g., filename)... just the message. well, the attachments were workin fine though, just the message displayed.

anyone who knows the work around for this?

thanks and God bless!

  13. Re: How do I parse e-mail attachments?   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2008-03-26 20:40:15 - In reply to message 12 from Jay
The class parses and decodes the messages in the original character set they are sent.

If you want to display the messages in an HTML page, you need to generate a page with the same character set encoding as the data from the message.

  14. Problem to SaveBody parts   Reply   Report abuse  
Picture of ANastasia Lita ANastasia Lita - 2008-05-25 11:02:30 - In reply to message 2 from Manuel Lemos
I have tell the class to save all message body parts by unmark
'SaveBody'=>'/tmp'
(http://i295.photobucket.com/albums/mm140/jeaffreygilbert/softwares/saveBody.jpg)

I look at 'tmp' folder (that i created before).. it is empty +_+"
(http://i295.photobucket.com/albums/mm140/jeaffreygilbert/softwares/emptyTmp.jpg)

Result on browser is getting error:
"Warning: fopen(/tmp\1) [function.fopen]: failed to open stream: No such file
or directory in C:\Program Files\xampp\htdocs\pop3\mime_parser.php on line
1328"
(http://i295.photobucket.com/albums/mm140/jeaffreygilbert/softwares/atBrowser.jpg)

And i look at file "parse_message.php" (line 1328), but i don't understand
about error from that code.
(http://i295.photobucket.com/albums/mm140/jeaffreygilbert/softwares/fOpenError.jpg)

Please someone give me a simple code to:
- Get "Subject" of emails to be set on $subject
- Attachment (just 1 file image) from each email:
+ Get filename of attachment to be set on $filename
+ Get last parts of a message that is encapsulated with content-type
multipart/mixed to be encoded as image file again.

Thank u so much.

  15. Re: How do I parse e-mail attachments?   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2008-05-25 17:24:49 - In reply to message 14 from ANastasia Lita
You are using Windows. So you should not be using /tmp as save path because that is not a valid Windows directory path. You should specify the actual Windows path of the directory you want to save the message body parts.

For the rest of what you want, you can obtain from the Analyze function.

  16. Re: How do I parse e-mail attachments?   Reply   Report abuse  
Picture of Raghuvansh Raghuvansh - 2008-09-13 09:40:20 - In reply to message 10 from Manuel Lemos
I am working with DBmail for getting mails aimed at our server directly into our database. But somehow , I am not getting any result array.
I am attaching the code used below.

<?php
require_once('mime_parser.php');
require_once('rfc822_addresses.php');





$con = mysql_connect('localhost','root','mypass') or die('Could not connect:' . mysql_error());
mysql_select_db("dbmail", $con);

$result = mysql_query("select messageblk from dbmail_messageblks where messageblk_idnr = 15");
$row = mysql_fetch_array($result);
$email = $row['messageblk'];

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






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);
}


?>

  17. Re: How do I parse e-mail attachments?   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2008-09-13 19:10:26 - In reply to message 16 from Raghuvansh
The MIME parser class has a function named Analyze that can return the message structure in an easy to process array. Please use that instead. Take a look at the test_message_decoder.php example script.

  18. Re: How do I parse e-mail attachments?   Reply   Report abuse  
Picture of Tim Dwyer Tim Dwyer - 2010-02-16 14:57:40 - In reply to message 17 from Manuel Lemos
Hello - I'm consistently receiving the following error message when attempting to save the parts of the email:

'MIME message decoding error: could not create file /saved/1/ to save the message body part'

The error appears for any type of email with or without attachments. I've worked with the scripts and can't seem to figure out what I'm missing. Any assistance or suggestions would very much be appreciated.

Thanks.

  19. Re: How do I parse e-mail attachments?   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2010-02-16 17:58:44 - In reply to message 18 from Tim Dwyer
It seems you are trying to save messages to a directory that either does not exist or the current machine user does not have permissions to write files in it.

Are you sure that there is a directory named saved in the root directory? Do you have permissions to write in it?

  20. Re: How do I parse e-mail attachments?   Reply   Report abuse  
Picture of Tim Dwyer Tim Dwyer - 2010-02-16 20:34:30 - In reply to message 19 from Manuel Lemos
The path to my webroot was wrong - I've corrected and now everything is being saved properly. Manuel, your class is outstanding - thank you - I've put it to work already!!

 
  1 - 10   11 - 20