Code for How to Delete Emails in Python Tutorial

View on Github

import imaplib
import email
from email.header import decode_header

# account credentials
username = ""
password = "yourpassword"

# create an IMAP4 class with SSL 
imap = imaplib.IMAP4_SSL("")
# authenticate
imap.login(username, password)
# select the mailbox I want to delete in
# if you want SPAM, use"SPAM") instead"INBOX")
# search for specific mails by sender
status, messages =, 'FROM ""')
# to get all mails
# status, messages =, "ALL")
# to get mails by subject
# status, messages =, 'SUBJECT "Thanks for Subscribing to our Newsletter !"')
# to get mails after a specific date
# status, messages =, 'SINCE "01-JAN-2020"')
# to get mails before a specific date
# status, messages =, 'BEFORE "01-JAN-2020"')
# convert messages to a list of email IDs
messages = messages[0].split(b' ')
for mail in messages:
    _, msg = imap.fetch(mail, "(RFC822)")
    # you can delete the for loop for performance if you have a long list of emails
    # because it is only for printing the SUBJECT of target email to delete
    for response in msg:
        if isinstance(response, tuple):
            msg = email.message_from_bytes(response[1])
            # decode the email subject
            subject = decode_header(msg["Subject"])[0][0]
            if isinstance(subject, bytes):
                # if it's a bytes type, decode to str
                subject = subject.decode()
            print("Deleting", subject)
    # mark the mail as deleted, "+FLAGS", "\\Deleted")
# permanently remove mails that are marked as deleted
# from the selected mailbox (in this case, INBOX)
# close the mailbox
# logout from the account