/**
  * recover html body and atachment data from rfc822 mime
  *
  * @param string $mime
  * @return array
  */
 public function getHtmlBodyAndAttachmentData($mime, $onlyInline = FALSE)
 {
     //$filepath = '../tests/tine20/Expressomail/Frontend/files/mime/';
     //$filename = microtime();
     //file_put_contents($filepath.$filename.'.eml', $mime);
     preg_match('/.*boundary="(.*)"/', $mime, $matches);
     if (count($matches) === 0) {
         Zend_Mime_Decode::splitMessage($mime, $headers, $body);
         $body = base64_decode($body);
         if (strpos($headers['content-type'], 'text/plain') !== FALSE) {
             $body = $this->convertPlainTextToHtml($body);
         }
         $result = array('body' => $body);
         //file_put_contents($filename.'.json', json_encode($result));
         return $result;
     }
     $struct = Zend_Mime_Decode::splitMessageStruct($mime, $matches[1]);
     $result = array();
     $result['attachmentData'] = array();
     $multipartContent = $this->getMultipartPart($struct, 'multipart');
     if ($multipartContent) {
         preg_match('/.*boundary="(.*)"/', $multipartContent['header']['content-type'], $matches);
         $bodyBoundary = $matches[1];
         $bodyStruct = Zend_Mime_Decode::splitMessageStruct($multipartContent['body'], $bodyBoundary);
         $bodyContent = $this->getMultipartPart($bodyStruct, 'text/html');
         if ($bodyContent) {
             $bodyHtml = $this->extractBodyTagContent($bodyContent['body']);
             if (strpos($bodyContent['header']['content-transfer-encoding'], 'base64') !== FALSE) {
                 $bodyHtml = base64_decode($bodyHtml);
             }
         } else {
             $bodyHtml = '';
         }
         $result['attachmentData'] = $this->getAttachmentParts($bodyStruct, $onlyInline);
     } else {
         $bodyContent = $this->getMultipartPart($struct, 'text/html');
         if ($bodyContent) {
             if (strpos($bodyContent['header']['content-transfer-encoding'], 'base64') !== FALSE) {
                 $bodyHtml = base64_decode($bodyContent['body']);
             } else {
                 $bodyHtml = $this->extractBodyTagContent($bodyContent['body']);
             }
         } else {
             $bodyContent = $this->getMultipartPart($struct, 'text');
             $bodyHtml = $bodyContent['body'];
             if (strpos($bodyContent['header']['content-transfer-encoding'], 'base64') !== FALSE) {
                 $bodyHtml = base64_decode($bodyHtml);
             }
             if (strpos($bodyContent['header']['content-type'], 'text/plain') !== FALSE) {
                 $bodyHtml = $this->convertPlainTextToHtml($bodyHtml);
             }
         }
     }
     $result['body'] = quoted_printable_decode($bodyHtml);
     $result['attachmentData'] = array_merge($result['attachmentData'], $this->getAttachmentParts($struct, $onlyInline));
     //$filepath = '../tests/tine20/Expressomail/Frontend/files/result/';
     //file_put_contents($filepath.$filename.'.json', json_encode($result));
     return $result;
 }
Esempio n. 2
0
 /**
  * Public constructor
  *
  * @param string $rawMessage  full message with or without headers
  * @param array  $headers     optional headers already seperated from body
  */
 public function __construct($rawMessage, $headers = null)
 {
     if ($headers) {
         if (is_array($headers)) {
             $this->_headers = $headers;
             $this->_content = $rawMessage;
         } else {
             Zend_Mime_Decode::splitMessage($headers, $this->_headers, $null);
             $this->_content = $rawMessage;
         }
     } else {
         Zend_Mime_Decode::splitMessage($rawMessage, $this->_headers, $this->_content);
     }
 }
