ll ll ll ll - 1 year ago 134
Python Question

Cant delete messege with imaplib

I use this code to delete a messege from my email:

mail.store(id, '+FLAGS', '\\Deleted')

And this code is return 'Ok'.

('OK', [None])

I also changed the Settings in my gmail acount to be:
Settings of my imap using with gmail

But i still cant delete messeges. Why is it?

Max Max
Answer Source

You are mixing 'message sequence numbers' (MSN, what you are calling 'fake ids') and 'unique ids' (UID, which you are calling 'real ids').

It is more convenient to use UIDs everywhere. There are a few commands that come in both MSN and UID versions: FETCH vs UID FETCH, SEARCH vs UID SEARCH, STORE vs UID STORE. You must be consistent, but they otherwise work identically.

So, if you use UID SEARCH, you should use UID STORE:

email.uid('STORE', id, '+FLAGS', '(\\Deleted)')

Regarding deletion on Gmail, specifically: despite the user setting about deletion, I have found it always just removes the 'Inbox' label (or whatever folder you happen to be in), which will leave it Archived in your All Mail folder or equivalent. To really delete it, you may need to:

  • move it to the users Trash folder (using (UID) MOVE, or a combination of a (UID) COPY and a deletion.
  • then delete it from the users Trash folder. Most users would find it acceptable to just leave it in the Trash until it auto deletes in 30 days.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download