for Imap_fetchheader, it need to use the UID of the message.
Try to use this:
$number = imap_num_msg($mail);
for ($x=0; $x<count($number); $x++) {
$uid = imap_uid ($mail, $number[$x]);
$header = imap_fetchheader($mail,$uid);
var_dump($header)
}
try to check whether the imap_uid is working, because imap_fetchheader() need the $uid of the particular Unique ID of the message.