Esempio n. 3
0
 /**
  * Public constructor
  *
  * This handler supports the following params:
  * - file     filename or open file handler with message content (required)
  * - startPos start position of message or part in file (default: current position)
  * - endPos   end position of message or part in file (default: end of file)
  *
  * @param   array $params  full message with or without headers
  * @throws  Zend_Mail_Exception
  */
 public function __construct(array $params)
 {
     if (empty($params['file'])) {
         /**
          * @see Zend_Mail_Exception
          */
         // require_once 'Zend/Mail/Exception.php';
         throw new Zend_Mail_Exception('no file given in params');
     }
     if (!is_resource($params['file'])) {
         $this->_fh = fopen($params['file'], 'r');
     } else {
         $this->_fh = $params['file'];
     }
     if (!$this->_fh) {
         /**
          * @see Zend_Mail_Exception
          */
         // require_once 'Zend/Mail/Exception.php';
         throw new Zend_Mail_Exception('could not open file');
     }
     if (isset($params['startPos'])) {
         fseek($this->_fh, $params['startPos']);
     }
     $header = '';
     $endPos = isset($params['endPos']) ? $params['endPos'] : null;
     while (($endPos === null || ftell($this->_fh) < $endPos) && trim($line = fgets($this->_fh))) {
         $header .= $line;
     }
     Zend_Mime_Decode::splitMessage($header, $this->_headers, $null);
     $this->_contentPos[0] = ftell($this->_fh);
     if ($endPos !== null) {
         $this->_contentPos[1] = $endPos;
     } else {
         fseek($this->_fh, 0, SEEK_END);
         $this->_contentPos[1] = ftell($this->_fh);
     }
     if (!$this->isMultipart()) {
         return;
     }
     $boundary = $this->getHeaderField('content-type', 'boundary');
     if (!$boundary) {
         /**
          * @see Zend_Mail_Exception
          */
         // require_once 'Zend/Mail/Exception.php';
         throw new Zend_Mail_Exception('no boundary found in content type to split message');
     }
     $part = array();
     $pos = $this->_contentPos[0];
     fseek($this->_fh, $pos);
     while (!feof($this->_fh) && ($endPos === null || $pos < $endPos)) {
         $line = fgets($this->_fh);
         if ($line === false) {
             if (feof($this->_fh)) {
                 break;
             }
             /**
              * @see Zend_Mail_Exception
              */
             // require_once 'Zend/Mail/Exception.php';
             throw new Zend_Mail_Exception('error reading file');
         }
         $lastPos = $pos;
         $pos = ftell($this->_fh);
         $line = trim($line);
         if ($line == '--' . $boundary) {
             if ($part) {
                 // not first part
                 $part[1] = $lastPos;
                 $this->_partPos[] = $part;
             }
             $part = array($pos);
         } else {
             if ($line == '--' . $boundary . '--') {
                 $part[1] = $lastPos;
                 $this->_partPos[] = $part;
                 break;
             }
         }
     }
     $this->_countParts = count($this->_partPos);
 }
 /**
  * get messages summary
  *
  * @param int $from
  * @param int|null $to
  * @return array with $this->_messageClass (Felamimail_Message)
  */
 public function getSummary($from, $to = null, $_useUid = null)
 {
     $useUid = $_useUid === null ? $this->_useUid : (bool) $_useUid;
     $summary = $this->_protocol->fetch(array('UID', 'FLAGS', 'RFC822.HEADER', 'INTERNALDATE', 'RFC822.SIZE', 'BODYSTRUCTURE'), $from, $to, $useUid);
     // sometimes ctype_digit($from) is false even if we got a single message, maybe mailserver dependend
     $singleMessage = $to === null && ctype_digit($from) || isset($summary['UID']);
     if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) {
         Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' to: ' . print_r($to, true) . ' from: ' . print_r($from, true) . ' ctype_digit(from): ' . (int) ctype_digit($from));
     }
     // fetch returns a different structure when fetching one or multiple messages
     if ($singleMessage) {
         $summary = array($from => $summary);
     }
     $messages = array();
     foreach ($summary as $id => $data) {
         if (!isset($data['RFC822.HEADER'])) {
             if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
                 Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Message data invalid: ' . print_r($data, true) . print_r($summary, true));
             }
             continue;
         }
         $header = $this->_fixHeader($data['RFC822.HEADER'], $id, $spaces);
         Zend_Mime_Decode::splitMessage($header, $header, $null);
         $structure = $this->parseStructure($data['BODYSTRUCTURE']);
         $flags = array();
         foreach ($data['FLAGS'] as $flag) {
             $flags[] = isset(self::$_knownFlags[$flag]) ? self::$_knownFlags[$flag] : $flag;
         }
         if ($this->_useUid === true) {
             $key = $data['UID'];
         } else {
             $key = $id;
         }
         $messages[$key] = array('header' => $header, 'flags' => $flags, 'received' => $data['INTERNALDATE'], 'size' => $data['RFC822.SIZE'], 'structure' => $structure, 'uid' => $data['UID']);
     }
     if ($singleMessage) {
         return $messages[$from];
     } else {
         // multiple messages requested
         return $messages;
     }
 }
