我正在使用Zend框架提供的IMAP类来通过imap访问gmail消息。我逐个访问收件箱中所有邮件的邮件头,并在本地索引它们。该脚本适用于邮箱小于10000的收件箱。对于较大的收件箱,脚本会丢失连接,可能是超时。
这是堆栈跟踪:
异常消息:无法读取 - 连接已关闭?
迹:
#0 /home/dev/trunk/Zend/Mail/Protocol/Imap.php(168):Zend_Mail_Protocol_Imap-> _nextLine()
#1 /home/dev/trunk/Zend/Mail/Protocol/Imap.php(285):Zend_Mail_Protocol_Imap-> _nextTaggedLine(NULL)
#2 /home/dev/trunk/Zend/Mail/Protocol/Imap.php(587):Zend_Mail_Protocol_Imap-> readLine(NULL,'TAG103')
#3 /home/dev/trunk/Zend/Mail/Storage/Imap.php(353):Zend_Mail_Protocol_Imap-> fetch('UID',12267)
#4 /home/dev/trunk/model/gmail_imap_oauth.class.php(121):Zend_Mail_Storage_Imap-> getUniqueId(12267)
是否有可能使连接保持较长时间?我正在通过命令行运行此脚本并尝试在php.ini中增加脚本max runtime,但它没有帮助。
答案 0 :(得分:1)
这里的功能
public function indexAllMails($ startIndex = 1) {
$this->_imap = new Zend_Mail_Protocol_Imap('imap.gmail.com', '993', true);
$authenticateParams = array('XOAUTH', $initClientRequestEncoded);
$this->_imap->requestAndResponse('AUTHENTICATE', $authenticateParams);
//Create the mail storage Object
$this->_storage = new Zend_Mail_Storage_Imap_Wrapper($this->_imap);
//Select Folder
$this->_storage->selectFolder("[Gmail]/All Mail");
$numMessagesTotal = $this->_storage->countMessages();
if($numMessagesTotal == 0 ) return true;
for($i=$startIndex;$i<=$numMessagesTotal;$i++)
{
try {
$uniqueId = $this->_storage->getUniqueId($i);
$message = $this->_storage->getMessage($i);
}
catch(Exception $ex)
{
log("Error getting Unique id",'index');
log($ex->getMessage(),'index');
log($ex->getTraceAsString(),'index');
if($ex->getMessage() == 'cannot read - connection closed?')
{
//Timeout :(
return true;
}
else
continue;
}
$from = $message->from;
echo $from;
}
}