How can one determine the message type in PHP when fetching email content using imap_fetchstructure()?

When fetching email content using imap_fetchstructure() in PHP, you can determine the message type by checking the value of the "type" property in the returned structure. This property will indicate whether the message is a text, HTML, multipart, or other type of message. By examining this property, you can easily identify the message type and process the email content accordingly.

$mailbox = imap_open("{mail.example.com:993/imap/ssl}INBOX", "username", "password");

$emails = imap_search($mailbox, 'UNSEEN');

foreach ($emails as $email) {
    $structure = imap_fetchstructure($mailbox, $email);

    if ($structure->type == 0) {
        echo "Text message\n";
    } elseif ($structure->type == 1) {
        echo "HTML message\n";
    } elseif ($structure->type == 2) {
        echo "Multipart message\n";
    } else {
        echo "Other type of message\n";
    }
}

imap_close($mailbox);