Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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:

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. 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?

like image 476
Esabe Avatar asked Jun 10 '10 09:06

Esabe


1 Answers

I believe your conversation isn't really deleted: By default, deleting a message per IMAP from the last folder archives the conversation. Have a look at Settings -> Forwarding and POP/IMAP -> When a message is marked as deleted and expunged from the last visible IMAP folder:

like image 102
hynek Avatar answered Oct 21 '22 21:10

hynek