首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何限制php-imap的结果

如何限制php-imap的结果
EN

Stack Overflow用户
提问于 2015-05-29 13:25:50
回答 1查看 2K关注 0票数 0

我使用php-imap客户端从php-imap获取邮件,脚本工作正常,但我想限制从服务器获取邮件的数量。

这是一个脚本:

代码语言:javascript
复制
<?PHP

require_once "Imap.php";

$mailbox = 'imap-mail.outlook.com';
$username = 'MYEMAIL@outlook.it';
$password = 'MYPASSWORD';
$encryption = 'ssl'; // or ssl or ''

// open connection
$imap = new Imap($mailbox, $username, $password, $encryption);

// stop on error
if($imap->isConnected()===false)
    die($imap->getError());

// get all folders as array of strings
$folders = $imap->getFolders();
foreach($folders as $folder)
    echo $folder;

// select folder Inbox
$imap->selectFolder('INBOX');

// count messages in current folder
$overallMessages = $imap->countMessages();
$unreadMessages = $imap->countUnreadMessages();

// fetch all messages in the current folder
$emails = $imap->getMessages($withbody = false);
var_dump($emails);

// add new folder for archive
$imap->addFolder('archive');

// move the first email to archive
$imap->moveMessage($emails[0]['id'], 'archive');

// delete second message
$imap->deleteMessage($emails[1]['id']);

我正在使用的脚本:https://github.com/SSilence/php-imap-client

我该怎么做呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-29 16:36:55

从描述来看,这些函数似乎没有办法限制收集的消息数量,因为它们使用消息的ID,而不仅仅是普通计数。因此,您可能有一个不同的ID列表。以他们的例子转储为例,您有ID 15和ID 14。列表中的下一个可能是ID 10,因为用户可能已经删除了13、12和11。

因此,您可以收集初始列表,然后使用$imap->getMessage($id);迭代它,如下所示:

代码语言:javascript
复制
$overallMessages = $imap->countMessages();
$unreadMessages = $imap->countUnreadMessages();

$limitCount = 20;
$partialEmailIdList = array();

// fetch all messages in the current folder
$emails = $imap->getMessages($withbody = false);

if($limitCount < $overallMessages){
    for($i=0; $i<$limitCount; $++){
        // Populate array with IDs
        $partialEmailIdList[] = $emails[$i]['id'];
    }
} else {
    foreach($emails as $email){
        $partialEmailIdList[] = $email['id'];
    }
}
foreach($partialEmailIdList as $mid){
        $message = $imap->getMessage($mid);
        // Do stuff...
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30530833

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档