Exemple #1
0
 /**
  * Returns the text message body.
  *
  * @return string The message body
  * @see Mime_Helper::getMessageBody()
  */
 public function getMessageBody()
 {
     $parts = array();
     foreach ($this as $part) {
         $ctype = $part->getHeaderField('Content-Type');
         $disposition = $part->getHeaderField('Content-Disposition');
         $filename = $part->getHeaderField('Content-Disposition', 'filename');
         $is_attachment = $disposition == 'attachment' || $filename;
         $charset = $part->getHeaderField('Content-Type', 'charset');
         switch ($ctype) {
             case 'text/plain':
                 if (!$is_attachment) {
                     $format = $part->getHeaderField('Content-Type', 'format');
                     $delsp = $part->getHeaderField('Content-Type', 'delsp');
                     $text = Mime_Helper::convertString($part->getContent(), $charset);
                     if ($format == 'flowed') {
                         $text = Mime_Helper::decodeFlowedBodies($text, $delsp);
                     }
                     $parts['text'][] = $text;
                 }
                 break;
             case 'text/html':
                 if (!$is_attachment) {
                     $parts['html'][] = Mime_Helper::convertString($part->getContent(), $charset);
                 }
                 break;
                 // special case for Apple Mail
             // special case for Apple Mail
             case 'text/enriched':
                 if (!$is_attachment) {
                     $parts['html'][] = Mime_Helper::convertString($part->getContent(), $charset);
                 }
                 break;
             default:
                 // avoid treating forwarded messages as attachments
                 $is_attachment |= $disposition == 'inline' && $ctype != 'message/rfc822';
                 // handle inline images
                 $type = current(explode('/', $ctype));
                 $is_attachment |= $type == 'image';
                 if (!$is_attachment) {
                     $parts['text'][] = $part->getContent();
                 }
         }
     }
     // now we have $parts with type 'text' and type 'html'
     if (isset($parts['text'])) {
         return implode("\n\n", $parts['text']);
     }
     if (isset($parts['html'])) {
         $str = implode("\n\n", $parts['html']);
         // hack for inotes to prevent content from being displayed all on one line.
         $str = str_replace('</DIV><DIV>', "\n", $str);
         $str = str_replace(array('<br>', '<br />', '<BR>', '<BR />'), "\n", $str);
         // XXX: do we also need to do something here about base64 encoding?
         $str = strip_tags($str);
         // convert html entities. this should be done after strip tags
         $str = html_entity_decode($str, ENT_QUOTES, APP_CHARSET);
         return $str;
     }
     return null;
 }