简体   繁体   English

如何使用 imap-tools 删除消息

[英]How to delete messages with imap-tools

I use the following code to delete messages from my IMAP server我使用以下代码从 IMAP 服务器中删除邮件

uids = []
for msg in mailbox.fetch(filter):
    print(msg.uid, msg.date, msg.from_, msg.subject)
    uids.append(msg.uid)
mailbox.delete([msg.uid])

That doesn't delete the intended messages, though.但是,这不会删除预期的消息。 If the filter returns eg 3 messages, only the first filtered message is deleted and then maybe two others (though I'm not sure about those two others).如果过滤器返回例如 3 条消息,则仅删除第一条过滤后的消息,然后可能还有另外两条(尽管我不确定另外两条)。

I've read about MSNs that cause errors when used instead of UIDs when deleting messages.我读过有关在删除消息时使用 MSN 而不是 UID 会导致错误的信息。 But I don't see the problem in the code above.但我在上面的代码中没有看到问题。 Here is the example code from the repo which seems to work fine, but I don't understand the difference:这是来自 repo 的示例代码,它似乎工作正常,但我不明白其中的区别:

mailbox.delete([msg.uid for msg in mailbox.fetch()])

Can anybody point me in the right direction?谁能指出我正确的方向?

You collect message UIDs into a list ( uids ), and then remove only the last message (after the loop is finished).您将消息 UID 收集到一个列表 ( uids ) 中,然后仅删除最后一条消息(在循环完成后)。

This is probably what you intended to do (minimal changes for clarity):这可能是您打算做的(为了清楚起见,进行了最小的更改):

uids = []
for msg in mailbox.fetch(filter):
    print(msg.uid, msg.date, msg.from_, msg.subject)
    uids.append(msg.uid)
mailbox.delete(uids)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM