0

I'm currently stuck on this code. I'm able to mark emails to the trash but once it goes to the trash. I can't however select my trash folder and empty it. What am I doing wrong? Below is a snippet of my code.

  MAIL_SERVER = 'imap.gmail.com'
  USERNAME = 'email@gmail.com'#
  PASSWORD = '123' # 
  MAILBOX = 'process' #
imap.select(MAILBOX)
  

    typ, data = imap.search(None, 'ALL', search_args)
    for num in data[0].split():
        #this works and sends emails to trash
        imap.store(num, '+X-GM-LABELS', '\\Trash')

    #issue I'm having
    imap.select('[Gmail]/Trash')  # select all trash
    imap.store(num, '+FLAGS', '\\Deleted')  #Flag all Trash as Deleted
  
    imap.expunge()
    imap.close()
    imap.logout()
Dilshan
  • 187
  • 6
  • You are using `num` after the `for` loop, which may or may not give you the intended result. Are you trying the solution from [this thread](https://stackoverflow.com/questions/3180891/imap-how-to-delete-messages)? – metatoaster Apr 07 '21 at 04:22
  • yes I got some ideas from that thread. Do you have any solution on how to fix this? I just want to know how I can empty the trash once the emails go there. – Dilshan Apr 07 '21 at 09:10
  • If you really want to delete *everything* in the trash, just use imap.store(‘1:*’, ...). 1:* is a shortcut for “all messages in the mailbox.” The message will NOT retain the same message number in the trash folder. If you’re lucky, the store will return a COPYUID or some response that will help you find it, but otherwise.... why not just leave it there, and set the folder to autodelete in 30 days? – Max Apr 07 '21 at 19:27

0 Answers0