简体   繁体   English

Python 3:通过uid(imaplib)将电子邮件移至垃圾箱

[英]Python 3: Move email to trash by uid (imaplib)

I want to move an email from my inbox to the trash folder, I do not want the email permanently deleted, I want it to go through the process of waiting 30 days in the trash to be permanently deleted. 我想将收件箱中的电子邮件移至垃圾箱文件夹,我不希望电子邮件被永久删除,我希望它能够在垃圾箱中等待30天才能永久删除。

1.Logged in: 1.登录:

mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login("example@gmail.com", "example")

2.Got the list of uids in my inbox: 2.在我的收件箱中输入uid列表:

mail.select("inbox")
result, data = mail.uid('search', None, "ALL")
uidList = data[0].split()

3.Processed these emails: 3.处理这些电子邮件:

#processEmails returns the uids of the emails that I need
#not really important for the purposes of this question

newUidList = processEmails(uidList)

4.Delete the emails in the newUidList 4.Delete中的电子邮件newUidList

for uid in newUidList:
    mail.uid('STORE',uid, '+FLAGS', '(\\Deleted)')

I thought that this line mail.uid('STORE',uid, '+FLAGS', '(\\\\Deleted)') would solve the problem (because that's what I found on the internet). 我认为这行mail.uid('STORE',uid, '+FLAGS', '(\\\\Deleted)')可以解决问题(因为这是我在互联网上找到的)。 But being here asking this question, you probably have correctly guessed that it did not. 但是在这里问这个问题,你可能已经正确地猜到它没有。

What was happening? 发生了什么事?

When I executed this script, the emails disappeared from the inbox just as planned. 当我执行此脚本时,电子邮件按计划从收件箱中消失。 But when I visit the trash folder, there is nothing there. 但是当我访问垃圾文件夹时,那里什么都没有。 So I thought maybe they were permanently deleted. 所以我想也许他们被永久删除了。

But they were not. 但他们不是。 I noticed this when I saw that my email storage space is filling up quick which means my emails were still there somewhere. 当我看到我的电子邮件存储空间快速填满时,我注意到了这一点,这意味着我的电子邮件仍在那里。

I go into the "All Emails" folder and there they are. 我进入“All Emails”文件夹,他们就在那里。

So my question is... 所以我的问题是......

  1. How do I get the result I want? 我如何得到我想要的结果? Which is moving the emails to the trash folder by their UIDs. 这是通过他们的UID将电子邮件移动到垃圾箱文件夹。
  2. Why is it not doing what I thought it would do? 为什么它没有做我认为它会做的事情? I am asking for an explanation for this line mail.uid('STORE',uid, '+FLAGS', '(\\\\Deleted)') 我要求解释这行mail.uid('STORE',uid, '+FLAGS', '(\\\\Deleted)')

And most importantly... 最重要的是......

Thank you for your input :) 谢谢您的意见 :)

I found the answer! 我找到了答案!

So for anyone having the same problem, I found out how to move your email to the trash by uid. 因此,对于遇到相同问题的任何人,我发现了如何通过uid将您的电子邮件移至垃圾箱。 referring back to my post, I should have put mail.uid('STORE', uid, '+X-GM-LABELS', '\\\\Trash') instead of mail.uid('STORE',uid, '+FLAGS', '(\\\\Deleted)') 回到我的帖子,我应该把mail.uid('STORE', uid, '+X-GM-LABELS', '\\\\Trash')而不是mail.uid('STORE',uid, '+FLAGS', '(\\\\Deleted)')

And there you have it :) 你有它:)

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

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