我正在研究一种应用程序,基于新的Gmail IMAP电子邮件触发PHP脚本。如何最好地知道Gmail IMAP帐户上有新邮件到达?我只能想到配置cron作业。我在Linux(Ubuntu)盒子上运行PHP + Nginx。
$t1=time();//mark time in
$tt=$t1+(60*1);//total time = t1 + n seconds
do{
if(isset($t2)) unset($t2);//clean it at every loop cicle
$t2=time();//mark time
if(imap_num_msg($imap)!=0){//if there is any message (in the inbox)
$mc=imap_check($imap);//messages check
//var_dump($mc); die;//vardump it to see all the data it is possible to get with imap_check() and them customize it for yourself
}else echo 'No new messagens';
sleep(rand(7,13));//Give Google server a breack
if(!@imap_ping($imap)){//if the connection is not up
//start the imap connection the normal way like you did at first
}
}while($tt>$t2);//if the total time was not achivied yet, get back to the beginning of the loop
就是这样了。
顺便说一下,以下是关于IMAP如何工作的一些好信息。我的观点是:由于IMAP使得保持一种“实时同步”连接成为可能,如果您不想配置MTA来接收电子邮件(像我一样),那么IMAP是一个真正的选项,可以让您“推送邮件”。
获取电子邮件账户信息只有两种方式,一种是连接到该账户,定期读取新消息(例如通过cron-job),另一种是将电子邮件转发到自己的服务器上,并将新到达的电子邮件传递给一个PHP脚本。
Zend_Mail 是 Zend Framework 的一部分,其中包括 Zend_Mail_Storage_Imap(可以不使用MVC结构),可连接到 Gmail 以轮询账户。