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,
The following code prints some message header fields and then delete message.
import imaplib
from email.parser import HeaderParser
m = imaplib.IMAP4_SSL("your_imap_server")
m.login("your_username","your_password")
# get list of mailboxes
list = m.list()
# select which mail box to process
m.select("Inbox")
resp, data = m.uid('search',None, "ALL") # search and return Uids
uids = data[0].split()
mailparser = HeaderParser()
for uid in uids:
resp,data = m.uid('fetch',uid,"(BODY[HEADER])")
msg = mailparser.parsestr(data[0][1])
print (msg['From'],msg['Date'],msg['Subject'])
print m.uid('STORE',uid, '+FLAGS', '(\\Deleted)')
print m.expunge()
m.close() # close the mailbox
m.logout() # logout