views:

128

answers:

1

I'm writing a help desk pipe handler to pipe incoming e-mails as helpdesk ticket replies. Some e-mails are coming in perfectly fine, others are coming in as a jumble of the text and =3D's all munged into one giant string. Does anyone have an idea on how to decode that into plain text.

For reference, this is my mail parser function:

public function parseEmailMessage(Zend_Mail_Message $msg)
{
 if ($msg->isMultiPart()) {
  $arrAttachments = array();
  $body = '';
  // Multipart Mime Message
  foreach (new RecursiveIteratorIterator($msg) as $part) {
      try {

       $mimeType = strtok($part->contentType, ';');

       // Parse file name
       preg_match('/name="(?<filename>[a-zA-Z0-9.\-_]+)"/is', $part->contentType, $attachmentName);

       // Append plaintext results to $body
       // All other content parts will be treated as attachments
       switch ($mimeType) {
        case 'text/plain':
         $body .= trim($part->getContent()) . "\n";
         break;
        case 'text/html':
         $body .= trim(strip_tags($part->getContent));
         break;
        default:
         $arrAttachments[] = array(
          'attachment_mime' => $mimeType,
          'attachment_name' => $this->filterFileName($attachmentName['filename']),
          'base64data' => trim($part->getContent())
         );
       }

      } catch (Zend_Mail_Exception $e) {
          // ignore
      }
  }

  return array($body, $arrAttachments);
 } else {
  // Plain text message
  return array(trim($msg->getContent()), array());
 }
}
+3  A: 

I'll take a guess that somehow the content type is not correctly specified and Zend doesn't know how to decode it. I know I've seen this before, but I can't remember where or how it was 'solved'.

It looks like quoted-printable being treated like plain text.

Tim Lytle
This happens to some of our scripts too - Apple Macs are to blame if I remember correctly
David Caunt
exactly what i was looking for. the function in php is called quoted_printable_decode(); you saved me hours of frustration, thank you
Mark