4 个版本
0.3.2 | 2022年12月25日 |
---|---|
0.3.1 | 2021年2月1日 |
0.3.0 | 2019年6月2日 |
0.2.1 | 2019年4月22日 |
#116 在 电子邮件 中排名
每月 26 次下载
23KB
425 行
mailproc
mailproc 从标准输入读取电子邮件,并根据其内容执行操作。它可以用来根据内容将电子邮件存档到不同的文件夹,删除不想要的邮件,或根据邮件内容执行其他任务。它旨在作为 procmail 的替代品。
邮件处理规则在以 TOML 格式化的配置文件中指定,存储在 $HOME/.mailproc.conf
中。
配置文件包括一个版本号(1)和一个规则数组。每个规则都必须符合程序源代码中给出的以下规范
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>>,
}
规则的所有元素都是可选的。一个空的规则匹配所有消息并且不执行任何操作(消息被丢弃)。没有匹配到规则的邮件将被丢弃。如果一个规则匹配,那么规则的 headers
、body
和 raw
部分都匹配,或者被省略。以下是对每个规则元素的描述
-
headers
:一个表格数组,指定要匹配的标题元素以及匹配它们的正则表达式。为了使headers
匹配,提供的任何表格都必须匹配其所有标题元素。例如,以下表格{ From = "you@example\\.com", To = "me@example\\.com" }
将会匹配,如果 From 和 To 消息标题都匹配给定的模式。指定多个表格以匹配任何一组标题值。 -
body
:用于在消息正文中匹配正则表达式的一组集合。类似于headers
,body
将匹配任何一组正则表达式,只要它们的所有表达式都匹配即可。 -
raw
:用于匹配原始消息的正则表达式的一组集合。类似于body
,但匹配整个原始消息。 -
action
:要执行的命令数组。每个命令指定为一个字符串数组,每个命令参数一个字符串,并将电子邮件消息提供给命令作为 stdin。例如,要运行 dovecot 的deliver
命令,您可以提供一个如下操作:["/usr/local/libexec/dovecot/deliver", "-d", "todd"]
。 -
filter
:指定为字符串数组的命令。如果提供,则将消息通过过滤器程序,并使用输出与规则中剩余部分进行匹配。
配置文件可能如下所示
# Configuration file format version
version = 1
# File mail from mailinglist.example.com in folder mailinglist
[[rules]]
action = [
["/usr/local/libexec/dovecot/deliver", "-d", "todd", "-m", "mailinglist"],
]
headers = [
{ List-ID = "mailinglist\\.example\\.com" },
]
# Rules that have no action mean the message will be dropped. Each table in
# the headers is tested for matches independently, and if all of the patterns
# in a table match then the headers match. So mail with a From matching "AnnoyingSender"
# OR a Subject of exactly "Buy pills online" will match this rule.
[[rules]]
headers = [
{ From = "AnnoyingSender" },
{ Subject = "^Buy pills online$" },
]
# Match spam with either of these specific phrases anywhere in the raw message text.
# Again, no action means the message will be dropped.
[[rules]]
raw = [
["a large sum of money"],
["limited time offer"],
]
# Rules can match on headers, body, raw, or any combination.
# Here, any email where the From and To match "[email protected]" AND
# which have body text matching "Dear me@mydomain" OR "Special offer"
# will match the rule and the action will run.
[[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"],
]
# Messages can be passed through a filter before matching.
# Here we pass the message through spamassassin and check the output
# for 'X-Spam-Status: Yes' in the headers.
[[rules]]
action = [
["/usr/local/libexec/dovecot/deliver", "-d", "todd", "-m", "junk"],
]
filter = ["/usr/local/bin/spamc"]
headers = [
{ X-Spam-Status = "Yes" },
]
# Rules with no headers, body, or raw parts always match, and multiple actions can be specified.
# This is the default action.
[[rules]]
action = [
["/usr/local/libexec/dovecot/deliver", "-d", "todd"],
["/usr/local/bin/notifynewmail"],
]
可以使用 -t
选项测试配置文件。配置测试将解析 mailproc.conf
并验证任何 action
或 filter
规则元素中给出的程序是否存在并且可执行,以及在 headers
、body
和 raw
部分中找到的正则表达式是否正确解析。成功的测试将打印 Config OK
并返回退出状态 0
。失败的测试将打印 Config FAIL
并返回退出状态 1
,以及任何错误输出。
$ mailproc -t
Config OK
$ echo $?
0
要通过 mailproc
传递邮件,可以使用 .forward
文件
$ cat $HOME/.forward
|/usr/local/bin/mailproc
依赖关系
~10–19MB
~310K SLoC