tags:

views:

46

answers:

2

How can I delete messages from the mail box? I am using this code, but the letters are not removed. Sorry for my English.

def getimap(self,server,port,login,password):
    import imaplib, email
    box = imaplib.IMAP4(server,port)
    box.login(login,password)
    box.select()
    box.expunge()
    typ, data = box.search(None, 'ALL')
    for num in data[0].split() :
        typ, data = box.fetch(num, '(UID BODY[TEXT])')
        print num
        print data[0][1]
    box.close()
    box.logout()
+2  A: 

I think you should mark the emails to be deleted, first.. For example:

for num in data[0].split():
   box.store(num, '+FLAGS', '\\Deleted')
box.expunge()
Joril
A: 

If you are using GMail the process is a bit different:

  1. Move it to the [Gmail]/Trash folder.
  2. Delete it from the [Gmail]/Trash folder (Add \Delete flag)

All emails in [Gmail]/Spam and [Gmail]/Trash are deleted after 30 days. If you delete a message from [Gmail]/Spam or [Gmail]/Trash, it will be deleted permanently.

Remember also to call EXPUNGE after setting the tag Deleted.

Pawel Lesnikowski