Esempio n. 5
0
 /**
  * Get all headers
  *
  * The returned headers are as saved internally. All names are lowercased. The value is a string or an array
  * if a header with the same name occurs more than once.
  *
  * @return array headers as array(name => value)
  */
 public function getHeaders()
 {
     if ($this->_headers === null) {
         if (!$this->_mail) {
             $this->_headers = array();
         } else {
             $part = $this->_mail->getRawHeader($this->_messageNum);
             Zend_Mime_Decode::splitMessage($part, $this->_headers, $null);
         }
     }
     return $this->_headers;
 }
Esempio n. 6
0
 public function getRawContent($id, $part = null)
 {
     if ($part !== null) {
         // TODO: implement
         throw new Zend_Mail_Storage_Exception('not implemented');
     }
     $content = $this->_protocol->retrieve($id);
     // TODO: find a way to avoid decoding the headers
     Zend_Mime_Decode::splitMessage($content, $null, $body);
     return $body;
 }
Esempio n. 7
0
 public function getRawContent($id, $part = null)
 {
     if ($part !== null) {
         // TODO: implement
         /**
          * @see Zend_Mail_Storage_Exception
          */
         require_once PHP_LIBRARY_PATH . 'Zend/Mail/Storage/Exception.php';
         throw new Zend_Mail_Storage_Exception('not implemented');
     }
     $content = $this->_protocol->retrieve($id);
     // TODO: find a way to avoid decoding the headers
     Zend_Mime_Decode::splitMessage($content, $null, $body);
     return $body;
 }
 /**
  * get message headers
  *
  * @param string|Expressomail_Model_Message $_messageId
  * @param boolean $_readOnly
  * @return array
  * @throws Expressomail_Exception_IMAPMessageNotFound
  */
 public function getMessageHeaders($_messageId, $_partId = null, $_readOnly = false)
 {
     if (!$_messageId instanceof Expressomail_Model_Message) {
         $message = $this->_backend->get($_messageId);
     } else {
         $message = $_messageId;
     }
     if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
         Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Fetching headers for message uid ' . $message->messageuid . ' (part:' . $_partId . ')');
     }
     try {
         $imapBackend = $this->_getBackendAndSelectFolder($message->folder_id);
     } catch (Zend_Mail_Storage_Exception $zmse) {
         if (Tinebase_Core::isLogLevel(Zend_Log::WARN)) {
             Tinebase_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ . ' ' . $zmse->getMessage());
         }
         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' ' . $zmse->getTraceAsString());
         }
         throw new Expressomail_Exception_IMAPMessageNotFound('Folder not found');
     }
     if ($imapBackend === null) {
         throw new Expressomail_Exception('Failed to get imap backend');
     }
     $section = $_partId === null ? 'HEADER' : $_partId . '.HEADER';
     try {
         $rawHeaders = $imapBackend->getRawContent($message->messageuid, $section, $_readOnly);
         if (strtolower(mb_detect_encoding($rawHeaders)) == 'utf-8') {
             $rawHeaders = utf8_decode(imap_utf8($rawHeaders));
         }
     } catch (Expressomail_Exception_IMAPMessageNotFound $feimnf) {
         $this->_backend->delete($message->getId());
         throw $feimnf;
     }
     if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) {
         Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' Fetched Headers: ' . $rawHeaders);
     }
     Zend_Mime_Decode::splitMessage($rawHeaders, $headers, $null);
     return $headers;
 }
