/** * Gets a string contains the message body content * * @param bool $format The required format of the body. Can be FORMAT_TEXT or FORMAT_HTML * @return Content * @throws Exception\InvalidBodyFormatException */ public function getContent($format = Body::FORMAT_TEXT) { if (!$this->part->isMultipart()) { if ($format === Body::FORMAT_TEXT) { return $this->extractContent($this->part); } } else { foreach ($this->part as $part) { $contentTypeHeader = $this->getPartContentType($part); if ($contentTypeHeader !== null) { if ($format === Body::FORMAT_TEXT && $contentTypeHeader->getType() === 'text/plain') { return $this->extractContent($part); } elseif ($format === Body::FORMAT_HTML && $contentTypeHeader->getType() === 'text/html') { return $this->extractContent($part); } } } } throw new Exception\InvalidBodyFormatException(sprintf('A messages does not have %s content.', $format === Body::FORMAT_TEXT ? 'TEXT' : 'HTML')); }
/** * @param Part $part * * @return string|null */ public function getPlainText(Part $part) { $text = null; if ($part->isMultipart()) { $partCount = $part->countParts(); // Parse all parts for ($i = 0; $i < $partCount; $i++) { $subPart = $part->getPart($i + 1); // Check for headers, to avoid exceptions if ($subPart->getHeaders() !== null) { if ($subPart->isMultipart()) { // Part is multipart as well $tmp = $this->getPlainText($subPart); $text = $tmp !== null ? $tmp : $text; } else { // Try to get plain/text of this content $tmp = $this->getPlainTextOfPart($subPart); $text = $tmp !== null ? $tmp : $text; } } } } else { // Its not multipart, so try to get its content $text = $this->getPlainTextOfPart($part); } return $text; }