Get number of message parts in an email using PHP
Alright, I admit up front that this is a pretty specific problem, but hopefully some Googlers will find it useful. I recently had need for a small side project to read e-mails. Every e-mail is split up into parts; each “part” represents every separate piece of the e-mail. The plain text format, rich text or HTML formats, and attachments are all sent as parts. Problem is that there is no obvious way to quickly decipher just how many parts you have in a message. The documentation for the imap functions in PHP is also woefully inadequate. Maybe I’ll help flesh it out once this project is done. Anyway, you can ascertain the total number of parts using the results from the function imap_fetchstructure(). The parts array in the returned object contains ALL the parts of the message, including the top level used to construct the rest of the object’s data. So, this simple call is all you need…
$structure = imap_fetchstructure($mbox, $message_num);
$total_parts = sizeof($structure->parts);
```