I got a mailbox-file ("Mboxrd") created by getmail, and I'd like to "process" it by sorting mail from various (groups of) senders, and move them to separate mailbox-files (I guess 3-5 for now). Ideally, the mail messages thus moved, should be deleted from the original mailbox-file.

I most likely would want to classify mail by the sender-address - although being able to classify it by Subject (ie. certain words/phrases) would also be nice...

Now, if this had been messages just received on my computers MTA - or if I'd set-up getmail to deliver mail to the MTA - I guess I could use procmail the normal way... However, what I got is a "dead" file with already stored mail-messages, so that's not an option. I guess I could send the mail through the MTA, but surely there must be a way to process the mail as it is (in the file)?

So, is there a program or command - or some special options to procmail or a similar command - that lets me "sort" the mail in a mailbox-file into several other mailbox-files, based on the sender/subject of the messages? A small example (eg. of the config-file) would also be appreciated...

2 Answers 2


The formail -s method is documented with a demo script in the procmail man page; look for this phrase:

Procmail can also be invoked to postprocess an already filled system mailbox.

You would also find this in http://www.iki.fi/era/procmail/mini-faq.html#split

If your mailbox is not a Berkeley mbox single file, chances are you have a directory where each file is an individual message. In this case, you can process it simply with

for f in folder/*; do
    procmail -options <"$f"
    # maybe rm "$f" to remove the original after processing completes

As an example of how to filter by sender, a simple recipe would be something like

# If your mailbox destination is maildir, no locking is required
:0:  # so you could lose the second colon here, and in subsequent examples
* ^From:.*\<alice@example\.com\>

* ^From:.*\<bob@example\.net\>

etc. This examines the "From:" header which is usually what people mean by "sender", though there are two other fields in common use (the envelope sender, which is usually in Return-Path:, and the actual Sender: header which is optional, but frequently populated by the sending software when the From: header is not the actual sender).

Similarly, to classify by Subject:, simply specify a regex which matches the subjects you want to capture.

* ^Subject: *((Fwd?|Re): *)*\[gunk]

would find messages with Subject headers which contain the label [gunk], optionally preceded by whitespace and/or Re: and/or Fw: and/or Fwd:.

  • If you need help crafting regular expressions for specific patterns, probably post a separate question with more detailed requirements.
    – tripleee
    Jan 16, 2017 at 6:01

Assuming that your mailbox file is in the mbox format, you can use the formail utility that comes with procmail to run a command on each mail, with the -s option to tell it that the input is a mailbox and not just a single mail. That command can be procmail if you want to apply your procmail rules, or it can be anything else that receives the mail on standard input and does whatever it wants with it.

formail -s procmail <Mboxrd && mv Mboxrd Mboxrd.done

Another approach would be to load the mailbox in mutt and apply some macros. This has the advantage of being agnostic to the mailbox format, but has the downside that mutt macros can be cryptic (not necessarily more cryptic than procmail rules though). Another advantage of using mutt is that it can move one mail at a time, so if it's interrupted midway, it can resume where it left off.

  • Just beware that procmail hasn't been updated in 15+ years. It has well-known, nasty security vulnerabilities. Its last die-hard supporters gave up on it and run for the hills some 5+ years ago. FWIW. Jan 16, 2017 at 7:41
  • 1
    @SatoKatsura Actually, we are still here
    – tripleee
    Jan 16, 2017 at 10:06
  • @tripleee That isn't really something to be proud of in 2017. :) Jan 16, 2017 at 11:05
  • @SatoKatsura Feel free to post an answer showing how to do this with Sieve or Maildrop or whatever it is you are trying to advocate between the lines here.
    – tripleee
    Jan 16, 2017 at 11:11
  • @tripleee Not trying to advocate anything, just a quick reminder. Jan 16, 2017 at 11:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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