Imaplib [search, move, delete] Yahoo Mail

Any python experts willing to share there knowledge, please?!

Want to:

  1. Search ALL folders including SPAM, for “specific_user@ANY (wildcard) domain”
  2. Move those emails to folder called DUMP
  3. Delete contents of DUMP

Unfortunately I can’t figure out how to do what I need above in Yahoo Mail!

My code so far below:

import imaplib
import time

####### IMAP SSL #######
start = time.time()
try:
    imap_ssl = imaplib.IMAP4_SSL(host="imap.mail.yahoo.com", port=993)
except Exception as e:
    print("ErrorType : {}, Error : {}".format(type(e).__name__, e))
    imap_ssl = None

print("Connection Object : {}".format(imap_ssl))
print("Total Time Taken  : {:,.2f} Seconds".format(time.time() - start))


####### Login to Mailbox #######
print("Logging into mailbox...")
try:
    resp_code, response = imap_ssl.login("XXXXXXX@yahoo.com", "XXXXXXXXXXX")
except Exception as e:
    print("ErrorType : {}, Error : {}".format(type(e).__name__, e))
    resp_code, response = None, None

print("Response Code : {}".format(resp_code))
print("Response      : {}\n".format(response[0].decode()))


####### Missing code to search, move and delete #######




####### Logout of Mailbox #######
print("\nLogging Out....")
try:
    resp_code, response = imap_ssl.logout()
except Exception as e:
    print("ErrorType : {}, Error : {}".format(type(e).__name__, e))
    resp_code, response = None, None

print("Response Code : {}".format(resp_code))
print("Response      : {}".format(response[0].decode()))

https://docs.python.org/3/library/imaplib.html#imaplib.IMAP4.search ?

1 Like