查找自上次使用python imaplib2检查以来添加到imap邮箱的新邮件? [英] Find new messages added to an imap mailbox since I last checked with python imaplib2?

查看:114
本文介绍了查找自上次使用python imaplib2检查以来添加到imap邮箱的新邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个程序,该程序监视IMAP邮箱并将每条新收到的邮件自动复制到存档"文件夹中.我正在使用实现IDLE命令的imaplib2.这是我的基本程序:

I am trying to write a program that monitors an IMAP mailbox and automatically copies every new incoming message into an "Archive" folder. I'm using imaplib2 which implements the IDLE command. Here's my basic program:

M = imaplib2.IMAP4("mail.me.com")
M.login(username,password)
lst = M.list()
assert lst[0]=='OK'
for mbx in lst[1]:
    print "Mailboxes:",mbx

def process(m):
    print "m=",m
    res = M.recent()
    print res


M.select('INBOX')
M.examine(mailbox='INBOX',callback=process)
while True:
    print "Calling idle..."
    M.idle()
    print "back from idle"
M.close()
M.logout()

当邮箱发生第一次更改时,它将正确打印邮箱并运行process().但是last()的响应对我来说是没有意义的,并且在收到第一条消息后,我再也没有收到任何其他通知.

It prints the mailboxes properly and runs process() when the first change happens to the mailbox. But the response from recent() doesn't make sense to me, and after the first message I never get any other notifications.

有人知道该怎么做吗?

推荐答案

请参见

See example and references in python-imap-idle-with-imaplib2. The module involves threading, you should pay attention to event synchronization.

该示例建议与事件进行同步,并将邮件处理留给阅读器:

The example suggests synchronizing with events, and leaves mail processing to the reader:

# The method that gets called when a new email arrives. 
# Replace it with something better.
def dosync(self):
    print "Got an event!"

从问题中暗示,更好" 可以是:

Taking a hint from the question, "something better" can be:

# Replaced with something better.
def dosync(self):
    print "Got an event!"
    res = self.M.recent()
    print res

这篇关于查找自上次使用python imaplib2检查以来添加到imap邮箱的新邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