PHP System_Daemon 和 IMAP 连接问题
我正在尝试创建一个 PHP 守护进程,它连接到 IMAP 服务器并处理传入的电子邮件。我已经快要工作了,但守护进程不断抓取第一次加载守护进程时发现的原始电子邮件。我相信原因是因为我在父进程中打开 IMAP 连接。下面的示例:
if ($imapConnection=imap_open($authhost,$user,$pass) or die())
{
//start daemon
while()
{
//Grab email headers
$imapHeaders = imap_headers($imapConnection);
$count = sizeof($imapHeaders)
//loop the emails
for($i = 1; $i <= $count, $i++)
{
//process the email
//delete the email
}
System_Daemon::iterate(15);
}
}
imap_close($imapConnection);
我不想将 IMAP 连接放入循环中。如何将与 IMAP 服务器的连接保持在循环之外并仍能收到新电子邮件?
I'm trying to create a PHP daemon that connects to an IMAP server and processes emails as they come in. I have it close to working, but the daemon keeps grabbing the original emails that it finds the first time the daemon is loaded. I believe the reason is because I'm opening the IMAP connection in the parent process. Example below:
if ($imapConnection=imap_open($authhost,$user,$pass) or die())
{
//start daemon
while()
{
//Grab email headers
$imapHeaders = imap_headers($imapConnection);
$count = sizeof($imapHeaders)
//loop the emails
for($i = 1; $i <= $count, $i++)
{
//process the email
//delete the email
}
System_Daemon::iterate(15);
}
}
imap_close($imapConnection);
I'd like to stay away from putting the IMAP connection within the loop. How can I keep the connection to the IMAP server outside of the loop and still get new emails?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 IMAP 中,邮件保留在服务器上。因此,每次您来时,如果您没有明确删除它们,旧电子邮件仍然存在。为了防止处理这些电子邮件,您可以使用一个 var 来保存您之前处理过的邮件数量,这样您就可以从 $i = 0 (假设最后到达的邮件)到 $i < $var 其中 $var 代表已处理的邮件数。
编辑:
由于您通过 imap_delete 删除了邮件,因此在每个循环中执行 imap_expunge 。
编辑2:
使用imap_reopen,我在每次循环后使用 imap_reopen($imapConnection, "{domain.tld}INBOX"); 尝试在我的服务器上编写脚本,现在它会看到新邮件。不进行新的身份验证,只需移动您的流。
In IMAP, mails stay on the server. So each time you come, if you have not explicitly removed them, old emails are still there. To prevent processing these emails, you could have a var that keeps the amount of mails you treated before, so you could go from $i = 0 (supposed the last arrived) to $i < $var where $var stands for the number of mails already treated.
EDIT :
Since you delete the mail by imap_delete, do an imap_expunge at each loop.
EDIT 2 :
Use imap_reopen, I tried you script on my server using
imap_reopen($imapConnection, "{domain.tld}INBOX");
after each loop and it sees the new mail now. Does not do a new authentication, just move your stream.