[Python] Python 3: Move email to trash by uid (imaplib)

Discussão em 'Python' iniciado por Stack, Janeiro 30, 2025.

  1. Stack

    Stack Membro Participativo

    I want to move an email from my inbox to the trash folder, I do not want the email permanently deleted, I want it to go through the process of waiting 30 days in the trash to be permanently deleted.

    1.Logged in:


    mail = imaplib.IMAP4_SSL('imap.gmail.com')
    mail.login("example@gmail.com", "example")

    2.Got the list of uids in my inbox:


    mail.select("inbox")
    result, data = mail.uid('search', None, "ALL")
    uidList = data[0].split()

    3.Processed these emails:


    #processEmails returns the uids of the emails that I need
    #not really important for the purposes of this question

    newUidList = processEmails(uidList)

    4.Delete the emails in the newUidList


    for uid in newUidList:
    mail.uid('STORE',uid, '+FLAGS', '(\\Deleted)')


    I thought that this line mail.uid('STORE',uid, '+FLAGS', '(\\Deleted)') would solve the problem (because that's what I found on the internet). But being here asking this question, you probably have correctly guessed that it did not.

    What was happening?


    When I executed this script, the emails disappeared from the inbox just as planned. But when I visit the trash folder, there is nothing there. So I thought maybe they were permanently deleted.

    But they were not. I noticed this when I saw that my email storage space is filling up quick which means my emails were still there somewhere.

    I go into the "All Emails" folder and there they are.

    So my question is...

    1. How do I get the result I want? Which is moving the emails to the trash folder by their UIDs.
    2. Why is it not doing what I thought it would do? I am asking for an explanation for this line mail.uid('STORE',uid, '+FLAGS', '(\\Deleted)')
    And most importantly...


    Thank you for your input :)

    Continue reading...

Compartilhe esta Página