Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

imap - how to delete messages

Tags:

python

imap

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() 
like image 1000
Derek Avatar asked Jul 05 '10 16:07

Derek


People also ask

How do I delete IMAP messages in Outlook?

Step 1: Shift to the Mail view, and click to open the specified IMAP folder that you will purge all messages marked for deletion. Step 2: Click the Purge > Purge Marked Items in “Inbox” on the Folder tab.

Does IMAP remove messages from server?

Yes, the IMAP sync deleted messages from server. It means, if you delete a message, it will get removed from the server. This article will guide you about IMAP synchronization.

Can I delete IMAP files?

No, disabling the account won't delete anything. But it should remove any files stored in IMAP on the computer. Since it is disabled, it no longer communicates with the server and will have no actions with the server site.

Does IMAP keep messages on server?

If you have an IMAP, or HTTP (such as Gmail or Outlook.com ) account, mail isn't stored on your computer. All email remains on the mail server until you delete it.


2 Answers

This is the working code for deleting all emails in your inbox:

import imaplib box = imaplib.IMAP4_SSL('imap.mail.microsoftonline.com', 993) box.login("[email protected]","paswword") box.select('Inbox') typ, data = box.search(None, 'ALL') for num in data[0].split():    box.store(num, '+FLAGS', '\\Deleted') box.expunge() box.close() box.logout() 
like image 166
codersofthedark Avatar answered Sep 22 '22 23:09

codersofthedark


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() 
like image 29
Joril Avatar answered Sep 24 '22 23:09

Joril