Kit Kit - 1 year ago 105
Android Question

imap_fetchbody - Encoded body error

I am trying to fetch a particular section of an email body using:

$message = imap_fetchbody ($imap_stream,$n,1);

This works fine for emails sent using Outlook, Hotmail, Yahoo etc. But when I try and fetch the body of the an email sent from a Blackberry or Andriod device, the message is encoded and scrambled such as:


Although the body is encoded, the header is fine. How do I decode the message body of en email sent from an Android or Blackberry device?

Thank you,


Answer Source

You should be able to run imap_fetchstructure to get the encoded value. If that value equals 3 you need to decode via imap_base64.

I've used the following before (Can't remember if it was ever tested with sent mobile email before though):

$structure = imap_fetchstructure($stream, $msgno);
$text = imap_fetchbody($stream, $msgno, $partnum);   
if($structure->encoding == 3) {
    $text = imap_base64($text);
} else if($structure->encoding == 4) {
    $text = imap_qprint($text));