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

发布于 2024-12-01 04:52:05 字数 156 浏览 0 评论 0原文

我试图弄清楚如何使用 imap 和 php 获取最新的 3 封电子邮件(已看到和未看到)。由于邮箱内有 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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萌能量女王 2024-12-08 04:52:05

我是这样做的:

$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

问候
多米尼克

I did it like that:

$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

Regards
Dominic

蘑菇王子 2024-12-08 04:52:05

又怎样呢

imap_search($res, 'RECENT');

http://php.net/manual/en/function.imap-search.php

What about

imap_search($res, 'RECENT');

?

http://php.net/manual/en/function.imap-search.php

删除→记忆 2024-12-08 04:52:05
$msgnos = imap_search($mbox, "UNSEEN", SE_UID);
$i=0;
foreach($msgnos as $msgUID) {
    $msgNo = imap_msgno($mbox, $msgUID);
    $head = imap_headerinfo($mbox, $msgNo);
    $mail[$i][] = $msgUID;
    $mail[$i][] = $head->Recent;    
    $mail[$i][] = $head->Unseen;    
    $mail[$i][] = $head->from[0]->mailbox."@".$head->from[0]->host; 
    $mail[$i][] = utf8_decode(imap_utf8($head->subject));   
    $mail[$i][] = $head->udate;
}
return $mail;
imap_close($mbox);

会做这项工作。

$msgnos = imap_search($mbox, "UNSEEN", SE_UID);
$i=0;
foreach($msgnos as $msgUID) {
    $msgNo = imap_msgno($mbox, $msgUID);
    $head = imap_headerinfo($mbox, $msgNo);
    $mail[$i][] = $msgUID;
    $mail[$i][] = $head->Recent;    
    $mail[$i][] = $head->Unseen;    
    $mail[$i][] = $head->from[0]->mailbox."@".$head->from[0]->host; 
    $mail[$i][] = utf8_decode(imap_utf8($head->subject));   
    $mail[$i][] = $head->udate;
}
return $mail;
imap_close($mbox);

Will do the job.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文