/** * Decodes a MIME encoded string and returns a Postman_Zend_Mime_Message object with * all the MIME parts set according to the given string * * @param string $message * @param string $boundary * @param string $EOL EOL string; defaults to {@link Postman_Zend_Mime::LINEEND} * @throws Postman_Zend_Exception * @return Postman_Zend_Mime_Message */ public static function createFromMessage($message, $boundary, $EOL = Postman_Zend_Mime::LINEEND) { require_once 'Zend/Mime/Decode.php'; $parts = Postman_Zend_Mime_Decode::splitMessageStruct($message, $boundary, $EOL); $res = new self(); foreach ($parts as $part) { // now we build a new MimePart for the current Message Part: $newPart = new Postman_Zend_Mime_Part($part['body']); foreach ($part['header'] as $key => $value) { /** * @todo check for characterset and filename */ switch (strtolower($key)) { case 'content-type': $newPart->type = $value; break; case 'content-transfer-encoding': $newPart->encoding = $value; break; case 'content-id': $newPart->id = trim($value, '<>'); break; case 'content-disposition': $newPart->disposition = $value; break; case 'content-description': $newPart->description = $value; break; case 'content-location': $newPart->location = $value; break; case 'content-language': $newPart->language = $value; break; default: throw new Postman_Zend_Exception('Unknown header ignored for MimePart:' . $key); } } $res->addPart($newPart); } return $res; }
public function getRawContent($id, $part = null) { if ($part !== null) { // TODO: implement /** * @see Postman_Zend_Mail_Storage_Exception */ require_once 'Zend/Mail/Storage/Exception.php'; throw new Postman_Zend_Mail_Storage_Exception('not implemented'); } $content = $this->_protocol->retrieve($id); // TODO: find a way to avoid decoding the headers Postman_Zend_Mime_Decode::splitMessage($content, $null, $body); return $body; }
/** * 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 Postman_Zend_Mail_Exception */ public function __construct(array $params) { if (empty($params['file'])) { /** * @see Postman_Zend_Mail_Exception */ require_once 'Zend/Mail/Exception.php'; throw new Postman_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 Postman_Zend_Mail_Exception */ require_once 'Zend/Mail/Exception.php'; throw new Postman_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; } Postman_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 Postman_Zend_Mail_Exception */ require_once 'Zend/Mail/Exception.php'; throw new Postman_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 Postman_Zend_Mail_Exception */ require_once 'Zend/Mail/Exception.php'; throw new Postman_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); }