Phyton Imaplib: how to delete an email from Gmail? python imaplib delete email,python imap delete email,python imaplib

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?

Answer:1



  1. python imaplib delete email
  2. python imap delete email
  3. python imaplib delete message
  4. python imap delete message
  5. python delete email imap

I'm new to Python (from Java+Ant) and was wondering if someone could detail how to best use Fabric+Pip+Virtualenv to set up a Python web application package skeleton. The end goal is to be able to do ...

I'm new to Python (from Java+Ant) and was wondering if someone could detail how to best use Fabric+Pip+Virtualenv to set up a Python web application package skeleton. The end goal is to be able to do ...

How can I extend a builtin class in python? I would like to add a method to the str class. I've done some searching but all I'm finding is older posts, I'm hoping someone knows of something newer.

How can I extend a builtin class in python? I would like to add a method to the str class. I've done some searching but all I'm finding is older posts, I'm hoping someone knows of something newer.