IMAP COPY命令在收件箱上不起作用-Python [英] IMAP COPY command not working on Inbox - Python

查看:79
本文介绍了IMAP COPY命令在收件箱上不起作用-Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Python3.6和IMAP4模块结合使用.我试图从收件箱"中复制电子邮件,到"mytestfolder".Iam表示确定"作为响应,但是电子邮件本身未复制到"mytestfolder".当相同的代码片段适用于"someotherfolder"时,到"mytestfolder"第一次没有任何问题,此后将无法正常工作.下面是代码片段,有人可以帮助我解决此问题.

Iam using Python3.6 with IMAP4 module.Iam trying to copy emails from "Inbox" to "mytestfolder". Iam getting "OK" as the response but the email itself is not being copied to "mytestfolder". Where as the same code snippet is working for "someotherfolder" to "mytestfolder" without any problem for the first time and after that it doesn't work. Below is the code snippet can someone please help me resolve this.

import config
import imaplib
from creds import username,password
imap = imaplib.IMAP4_SSL(config.imap_server,config.imap_port)
r, d = imap.login(username, password)
assert r == 'OK', 'login failed: %s' % str (r)
print(" > Signed in as %s" % username, d)

imap.select("Inbox")
r, d = imap.search(None, "ALL")
allIds = d[0].decode('utf8').split(' ')
''' Login works and iam getting msg_ids as well'''

for msg_id in allIds:
    apply_lbl_msg = imap.uid('COPY', msg_id, 'mytestfolder')
    if apply_lbl_msg[0] == 'OK':
        mov, data = imap.uid('STORE', msg_id , '+FLAGS', '(\Deleted)')
        imap.expunge()

推荐答案

TLDR:您通过删除内容,然后按过去的顺序编制索引来误算.

TLDR: You're miscounting by removing things and then indexing by what used to be the order.

您的代码可以:

r, d = imap.search(None, "ALL")

请给我收件箱中所有邮件的序列号",这样您就可以得到1、2、3、4、5,依此类推.d中的最后一个数字将等于上面几行 select()的返回值.然后循环,我将解释第一个迭代:

"Give me the sequence numbers of all messages in the inbox", so you get 1, 2, 3, 4, 5 and so on. The last number in d will equal the return value from select() a few lines above. Then you loop, I'll explain the first iteration:

apply_lbl_msg = imap.uid('COPY', msg_id, 'mytestfolder')
if apply_lbl_msg[0] == 'OK':

将第一条消息复制到mytestfolder,如果可行……".

"Copy the first message to mytestfolder, and if that works…."

    mov, data = imap.uid('STORE', msg_id , '+FLAGS', '(\Deleted)')
    imap.expunge()

…然后删除收件箱中的第一条消息",这意味着第二条消息现在成为第一条消息.

"… then delete the first message in the inbox", which means that what was the second message now becomes the first.

下一次迭代将对当前是邮箱中第二个消息的消息进行操作,而该消息是第三次,因此您永远不会对开始时为2的消息进行操作.第三次迭代基于当前的第三次消息进行,并且曾经是……我认为是第五次?没关系.

The next iteration operates on the message that's currently the second in the mailbox, and was once the third, so you never operate on the message that was 2 at the start. The third iteration operates on the message that's currently the third, and was once the... fifth I think? It doesn't matter.

您可以通过切换到UID版本来使其正确.重新编号时,UID不会改变.

You can make this correct by switching to the UID versions of the same. UIDs don't change as you renumber.

您还可以通过发出一个复制所有邮件的COPY命令,然后发布一个将其标记为已删除的单个存储,来使此操作正确并且快得多.您甚至不需要SEARCH,因为搜索的结果只是从1到 select()的返回值的所有数字.

You could also make this correct and very much faster by issuing one single COPY command that copies all messages, and then one single STORE that marks them as deleted. You don't even need the SEARCH, because the result of the search is just all the numbers from 1 to the return value of select().

这篇关于IMAP COPY命令在收件箱上不起作用-Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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