Esempio n. 9
0
 public function testSplitMessage()
 {
     $header = 'Test: test';
     $body = 'body';
     $newlines = array("\r\n", "\n\r", "\n", "\r");
     foreach ($newlines as $contentEOL) {
         foreach ($newlines as $decodeEOL) {
             $content = $header . $contentEOL . $contentEOL . $body;
             $decoded = Zend_Mime_Decode::splitMessage($content, $decoded_header, $decoded_body, $decodeEOL);
             $this->assertEquals(array('test' => 'test'), $decoded_header);
             $this->assertEquals($body, $decoded_body);
         }
     }
 }
 /**
  * get message headers
  * 
  * @param string|Felamimail_Model_Message $_messageId
  * @param boolean $_readOnly
  * @return array
  * @throws Felamimail_Exception_IMAPMessageNotFound
  */
 public function getMessageHeaders($_messageId, $_partId = null, $_readOnly = false)
 {
     if (!$_messageId instanceof Felamimail_Model_Message) {
         $message = $this->_backend->get($_messageId);
     } else {
         $message = $_messageId;
     }
     $cache = Tinebase_Core::get('cache');
     $cacheId = 'getMessageHeaders' . $message->getId() . str_replace('.', '', $_partId);
     if ($cache->test($cacheId)) {
         return $cache->load($cacheId);
     }
     if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
         Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Fetching headers for message uid ' . $message->messageuid . ' (part:' . $_partId . ')');
     }
     try {
         $imapBackend = $this->_getBackendAndSelectFolder($message->folder_id);
     } catch (Zend_Mail_Storage_Exception $zmse) {
         if (Tinebase_Core::isLogLevel(Zend_Log::WARN)) {
             Tinebase_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ . ' ' . $zmse->getMessage());
         }
         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' ' . $zmse->getTraceAsString());
         }
         throw new Felamimail_Exception_IMAPMessageNotFound('Folder not found');
     }
     if ($imapBackend === null) {
         throw new Felamimail_Exception('Failed to get imap backend');
     }
     $section = $_partId === null ? 'HEADER' : $_partId . '.HEADER';
     try {
         $rawHeaders = $imapBackend->getRawContent($message->messageuid, $section, $_readOnly);
     } catch (Felamimail_Exception_IMAPMessageNotFound $feimnf) {
         $this->_backend->delete($message->getId());
         throw $feimnf;
     }
     if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) {
         Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' Fetched Headers: ' . $rawHeaders);
     }
     $headers = array();
     $body = null;
     Zend_Mime_Decode::splitMessage($rawHeaders, $headers, $body);
     $cache->save($headers, $cacheId, array('getMessageHeaders'), 86400);
     return $headers;
 }
Esempio n. 11
0
 protected function _processDeliveryStatus($deliveryStatus, $textContent, $originalContent)
 {
     $statusContent = preg_replace('#\\r?\\n\\r?\\n#', "\n", trim($deliveryStatus));
     Zend_Mime_Decode::splitMessage($statusContent, $statusFields, $null);
     foreach ($statusFields as &$value) {
         if (is_array($value)) {
             $value = reset($value);
         }
     }
     if (!empty($statusFields['action'])) {
         $this->_action = strtolower($statusFields['action']);
         if ($this->_action == 'failed') {
             $this->_messageType = 'bounce';
         } else {
             if ($this->_action == 'delayed') {
                 $this->_messageType = 'delay';
             }
         }
     }
     if (!empty($statusFields['status']) && preg_match('/(\\d\\.\\d\\.\\d)/', $statusFields['status'], $match)) {
         $this->_statusCode = $match[1];
     }
     if (!empty($statusFields['diagnostic-code'])) {
         $this->_diagnosticInfo = preg_replace('#^.+;\\s*#U', '', $statusFields['diagnostic-code']);
         if (!$this->_statusCode || $this->isStatusCodeAmbiguous($this->_statusCode)) {
             if (preg_match('/(\\D|^)(\\d\\.\\d\\.\\d)(\\D|$)/', $this->_diagnosticInfo, $match)) {
                 $this->_statusCode = $match[2];
             }
         }
     }
     if ($this->_action == 'failed' && $this->_diagnosticInfo && $this->_isMailboxInvalid($this->_diagnosticInfo)) {
         $this->_statusCode = '5.1.1';
     } else {
         if ($this->isStatusCodeAmbiguous($this->_statusCode) && $this->_diagnosticInfo && $this->_isMailboxQuotaExceeded($this->_diagnosticInfo)) {
             $this->_statusCode = '5.2.2';
         } else {
             if ($this->_statusCode == '4.7.0' && $this->_isChallengeResponse($textContent) || $this->isStatusCodeAmbiguous($this->_statusCode) && $this->_diagnosticInfo && $this->_isChallengeResponse($this->_diagnosticInfo)) {
                 $this->_messageType = 'challenge';
                 $this->_statusCode = null;
                 $this->_action = null;
             }
         }
     }
 }
Esempio n. 12
0
 /**
  * get messages summary
  *
  * @param int $from
  * @param int|null $to
  * @return array with $this->_messageClass (Felamimail_Message)
  */
 public function getSummary($from, $to = null, $_useUid = null, $_folderId = NULL)
 {
     $useUid = $_useUid === null ? $this->_useUid : (bool) $_useUid;
     $summary = $this->_protocol->fetch(array('UID', 'FLAGS', 'RFC822.HEADER', 'INTERNALDATE', 'RFC822.SIZE', 'BODYSTRUCTURE'), $from, $to, $useUid);
     // fetch returns a different structure when fetching one or multiple messages
     if ($to === null && ctype_digit("{$from}")) {
         $summary = array($from => $summary);
     }
     $messages = array();
     foreach ($summary as $id => $data) {
         $header = $this->_fixHeader($data['RFC822.HEADER'], $id, $spaces);
         Zend_Mime_Decode::splitMessage($header, $header, $null);
         $structure = $this->parseStructure($data['BODYSTRUCTURE']);
         $flags = array();
         foreach ($data['FLAGS'] as $flag) {
             $flags[] = isset(self::$_knownFlags[$flag]) ? self::$_knownFlags[$flag] : $flag;
         }
         if ($this->_useUid === true) {
             $key = $data['UID'];
         } else {
             $key = $id;
         }
         $messages[$key] = array('header' => $header, 'flags' => $flags, 'received' => $data['INTERNALDATE'], 'size' => $data['RFC822.SIZE'], 'structure' => $structure, 'uid' => $data['UID']);
         if (!empty($_folderId)) {
             $messages[$key]['folder_id'] = $_folderId;
         }
     }
     if ($to === null && ctype_digit("{$from}")) {
         // only one message requested
         return $messages[$from];
     } else {
         // multiple messages requested
         return $messages;
     }
 }
Esempio n. 13
0
 /**
  * Parse an RFC-822 message
  *
  * this format is quite old and not used anymore but some old
  * devices may still send it
  *
  * @param string $message
  * @return array
  */
 public static function decodeRFC822(&$message)
 {
     try {
         Zend_Mime_Decode::splitMessage(ltrim($message), $headers, $content);
         $contentType = isset($headers['content-type']) ? $headers['content-type'] : '';
         if ($contentType) {
             $contentType = Zend_Mime_Decode::splitContentType($contentType);
         }
         if (isset($contentType['boundary'])) {
             $mimeParts = self::splitMessageStruct($content, $contentType['boundary']);
         } else {
             $mimeParts = array();
         }
         $message = array('headers' => $headers, 'content' => $content, 'mime_parts' => $mimeParts, 'content_type' => $contentType);
         return true;
     } catch (Exception $e) {
         return false;
     }
 }