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

标签 php imap

我正在尝试弄清楚如何使用 imap 和 php 获取最新的 3 封电子邮件(SEEN 和 UNSEEN)。它需要资源高效,因为邮箱内部有 1000 封电子邮件。我认为获取所有 header 可能需要太多资源。

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

有什么想法吗?感谢任何 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/en/ref.imap.php

问候 多米尼克

关于php - 使用 imap 和 php 检索最近的 3 封电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7140058/

相关文章:

php - 将分数转换为 html 实体

haskell - 使用 HaskellNet 的 IMAP 从电子邮件中获取主题时出错

java - 使用 Javamail API 和 IMAP 批量获取邮件正文

php - 从另一个模型cakephp 3.5获取数据

android - 是否有任何简单的阅读新 gmail 邮件的简短代码示例?

php - 如何使用 PHP IMAP 下载所有带有附件的邮件到服务器?

python - 在python中解码这种类型的字符串

php - 从 https 站点发送和接收数据

php - 错误 : jQuery1830649454693285679_1359620502896 was not called JSON

php - 在mysql查询中获取id和echo