使用imap和php检索最近的3封电子邮件 [英] Retrieve the 3 most recent email using imap and php

查看:90
本文介绍了使用imap和php检索最近的3封电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何使用imap和php获取最新的3封电子邮件(SEEN和UNSEEN).它必须是资源有效的,因为邮箱内部包含1 000封电子邮件.我认为获取所有标头可能需要太多资源.

I'm trying to figure out how to get the latest 3 emails (SEEN and UNSEEN) using imap and php. It need to be ressource-efficient since the mailbox as 1 000 emails inside. Getting all header may need too much ressources I think.

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

I just need the sender, the subject and the date...

有什么主意吗?感谢您的任何沉迷/帮助/解释/提示...

Any idea? Thanks for any syggestion/help/explaination/hint...

推荐答案

我是这样做的:

$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-Ref IMAP: http://php.net/manual/zh/ref .imap.php

PHP-Ref IMAP: http://php.net/manual/en/ref.imap.php

问候 多米尼克(Dominic)

Regards Dominic

这篇关于使用imap和php检索最近的3封电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