I have label named “Invalid mail” I need to delete all mails from that label. Is there any solution? I have written a code to delete all mail from Trash.
import imaplib as imp
import pprint as pp
import email
user = 'xxxxxxxxx'
app_password = '=xxxxxxxxx'
host = 'imap.gmail.com'
imapHostServer = host
imapUserEmail = user
imapPassword = app_password
imapVar = imp.IMAP4_SSL(imapHostServer)
imapVar.login(imapUserEmail, imapPassword)
# label = "Invalid mail"
# imapVar.select(f'"{label}"')
imapVar.select(mailbox='"[Gmail]/Trash"', readonly=False)
imapVar.store("1:*", '+FLAGS', '\Deleted')
imapVar.expunge()
imapVar.close() # close and logout the connection
imapVar.logout()
New contributor
Ajay is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.