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,
If you are using GMail the process is a bit different:
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.
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("user@domain.com","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()
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