public function testHeadersFromStringFactoryCreatesMultipleObjects() { $headers = Headers::fromString("Fake: foo-bar\r\nAnother-Fake: boo-baz"); $this->assertEquals(2, $headers->count()); $header = $headers->get('fake'); $this->assertInstanceOf('Zend\\Mail\\Header\\GenericHeader', $header); $this->assertEquals('Fake', $header->getFieldName()); $this->assertEquals('foo-bar', $header->getFieldValue()); $header = $headers->get('anotherfake'); $this->assertInstanceOf('Zend\\Mail\\Header\\GenericHeader', $header); $this->assertEquals('Another-Fake', $header->getFieldName()); $this->assertEquals('boo-baz', $header->getFieldValue()); }
/** * split a message in header and body part, if no header or an * invalid header is found $headers is empty * * The charset of the returned headers depend on your iconv settings. * * @param string|Headers $message raw message with header and optional content * @param Headers $headers output param, headers container * @param string $body output param, content of message * @param string $EOL EOL string; defaults to {@link Zend\Mime\Mime::LINEEND} * @param bool $strict enable strict mode for parsing message * @return null */ public static function splitMessage($message, &$headers, &$body, $EOL = Mime::LINEEND, $strict = false) { if ($message instanceof Headers) { $message = $message->toString(); } // check for valid header at first line $firstlinePos = strpos($message, "\n"); $firstline = $firstlinePos === false ? $message : substr($message, 0, $firstlinePos); if (!preg_match('%^[^\\s]+[^:]*:%', $firstline)) { $headers = []; // TODO: we're ignoring \r for now - is this function fast enough and is it safe to assume noone needs \r? $body = str_replace(["\r", "\n"], ['', $EOL], $message); return; } // see @ZF2-372, pops the first line off a message if it doesn't contain a header if (!$strict) { $parts = explode(':', $firstline, 2); if (count($parts) != 2) { $message = substr($message, strpos($message, $EOL) + 1); } } // find an empty line between headers and body // default is set new line if (strpos($message, $EOL . $EOL)) { list($headers, $body) = explode($EOL . $EOL, $message, 2); // next is the standard new line } elseif ($EOL != "\r\n" && strpos($message, "\r\n\r\n")) { list($headers, $body) = explode("\r\n\r\n", $message, 2); // next is the other "standard" new line } elseif ($EOL != "\n" && strpos($message, "\n\n")) { list($headers, $body) = explode("\n\n", $message, 2); // at last resort find anything that looks like a new line } else { ErrorHandler::start(E_NOTICE | E_WARNING); list($headers, $body) = preg_split("%([\r\n]+)\\1%U", $message, 2); ErrorHandler::stop(); } $headers = Headers::fromString($headers, $EOL); }
/** * Access headers collection * * Lazy-loads if not already attached. * * @return Headers */ public function getHeaders() { if (null === $this->headers) { if ($this->mail) { $part = $this->mail->getRawHeader($this->messageNum); $this->headers = Headers::fromString($part); } else { $this->headers = new Headers(); } } return $this->headers; }
/** * 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 Exception\RuntimeException * @throws Exception\InvalidArgumentException */ public function __construct(array $params) { if (empty($params['file'])) { throw new Exception\InvalidArgumentException('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 Exception\RuntimeException('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; } $this->_headers = Headers::fromString($header); $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 Exception\RuntimeException('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 Exception\RuntimeException('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); }
/** * split a message in header and body part, if no header or an * invalid header is found $headers is empty * * The charset of the returned headers depend on your iconv settings. * * @param string|Headers $message raw message with header and optional content * @param Headers $headers output param, headers container * @param string $body output param, content of message * @param string $EOL EOL string; defaults to {@link Zend_Mime::LINEEND} * @return null */ public static function splitMessage($message, &$headers, &$body, $EOL = Mime::LINEEND) { if ($message instanceof Headers) { $message = $message->toString(); } // check for valid header at first line $firstline = strtok($message, "\n"); if (!preg_match('%^[^\\s]+[^:]*:%', $firstline)) { $headers = array(); // TODO: we're ignoring \r for now - is this function fast enough and is it safe to asume noone needs \r? $body = str_replace(array("\r", "\n"), array('', $EOL), $message); return; } // find an empty line between headers and body // default is set new line if (strpos($message, $EOL . $EOL)) { list($headers, $body) = explode($EOL . $EOL, $message, 2); // next is the standard new line } else { if ($EOL != "\r\n" && strpos($message, "\r\n\r\n")) { list($headers, $body) = explode("\r\n\r\n", $message, 2); // next is the other "standard" new line } else { if ($EOL != "\n" && strpos($message, "\n\n")) { list($headers, $body) = explode("\n\n", $message, 2); // at last resort find anything that looks like a new line } else { @(list($headers, $body) = @preg_split("%([\r\n]+)\\1%U", $message, 2)); } } } $headers = Headers::fromString($headers, $EOL); }
/** * @dataProvider getEmbeddedContentIdProvider * @param $rawHeaders * @param $expected */ public function testGetEmbeddedContentId($rawHeaders, $expected) { $headers = Headers::fromString(implode(PHP_EOL, $rawHeaders), PHP_EOL); $contentIdHeader = null; $contentDispositionHeader = null; if (isset($rawHeaders['Content-ID'])) { $contentIdHeader = GenericHeader::fromString($rawHeaders['Content-ID']); } if (isset($rawHeaders['Content-Disposition'])) { $contentDispositionHeader = GenericHeader::fromString($rawHeaders['Content-Disposition']); } $this->part->expects($this->any())->method('getHeader')->willReturnMap([['Content-ID', null, $contentIdHeader], ['Content-Disposition', null, $contentDispositionHeader]]); $this->part->expects($this->any())->method('getHeaders')->willReturn($headers); $this->assertEquals($expected, $this->attachment->getEmbeddedContentId()); }