public function getBody(string $pid = '1', int $mode = self::BODY, string $mimeType = 'text/html', int $attempt = 1) : array
{
try {
$this->checkIfParsed();
} catch (\Jyxo\Mail\Parser\EmailNotExistException $e) {
throw $e;
}
$key = array_search($pid, $this->structure['pid']);
if (false === $key) {
throw new Parser\PartNotExistException('Requested part does not exist');
}
$output['encoding'] = $this->structure['encoding'][$key];
$output['type'] = $this->structure['ftype'][$key];
$output['size'] = $this->structure['fsize'][$key];
if (isset($this->structure['fname'][$key])) {
$output['filename'] = $this->structure['fname'][$key];
}
if (isset($this->structure['charset'][$key])) {
$output['charset'] = $this->structure['charset'][$key];
}
if (isset($this->structure['cid'][$key])) {
$output['cid'] = $this->structure['cid'][$key];
}
if (self::BODY_INFO === $mode) {
return $output;
}
if (self::BODY_LITERAL === $mode) {
$output['content'] = imap_fetchbody($this->connection, $this->uid, $pid, FT_UID);
return $output;
}
if (self::BODY_LITERAL_DECODE === $mode) {
$output['content'] = self::decodeBody(imap_fetchbody($this->connection, $this->uid, $pid, FT_UID), $output['encoding']);
// Textual types are converted to UTF-8
if (0 === strpos($output['type'], 'text/') || 0 === strpos($output['type'], 'message/')) {
$output['content'] = $this->convertToUtf8($output['content'], isset($output['charset']) ? $output['charset'] : '');
}
return $output;
}
// Get a new part number
if ('message/rfc822' == $this->structure['ftype'][$key] || $this->isPartMultipart($key, 'related') || $this->isPartMultipart($key, 'alternative') || $this->isPartMultipart($key, 'report')) {
$newPid = 'message/rfc822' == $this->structure['ftype'][$key] || $this->isPartMultipart($key, 'related') || $this->isPartMultipart($key, 'alternative') || $this->isPartMultipart($key, 'report') ? $this->getMultipartPid($pid, $mimeType, 'subparts') : $this->getMultipartPid($pid, $mimeType, 'multipart');
// If no type was found, try again
if (!empty($newPid)) {
$pid = $newPid;
} elseif (empty($newPid) && 'text/html' == $mimeType) {
if (1 === $attempt) {
return $this->getBody($pid, $mode, 'text/plain', 2);
}
} elseif (empty($newPid) && 'text/plain' == $mimeType) {
if (1 === $attempt) {
return $this->getBody($pid, $mode, 'text/html', 2);
}
}
}
if (!empty($newPid)) {
$key = array_search($pid, $this->structure['pid']);
if (false === $key) {
throw new Parser\PartNotExistException('Requested part does not exist');
}
}
$output['encoding'] = $this->structure['encoding'][$key];
$output['type'] = $this->structure['ftype'][$key];
$output['size'] = $this->structure['fsize'][$key];
if (isset($this->structure['fname'][$key])) {
$output['filename'] = $this->structure['fname'][$key];
}
if (isset($this->structure['charset'][$key])) {
$output['charset'] = $this->structure['charset'][$key];
}
$output['content'] = self::decodeBody(imap_fetchbody($this->connection, $this->uid, $pid, FT_UID), $output['encoding']);
// Textual types are converted to UTF-8
if (0 === strpos($output['type'], 'text/') || 0 === strpos($output['type'], 'message/')) {
$output['content'] = $this->convertToUtf8($output['content'], isset($output['charset']) ? $output['charset'] : '');
}
return $output;
}