Fetch message parts into a flat array with PHP IMAP
The PHP IMAP functions imap_fetchstructure and imap_fetchbody are used to work out the structure of an email and get the message body and attachments, but they can be fiddly to use because the message parts can be nested. This post has a function which effectively flattens the message parts into a new array, indexed by the part number which can be directly passed to imap_fetchbody.