/** * Instantiate from raw message string, Restore body to Mime\Message * * @param string $rawMessage * @return Message */ public static function fromString($rawMessage) { $message = new static(); $headers = null; $content = null; Decode::splitMessage($rawMessage, $headers, $content); if ($headers->has('mime-version')) { $boundary = $headers->get('contenttype')->getParameter('boundary'); if ($boundary) { $content = ZendMimeMessage::createFromMessage($content, $boundary); } } if ($headers->has('content-transfer-encoding')) { if ($headers->get('contenttransferencoding')->getTransferEncoding() == 'base64') { $content = base64_decode($content); } } $message->setHeaders($headers); $message->setBody($content); return $message; }
/** * Instantiate from raw message string * * @todo Restore body to Mime\Message * @param string $rawMessage * @return Message */ public static function fromString($rawMessage) { $message = new static(); $headers = null; $content = null; Mime\Decode::splitMessage($rawMessage, $headers, $content); if ($headers->has('mime-version')) { // todo - restore body to mime\message } $message->setHeaders($headers); $message->setBody($content); return $message; }
public function testSplitMessage() { $header = 'Test: test'; $body = 'body'; $newlines = array("\r\n", "\n\r", "\n", "\r"); $decoded_body = null; // "Declare" variable befor first "read" usage to avoid IDEs warning $decoded_header = null; // "Declare" variable befor first "read" usage to avoid IDEs warning foreach ($newlines as $contentEOL) { foreach ($newlines as $decodeEOL) { $content = $header . $contentEOL . $contentEOL . $body; $decoded = Mime\Decode::splitMessage($content, $decoded_header, $decoded_body, $decodeEOL); $this->assertEquals(array('test' => 'test'), $decoded_header); $this->assertEquals($body, $decoded_body); } } }
/** * Public constructor * * Part supports different sources for content. The possible params are: * - handler an instance of AbstractStorage for late fetch * - id number of message for handler * - raw raw content with header and body as string * - headers headers as array (name => value) or string, if a content part is found it's used as toplines * - noToplines ignore content found after headers in param 'headers' * - content content as string * - strict strictly parse raw content * * @param array $params full message with or without headers * @throws Exception\InvalidArgumentException */ public function __construct(array $params) { if (isset($params['handler'])) { if (!$params['handler'] instanceof AbstractStorage) { throw new Exception\InvalidArgumentException('handler is not a valid mail handler'); } if (!isset($params['id'])) { throw new Exception\InvalidArgumentException('need a message id with a handler'); } $this->mail = $params['handler']; $this->messageNum = $params['id']; } $params['strict'] = isset($params['strict']) ? $params['strict'] : false; if (isset($params['raw'])) { Mime\Decode::splitMessage($params['raw'], $this->headers, $this->content, Mime\Mime::LINEEND, $params['strict']); } elseif (isset($params['headers'])) { if (is_array($params['headers'])) { $this->headers = new Headers(); $this->headers->addHeaders($params['headers']); } else { if (empty($params['noToplines'])) { Mime\Decode::splitMessage($params['headers'], $this->headers, $this->topLines); } else { $this->headers = Headers::fromString($params['headers']); } } if (isset($params['content'])) { $this->content = $params['content']; } } }
/** * 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'])) { throw new 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) { throw new 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; } $body = null; // "Declare" variable since it's passed by reference Mime\Decode::splitMessage($header, $this->_headers, $body); $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) { throw new 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; } throw new 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); }
public function getRawContent($id, $part = null) { if ($part !== null) { // TODO: implement throw new Exception\RuntimeException('not implemented'); } $content = $this->protocol->retrieve($id); // TODO: find a way to avoid decoding the headers $headers = null; // "Declare" variable since it's passed by reference $body = null; // "Declare" variable before first usage. Mime\Decode::splitMessage($content, $headers, $body); return $body; }
/** * 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); $body = null; // "Declare" variable since it's passed by reference Mime\Decode::splitMessage($part, $this->_headers, $body); } } return $this->_headers; }