我使用php-imap-client从php-imap获取邮件,脚本工作没有问题,但我想限制从服务器获取的邮件数量。
这是剧本:
<?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
我该怎么做?
答案 0 :(得分:2)
查看描述,这些函数似乎没有办法限制收集的消息数量,因为它们使用消息的ID而不仅仅是一般计数。因此,您可能拥有不同的ID列表。拿他们的示例转储,你有ID 15和ID 14.列表中的下一个可能是ID 10,因为用户可能删除了13,12和11。
所以你可以收集初始列表,然后用$imap->getMessage($id);
迭代它。它看起来像这样:
$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...
}