The third parameter, the section parameter is not properly documented and I've seen a dozen or two developers in my research to figure this issue out come to hopeless conclusions. It's critical to understand that various email providers (e.g. AOL, Gmail, Microsoft, Yahoo and client programs like Thunderbird and...yes, the five thousand versions of Outlook) do NOT follow some "standard".
- Unlike JavaScript and how PHP normally function all index numbers start at 1, NOT 0.
- Your third/section parameter will be a whole number if there is not a second 'parts' array (I am NOT using classes here).
- If the 'parts' array contains a 'parts' subarray you will have floating-point numbers (e.g. 1.1 or 2.1 instead of 1 or 2). If you have two parts in the 'parts' subarray you will have something such as 1.1 and 1.2 or 2.1 and 2.2 - REMEMBER: these vary by provider and there is no "STANDARD" as this is a PHP-specific issue!
- None, some or all 'parts' arrays may be singular or contain child 'parts' subarrays, this is completely dynamic and may NOT be consistent from even the same provider!
The following presumes you have a $mail_connection and know which $email_number number you're working with:
<?php
$i = 0;$j = 0;$mail_message_structure = json_decode(json_encode(imap_fetchstructure($mail_connection, $email_number)), true);while ($i < count($mail_message_structure['parts']))
{
if (isset($mail_message_structure['parts'][$i]['parts']))
{while ($j < count($mail_message_structure['parts'][$i]['parts']))
{
$mail_message_structure['parts'][$i]['parts'][$j]['subtype'] = strtolower($mail_message_structure['parts'][$i]['parts'][$j]['subtype']);
$mail_message_structure['parts'][$i]['parts'][$j]['id_imap_fetchbody'] = ($i + 1).'.'.($j + 1);
echo '<p>'.$i.': multi, '.$j.' '.$mail_message_structure['parts'][$i]['parts'][$j]['subtype'].': '.($i + 1).'.'.($j + 1).'</p>';
$j++;
}
}
else
{$mail_message_structure['parts'][$i]['subtype'] = strtolower($mail_message_structure['parts'][$i]['subtype']);
$mail_message_structure['parts'][$i]['id_imap_fetchbody'] = ($i + 1);
echo '<p>'.$i.': single: '.$mail_message_structure['parts'][$i]['subtype'].': '.($i + 1).'</p>';
}
$i++;
}
?>
Now that we can actually understand HOW to access specific PARTS of the email body it is a simple manner to iterate over the structure array:
<?php
foreach ($mail_message_structure['parts'] as $k4 => $v4)
{
if (isset($v4['parts']))
{
foreach ($v4['parts'] as $k5 => $v5) {mail_imap_body($mail_connection, $email_number, $v5);}}
else {mail_imap_body($mail_connection, $email_number, $v4);}}
?>