使用imap和php检索最近的3封电子邮件

使用imap和php检索最近的3封电子邮件,php,imap,Php,Imap,我正试图弄清楚如何使用imap和php获取最新的3封电子邮件(可见和不可见)。由于邮箱中包含1000封电子邮件,因此需要提高资源效率。我认为获取所有标题可能需要太多资源 我只需要发件人、主题和日期 有什么想法吗?谢谢你的建议/帮助/解释/提示…你呢 imap_search($res, 'RECENT'); ? 我会做的。我就是这样做的: $mbox = imap_open("{imap.myconnection.com:993/imap/ssl}INBOX", "username", "pa

我正试图弄清楚如何使用imap和php获取最新的3封电子邮件(可见和不可见)。由于邮箱中包含1000封电子邮件,因此需要提高资源效率。我认为获取所有标题可能需要太多资源

我只需要发件人、主题和日期

有什么想法吗?谢谢你的建议/帮助/解释/提示…

你呢

imap_search($res, 'RECENT');
?

我会做的。

我就是这样做的:

$mbox = imap_open("{imap.myconnection.com:993/imap/ssl}INBOX", "username", "password");

// get information about the current mailbox (INBOX in this case)
$mboxCheck = imap_check($mbox);

// get the total amount of messages
$totalMessages = $mboxCheck->Nmsgs;

// select how many messages you want to see
$showMessages = 5;

// get those messages    
$result = array_reverse(imap_fetch_overview($mbox,($totalMessages-$showMessages+1).":".$totalMessages));

// iterate trough those messages
foreach ($result as $mail) {

    print_r($mail); 

    // if you want the mail body as well, do it like that. Note: the '1.1' is the section, if a email is a multi-part message in MIME format, you'll get plain text with 1.1
    $mailBody = imap_fetchbody($mbox, $mail->msgno, '1.1');

    // but if the email is not a multi-part message, you get the plain text in '1'
    if(trim($mailBody)=="") {
        $mailBody = imap_fetchbody($mbox, $mail->msgno, '1');
    }

    // just an example output to view it - this fit for me very nice
    echo nl2br(htmlentities(quoted_printable_decode($mailBody)));
}

imap_close($mbox);
PHP参考IMAP:

问候
Dominic

我是否可以在搜索过程中限制搜索结果的数量,而不是获取所有最新消息,然后只获取3条消息?并且在返回后将不会调用
imap\u close()
$mbox = imap_open("{imap.myconnection.com:993/imap/ssl}INBOX", "username", "password");

// get information about the current mailbox (INBOX in this case)
$mboxCheck = imap_check($mbox);

// get the total amount of messages
$totalMessages = $mboxCheck->Nmsgs;

// select how many messages you want to see
$showMessages = 5;

// get those messages    
$result = array_reverse(imap_fetch_overview($mbox,($totalMessages-$showMessages+1).":".$totalMessages));

// iterate trough those messages
foreach ($result as $mail) {

    print_r($mail); 

    // if you want the mail body as well, do it like that. Note: the '1.1' is the section, if a email is a multi-part message in MIME format, you'll get plain text with 1.1
    $mailBody = imap_fetchbody($mbox, $mail->msgno, '1.1');

    // but if the email is not a multi-part message, you get the plain text in '1'
    if(trim($mailBody)=="") {
        $mailBody = imap_fetchbody($mbox, $mail->msgno, '1');
    }

    // just an example output to view it - this fit for me very nice
    echo nl2br(htmlentities(quoted_printable_decode($mailBody)));
}

imap_close($mbox);