如果不支持imap fetch mime,如何通过php imap获取charset

I'm wondering how to get the charset via PHP IMAP when imap_fetchmime() isn't supported. I either have a lower version of PHP that doesn't support it or maybe it's deprecated, but it doesn't work for me.

What I basically need is to find out the charset of the email via IMAP, so the output would be:

ISO-8859-1

Or whatever it turns out to be. I don't want to read the whole email to find out, I want to find out what the charset is so I can use the correct encoding.

Here is the code I'm using simplified down for the purpose of this email:

$mailbox = "xxx@gmail.com";
$mailboxPassword = "xxx";

$mailbox = imap_open("{imap.gmail.com:993/imap/ssl}INBOX",
mailbox, $mailboxPassword);

mb_internal_encoding("UTF-8");
$subject = mb_decode_mimeheader(str_replace('_', ' ', $subject));

$body = imap_fetchbody($mailbox, $val, 1);
$body = base64_decode($body);

echo $body;