I made a cleaned up version of the example, but did not run it:
function create_part_array($structure, $prefix = '')
{
// There some sub parts
if (count($structure->parts) > 0) {
foreach ($structure->parts as $count => $part) {
add_part_to_array($part, $prefix . ($count + 1), $part_array);
}
return $part_array;
}
// Email does not have a separate mime attachment for text
$part_array[] = ['part_number' => $prefix . '1', 'part_object' => $obj];
return $part_array;
}
function add_part_to_array($obj, $partNo, &$part_array)
{
$part_array[] = ['part_number' => $partNo, 'part_object' => $obj];
// Check to see if the part is an attached email message, as in the RFC-822 type
if ($obj->type === 2) {
// Check to see if the email has parts
if (count($obj->parts) > 0) {
foreach ($obj->parts as $index => $part) {
// Iterate here again to compensate for the broken way that imap_fetchbody() handles attachments
if (count($part->parts) > 0) {
foreach ($part->parts as $subIndex => $subPart) {
add_part_to_array($subPart, $partNo . '.' . ($subIndex + 1), $part_array);
}
} else { // Attached email does not have a separate mime attachment for text
$part_array[] = ['part_number' => $partNo . '.' . ($index + 1), 'part_object' => $obj];
}
}
return;
}
// Not sure if this is possible
$part_array[] = ['part_number' => $prefix . '.1', 'part_object' => $obj];
return;
}
// If there are more sub-parts, expand them out.
if (count($obj->parts) > 0) {
foreach ($obj->parts as $count => $p) {
add_part_to_array($p, $partNo . '.' . ($count + 1), $part_array);
}
}
}
I finally did not use this code, only the basic logic
See: http://sgerwk.altervista.org/imapbodystructure.html
The code to handle parts in parts is present on this PHP example: https://www.php.net/manual/fr/function.imap-fetchbody.php#89002
I am working on a fix
I made a cleaned up version of the example, but did not run it:
I finally did not use this code, only the basic logic