This question has been replicated under suggestion from this one at StackOverflow, as long as it is a programming issue, but seems to depend on Linux tools. I will post the link to the solution (if found) on both places.

I don't know much about IMAP mail, since I have always used POP3. But deleting emails from the POP3 mail server was possible by using tools included on Windows eMail clients like The Bat!.

But now on my job I have a new IMAP account with a stupid problem: sometimes emails from outside my domain (the @myenterprise.com part) do not arrive, and would like to script some method to detect it.

Just a simple shell script (Linux is highly preferred for me on scripting) that sends periodical emails from an external account and checks for the correct arrival of them would be enough, but if I schedule it to run once each hour... well, after a few days I will have lots of Subject:Test emails on trash. So the solution would be to delete them from IMAP server after the checks.

How can I delete specific emails from my IMAP server from Linux shell in a scriptable manner?

Bash shell preferred, if solutions are specific.

closed as off-topic by jasonwryan, Jeff Schaller, Kiwy, G-Man, Jesse_b May 2 '18 at 16:10

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question has been posted on multiple sites. Cross-posting is strongly discouraged; see the help center and community FAQ for more information." – jasonwryan, Jeff Schaller, Kiwy, Jesse_b

  • What email client are you using? Creative filter rules and auto-delete rules may work for you. Also, remember email isn't designed to be instant, and it is designed for "best effort" delivery. I've had mails arrive a few weeks after they were sent. – ivanivan May 1 '18 at 0:01
  • @ivanivan , why should the email client be relevant here? The shell script must do its work whatever program the users install as an email client. It is even possible that different stations could use different email clients. – Sopalajo de Arrierez May 1 '18 at 0:49
  • Because as I said creative filtering in the client could take care of the issue for you. But since this is multiple users, multiple clients, etc. that won't work. Instead I'll ask - is the server storing in mbox format or maildir format? I'm now assuming that you'll be running this on the server via shell access.... if I'm wrong, feel free to ignore – ivanivan May 1 '18 at 0:52
  • No, @ivanivan , I have no direct shell access to the IMAP server, just the config data and the password that I use on the client program for downloading/sending mails. I was rather thinking about a remote tool for the job. – Sopalajo de Arrierez May 1 '18 at 1:50

I would go for a Python script. The following script asks for the IMAP-Server, a username and password and subject string. It then lists all mails where the subject contains the given subject string.

Comment in the line imapserver.store(num, '+FLAGS', '\Deleted') when you are satisfied with the behavior. The shown mails will then be flagged for deletion.


import getpass

def del_imap(server, port, login, password, search):
    import imaplib, email

    # NOTE: According to RFC 1730 the SEARCH commands searches for 'messages that
    # CONTAIN the specified string. When multiple keys are specified, the result
    # is the intersection (AND function) of all the messages that match those
    # keys.
    # _search_command = '(FROM ' + search + ')'
    # _search_command = '(SUBJECT "testmail" FROM ' + search + ')'
    _search_command = '(SUBJECT ' + search + ')'

    imapserver = imaplib.IMAP4_SSL(server, port)
    imapserver.login(login, password)

    typ, data = imapserver.search(None, _search_command)
    for num in data[0].split():
        typ, data = imapserver.fetch(num, '(RFC822.HEADER)')
        print (data[0][1].decode())
        # Uncomment the following line if the listed files should also be
        # flagged for deletion
        # imapserver.store(num, '+FLAGS', '\\Deleted')

del_imap(input("IMAP Server: "), 993, input("Username: "), getpass.getpass(), input("Search: "))

I also created a Github-Gist where further improvements will be made: https://gist.github.com/jkirk/39dc64747a9d78accde49de2e8dbdc6d

  • Could you please expand with some usage examples? Some more details about what data should be changed for each specific configuration could be a good idea too. – Sopalajo de Arrierez May 1 '18 at 0:46
  • I've tried to improve my answer. The Python3 script can be used as is. It will show all mails which where the subject contains the given subject string (i.e. "testmail"). If you want to delete the shown mails, just comment in the mentioned line. – dpat May 1 '18 at 10:41
  • Is this method supposed to delete the shown mails, or just move them to the Thrash mailbox? – Sopalajo de Arrierez May 1 '18 at 15:14
  • @SopalajodeArrierez: the mails should be deleted. If you want to move them to the trash you have to use a the move function. – dpat May 3 '18 at 12:24
  • @SopalajodeArrierez: actually you have to add the copy function before deletion. – dpat May 3 '18 at 12:32

Not the answer you're looking for? Browse other questions tagged or ask your own question.