Imaplib [search, move, delete] Yahoo Mail

Discussion in 'Python' started by Linh Lee, Aug 19, 2022.

  1. Linh Lee

    Linh Lee New Member

    Joined:
    Aug 18, 2022
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    1
    Gender:
    Female
    Location:
    San Francisco
    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:
    Code:
    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()))
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice