PHP Classes

Decoding imap_body()

Recommend this page to a friend!

      PHP MIME Email Message Parser  >  All threads  >  Decoding imap_body()  >  (Un) Subscribe thread alerts  
Subject:Decoding imap_body()
Summary:Can i decode an imap-body instead of an mbox file?
Messages:12
Author:Tommi Trinkaus
Date:2010-03-14 10:05:24
Update:2010-07-24 19:32:30
 
  1 - 10   11 - 12  

  1. Decoding imap_body()   Reply   Report abuse  
Picture of Tommi Trinkaus Tommi Trinkaus - 2010-03-14 10:05:25
Hello Manuel,

is it possible to decode the result objects of any of the php imap functions with your mimeparser? The example does only work with a stored mbox file.

I would like to store emails from an email server into a database and then handle them in my own web email client.

I got no problems getting the headers and storing them and also the attachments are recognized correctly and i can save them into files and ceate an database entry for each of them.

The problem seems to be the content of the message itself - and i would like to use your mimeparser for that to handle all the decodings and multipart-things because this seems to be a little more difficult.

So is it possible to do something like:
$msg = imap_body(...);
$decodedArray = mimeparser->decode($msg);

Thanks for any help.

tt

  2. Re: Decoding imap_body()   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2010-03-14 20:25:34 - In reply to message 1 from Tommi Trinkaus
You need to get both the headers and the body of the message and concatenate them before parsing the message with the class.

  3. Re: Decoding imap_body()   Reply   Report abuse  
Picture of Tommi Trinkaus Tommi Trinkaus - 2010-03-15 07:25:09 - In reply to message 2 from Manuel Lemos
If i understand you right, i need the sourcecode or rawdata of the complete email as it is stored in an eml file or mbox file.
But i can't find an imap function that gives me this stuff.
All of them return nice arrays to me, like e.g. imap_headerinfo(), but how can i retrieve the hole thing?

To store the email for later forwarding, answering and so on it would probalbly be helpfull to store it in an eml or mbox file, but the first thing to do is getting the complete email source, is it?

Thanks for your help!

tt

  4. Re: Decoding imap_body()   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2010-03-15 09:55:03 - In reply to message 3 from Tommi Trinkaus
You can use this to assemble the whole message. Just take care that for large messages it may exceed the configured PHP memory limits.

$whole_message = implode("\r\n", imap_headers($message))."\r\n".imap_body($message);

  5. Re: Decoding imap_body()   Reply   Report abuse  
Picture of Tommi Trinkaus Tommi Trinkaus - 2010-03-15 13:33:35 - In reply to message 4 from Manuel Lemos
Sorry but that doesn't work. I believe you mean imap_header(), not imap_headers(), but then still the imap_header() function returns an object, not an array that can be imploded.

Instead I tried:
----------------
$header = (array) imap_header($imap, $msgid);
$header = implode("\r\n", $header);

and i got something like:
----------------
Fri, 12 Mar 2010 20:40:27 +0100
Fri, 12 Mar 2010 20:40:27 +0100
Freiexemplare etc.
Freiexemplare etc.

Thomas Trinkaus
Array
Reinhard Friedrich
Array
...
----------------

But as far as i know an email source should begin with "From" and look something like
-----------------
From - Sat Mar 13 15:37:25 2010
X-Account-Key: account5
X-UIDL: UID344-1254383253
X-Mozilla-Status: 0001
X-Mozilla-Status2: 00000000
X-Mozilla-Keys:
Return-Path: <[email protected]>
...
------------------

Where's the mistake, what have i done wrong?

  6. Re: Decoding imap_body()   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2010-03-15 22:07:29 - In reply to message 5 from Tommi Trinkaus
Right, I do not use the IMAP extension, so I was not sure what function would retrieve the headers. Anyway, if you look at the PHP manual section for the IMAP extension, it seems imap_fetchheader is the function that gets all the raw headers of message into a string.

php.net/imap_fetchheader

  7. Re: Decoding imap_body()   Reply   Report abuse  
Picture of Tommi Trinkaus Tommi Trinkaus - 2010-03-16 11:35:06 - In reply to message 6 from Manuel Lemos
Thanks, now we're getting closer.

The correct use of imap functions has to be:
$whole_message = imap_fetchheader($imap,$msgid)."\r\n".imap_body($imap,$msgid);

When i save that result to an .eml or .txt file, i can open it e.g. in thunderbird and your script "test_message_decoder.php" will return success and an array with contents.

What i was actual looking for, is a way to view the mail itself so i have to find the essential body parts of the array.
Can you tell me the best way to get the content?
Is it: Search the array for [BodyPart] which is not an attachement ([FileDisposition] != "attachment") and then decide wether to show text/plain or text/html (from [content-type]) in case of both available?
And how to show the cid-images?

  8. Re: Decoding imap_body()   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2010-03-16 17:27:09 - In reply to message 7 from Tommi Trinkaus
You can use either SaveBody parameter to save message parts to files or do not use the SkipBody parameter to make the message part data be returned as strings in the parsed message array.

  9. Re: Decoding imap_body()   Reply   Report abuse  
Picture of Tommi Trinkaus Tommi Trinkaus - 2010-03-16 18:20:58 - In reply to message 8 from Manuel Lemos
Thank you very much - the SaveBody parameter is pretty cool.

If you or anybody else is interested: To get a reasonable sourcecode to be used as an .eml-file or even to fill a mbox-file i added one line with "From..." at the beginning of the imap-data.

So its:
----------------------
$header = imap_headerinfo($imap, $msgNo); // only for first line
$from = "From ".$header->from[0]->mailbox."@".$header->from[0]->host." ".date("D M j H:i:s Y", strtotime($header->date)); // Create From-Line

$whole_message = $from."\r\n".imap_fetchheader($imap, $msgNo)."\r\n".imap_body($imap, $msgNo); // Put everything together
----------------------

This you can store as .eml file or insert into a mbox, open with e.g. Thunderbird, parse with your mimeparser...

  10. Re: Decoding imap_body()   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2010-03-16 20:58:21 - In reply to message 9 from Tommi Trinkaus
The MIME parser class does not require the messages to start with a From line. That is used only in multiple messages contained in a mbox format messages.

 
  1 - 10   11 - 12