eightShirt
eightShirt

Reputation: 1447

Python - Why uid function is not deleting my e-mail sent?

I'm new to Python and I'm trying to use imaplib. I want to delete a specific e-mail sent from my hotmail account. I found the e-mail, the code doensn't return error but the message is not deleted.

mail = imaplib.IMAP4_SSL('imap-mail.outlook.com')
mail.login('[email protected]', 'XXXXX')
mail.select('Sent')
typ, data = mail.search(None, 'ALL')
control = 0
tam = len(data[0].split())
while control < tam:
    typ, data = mail.fetch(tam - control, '(RFC822)')
    if str(data).find("My Subject") != -1: #works
        mail.uid('STORE', str(tam - control) , '+FLAGS', '(\Deleted)') # doesn't work
        mail.expunge() # doesn't work
    control = control + 1

Any idea? Thanks in advance!

Upvotes: 1

Views: 627

Answers (1)

Eugene Yarmash
Eugene Yarmash

Reputation: 149776

IMAP4.uid() expects a message UID, while you seem to be using a message number. Try this instead:

mail.store(str(tam - control), '+FLAGS', '\\Deleted')
mail.expunge()

Note that IMAP4 message numbers change as the mailbox changes; in particular, after an EXPUNGE command performs deletions the remaining messages are renumbered.

Upvotes: 3

Related Questions