imap_fetchbody()text / html section

时间:2014-08-03 17:35:24

标签: php imap

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

使用以下功能,电子邮件应直接保存在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;
 }

非常感谢提前。

1 个答案:

答案 0 :(得分:0)

根据php documentation

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

该数字必须仅为字符串类型。使用整数类型会引发错误。