[英]Find new messages added to an imap mailbox since I last checked with python imaplib2?
我正在嘗試編寫一個監視IMAP郵箱的程序,並自動將每個新傳入的郵件復制到“存檔”文件夾中。 我正在使用imaplib2來實現IDLE命令。 這是我的基本程序:
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()。 但是來自recent()的響應對我來說沒有意義,並且在第一條消息之后我從未收到任何其他通知。
有人知道怎么做嗎?
請參閱python-imap-idle-with-imaplib2中的示例和引用。 該模塊涉及線程,您應該注意事件同步。
該示例建議與事件同步,並將郵件處理留給讀者:
# The method that gets called when a new email arrives.
# Replace it with something better.
def dosync(self):
print "Got an event!"
從問題中提示, “更好的東西”可以是:
# Replaced with something better.
def dosync(self):
print "Got an event!"
res = self.M.recent()
print res
我發現最近的()有點模糊(這是IMAP模糊,而不是imaplib2)。 似乎最好在空閑之前和之后保留消息號列表,區別在於新消息。
然后使用fetch(messages,“UID”)來獲取消息uid。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.