简体   繁体   English

Imaplib:如何从Gmail删除电子邮件?

[英]Imaplib: how to delete an email from Gmail?

I am trying to do something as simple as moving an email from inbox to trash using the imaplib of python. 我正在尝试做一些简单的事情,例如使用python的imaplib将电子邮件从收件箱移到垃圾箱。 However, I am having some problems and I don't know how to solve them. 但是,我遇到了一些问题,我不知道如何解决。

After connect and login into the server, I select the INBOX mailbox and I get the ids list this way: 连接并登录到服务器后,选择INBOX邮箱,并通过以下方式获取ID列表:

typ, ids = imap_object.search(None, 'INBOX')
ids_list = ids[0].split()

Let's suppose I take an Id from there, X, and let's suppose that that email is part of a conversation. 假设我从那里获取一个ID X,并假设该电子邮件是对话的一部分。 When I do 当我做

imap_object.fetch (X, '(RFC822)')[1]

I can see just the message, as I expected, but when I do 正如我所期望的,我只能看到该消息,但是当我这样做时

imap.store(X, '+FLAGS', r'(\Deleted)')

it deletes all the conversation, not just the message!. 它会删除所有对话,而不仅仅是消息! Also, I don't want to completely remove the email, I want to send it to trash, so I have tried: 另外,我不想完全删除电子邮件,而是想将其发送到垃圾箱,所以我尝试了以下操作:

imap.copy(X, '[Gmail]/Papelera')
imap.store(X, '+FLAGS', r'(\Deleted)')
imap.expunge()

but it copies just the message to trash, and after that it deletes the whole conversation, so I am loosing emails! 但它只将邮件复制到垃圾箱,然后删除整个对话,所以我在丢失电子邮件!

QUESTION : How can I delete an email from Gmail using imaplib, without deleting the whole conversation? 问题 :如何在不删除整个对话的情况下使用imaplib从Gmail删除电子邮件?

I believe your conversation isn't really deleted: By default, deleting a message per IMAP from the last folder archives the conversation. 我相信您的对话并没有真正删除:默认情况下,从最后一个文件夹中的每个IMAP删除一条消息都会存档该对话。 Have a look at Settings -> Forwarding and POP/IMAP -> When a message is marked as deleted and expunged from the last visible IMAP folder: 查看设置->转发和POP / IMAP->当邮件被标记为已删除并从最后一个可见的IMAP文件夹中删除时:

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

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