【问题标题】:Retrieve the 3 most recent email using imap and php使用 imap 和 php 检索最近的 3 封电子邮件
【发布时间】:2011-10-31 16:31:44
【问题描述】:

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

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

有什么想法吗?感谢您的任何 syggestion/帮助/解释/提示...

【问题讨论】:

    标签: php imap


    【解决方案1】:

    我是这样做的:

    $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

    问候 多米尼克

    【讨论】:

      【解决方案2】:
      $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);
      

      会做的。

      【讨论】:

      • 并且imap_close() 在返回后不会被调用。
      【解决方案3】:

      怎么样

      imap_search($res, 'RECENT');
      

      ?

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

      【讨论】:

      • 我可以在搜索期间限制结果的数量,而不是获取所有最近的消息然后只接收 3 条消息吗?
      猜你喜欢
      • 2011-08-03
      • 1970-01-01
      • 2019-05-26
      • 1970-01-01
      • 2013-02-16
      • 1970-01-01
      • 2021-01-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多