从Gmail获取电子邮件详细信息并保存到Mysql数据库中

I want to get emails detail and save into MySQL table. The data I need is the email id, Subject, Body and date. I google it and I find a script but I am getting only Body. I want to get daily emails data in my Mysql table.

Here is the code :

<?php
$hostname = "{imap.gmail.com:993/imap/ssl}INBOX";
$username = "testmail@gmail.com";
$password = "password";
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
$emails = imap_search($inbox,'UNSEEN');
if($emails) {
    $output = '';
    rsort($emails);
    foreach($emails as $email_number) {
        $message = (imap_fetchbody($inbox,$email_number,1.1)); 
        if($message == '')
        {
            $message = (imap_fetchbody($inbox,$email_number,1));
        }
        $array = explode("
", $message);
    }
    print_r($array);
}
imap_close($inbox);
?>

Any help would be highly appreciable.