mailproc reads email on stdin and takes actions based on its contents. It can be used to file email in different folders based on content, drop unwanted mail, or execute other tasks based on mail content. It is intended as a replacement for procmail.
Mail handling rules are specified in a TOML formatted configuration file, stored in $HOME/.mailproc.conf
.
The configuration file consists of a version number (1), and an array of rules. Each rule must match the following specification, as given in the program source:
rust
struct Rule {
headers: Option<Vec<HashMap<String, String>>>,
body: Option<Vec<Vec<String>>>,
raw: Option<Vec<Vec<String>>>,
action: Option<Vec<Vec<String>>>,
filter: Option<Vec<String>>,
}
All elements of a rule are optional. An empty rule matches all messages and performs no actions (the message is dropped). Messages which match no rules are dropped. A rule matches if the headers
, body
, and raw
parts of the rule each match, or are omitted. Each rule element is described below:
headers
: An array of tables specifying header elements to match, and a regular expression to match them against. In order for the headers
to match, any of the provided tables must have all of its header elements matched. For example, the table { From = "you@example\\.com", To = "me@example\\.com" }
will match if both the From and To message headers match the given patterns. Specify multiple tables to match on any of the sets of header values.
body
: An array of sets of regular expressions to match in the message body. Like headers
, body
will match of any of the sets of regular expressions have all of their expressions match.
raw
: An array of sets of regular expressions to match against the raw message. Like body
, but matches the entire raw message.
action
: An array of commands to execute. Each command is specified as an array of strings, one string per command argument, and the email message will be provided to the command on stdin. For example, to run the dovecot deliver
command, you could provide an action like ["/usr/local/libexec/dovecot/deliver", "-d", "todd"]
.
filter
: A command specified as an array of strings, one string per argument. If provided, the message will be passed through the filter program and the output will be used to match the rest of the rule.
A configuration file could look like the following:
```toml
version = 1
[[rules]] action = [ ["/usr/local/libexec/dovecot/deliver", "-d", "todd", "-m", "mailinglist"], ] headers = [ { List-ID = "mailinglist\.example\.com" }, ]
[[rules]] headers = [ { From = "AnnoyingSender" }, { Subject = "^Buy pills online$" }, ]
[[rules]] raw = [ ["a large sum of money"], ["limited time offer"], ]
[[rules]] action = [ ["/usr/local/libexec/dovecot/deliver", "-d", "todd", "-m", "junk"], ] headers = [ { From = "me@mydomain\.com", To = "me@mydomain\.com" }, ] body = [ ["Dear me@mydomain"], ["Special offer"], ]
[[rules]] action = [ ["/usr/local/libexec/dovecot/deliver", "-d", "todd", "-m", "junk"], ] filter = ["/usr/local/bin/spamc"] headers = [ { X-Spam-Status = "Yes" }, ]
[[rules]] action = [ ["/usr/local/libexec/dovecot/deliver", "-d", "todd"], ["/usr/local/bin/notifynewmail"], ] ```
A configuration file can be tested using the -t
option. The config test will parse mailproc.conf
and verify that the program given in any action
or filter
rule elements exists and is executable, and that any regular expressions found in the headers
, body
and raw
sections parse correctly. A successful test will print Config OK
and return exit status 0
. A failed test will print Config FAIL
and return exit status 1
, along with any error output.
$ mailproc -t
Config OK
$ echo $?
0
To pass mail through mailproc
, a .forward
file can be used:
$ cat $HOME/.forward
|/usr/local/bin/mailproc