imap_fetchbody() text/html section


imap_fetchbody() text/html section

我正在尝试开发一个电子邮件提供者脚本。

使用下面的函数,电子邮件应该直接保存在MySQL数据库中,但是我只保存TEXT/HTML部分失败了。我做错了什么(可能与imap_fetchbody())?

function getEmailsImap($mailserver, $port, $user, $pass)
{
$imap = imap_open( "{" . $mailserver . ":" . $port . "}INBOX", $user, $pass );
    $check = imap_mailboxmsginfo($imap);
    $totalrows = imap_num_msg($imap);
    //iterate through all unread mails
    for ($index = 0; $index < $totalrows; $index++)
    {
        $header = imap_header($imap, $index + 1);
         //get mail subject
        $subject = $header->subject;
         //get mail sent date
        $date = date(DateTime::ISO8601 , $header->udate);
        //get email authors
        $email = "{$header->from[0]->mailbox}@{$header->from[0]->host}";
        //get body
        $body = imap_fetchbody($imap, $index+1, "1.2"); /*** I think this might be the mistake **/
        //get user
        $to = $header->to[0]->mailbox;
        $user = explode("@", $to)[0];
        $id = (int)mysql_fetch_row(mysql_query("SELECT `id` FROM `fd_emails` ORDER BY `id` DESC LIMIT 1"))[0];
            $new_id = $id+1;
        $sql = mysql_query("INSERT INTO `fd_emails` (`id`, `subject`, `text`, `sender`, `user`, `date`)
                                                   VALUES ('$new_id', '$subject', '$body', '$email', '$user', '$date');");
        imap_delete($imap, $index + 1);
    }
    //close connection to mailbox
    imap_expunge($imap);
    imap_close($imap);
    return true;
 }

提前感谢。

根据php文档

string imap_fetchbody ( resource $imap_stream , int $msg_number , string $section [, int $options = 0 ] )

号码必须只有字符串类型。使用整数类型会引发错误。