private function getDefaultPid(string $mimeType = 'text/html', $attempt = 1) : string
{
$mimeCheck = 'text/html' == $mimeType ? ['text/html', 'text/plain'] : ['text/plain', 'text/html'];
// Tries to find text/html or text/plain in main parts
foreach ($mimeCheck as $mime) {
$parts = array_keys($this->structure['ftype'], $mime);
foreach ($parts as $part) {
if ('inline' == $this->structure['disposition'][$part] && false === strpos($this->structure['pid'][$part], '.')) {
return $this->structure['pid'][$part];
}
}
}
// There was nothing found in the main parts, try multipart/alternative or multipart/report
$partLevel = 1;
$pidLength = 1;
foreach ($this->structure['pid'] as $partNo => $pid) {
if ($pid === null) {
continue;
}
$level = count(explode('.', $pid));
if (!isset($multipartPid)) {
if (1 === $level && isset($this->structure['ftype'][$partNo]) && $this->isPartMultipart($partNo, 'related')) {
$partLevel = 2;
$pidLength = 3;
continue;
}
if ($level == $partLevel && isset($this->structure['ftype'][$partNo]) && ($this->isPartMultipart($partNo, 'alternative') || $this->isPartMultipart($partNo, 'report') || $this->isPartMultipart($partNo, 'mixed'))) {
$multipartPid = $pid;
continue;
}
}
if (isset($multipartPid) && $level == $partLevel + 1 && $this->structure['ftype'][$partNo] == $mimeType && $multipartPid == substr($pid, 0, $pidLength)) {
return $pid;
}
}
// Nothing was found, try next possible type
if (1 === $attempt) {
if ('text/html' == $mimeType) {
return $this->getDefaultPid('text/plain', 2);
} else {
return $this->getDefaultPid('text/html', 2);
}
} else {
// There should be a default part found in every mail; this is because of spams that are often in wrong format
return '1';
}
}