Is there any solution to delete thread emails. My solution deletes based on id and it takes time.
import imaplib
import email
from email.header import decode_header
user= "xxxxxxx"
app_password= "xxxxxxxxxx"
host = 'imap.gmail.com'
imap = imaplib.IMAP4_SSL("imap.gmail.com")
imap.login(user, app_password)
imap.select("INBOX")
status, messages = imap.search(None, 'SUBJECT "Appvila: Developer Test"')
messages = messages[0].split(b' ')
print(messages)
print(len(messages))
for mail in messages:
_, msg = imap.fetch(mail, "(RFC822)")
for response in msg:
if isinstance(response, tuple):
msg = email.message_from_bytes(response[1])
subject = decode_header(msg["Subject"])[0][0]
if isinstance(subject, bytes):
subject = subject.decode()
print("Deleting", subject)
imap.store(mail, "+FLAGS", "\\Deleted")
imap.expunge()
imap.close()
imap.logout()
I have thread mails but my solution takes one by one id and deletes is there any solution to delete completely