Procmail has an -m
option which lets you test a rule file without delivering a message anywhere; it turns off delivering to your inbox $DEFAULT
if none of your rules matched. The option requires you to pass in the file name of the rule file you want to run, and allows you to set variables like VERBOSE=yes
on the command line.
procmail -m test.rc VERBOSE=yes <test.msg
Perhaps see also https://www.iki.fi/era/mail/procmail-debug.html
(old but still vaguely relevant).
For your concrete rule, it's prone to false positives. Remember that the regex engine will accept any partial match; so your rule will trigger on e.g.
Received: from postfix.rules.example.com ...
because it matches the substring .ru
. You can somewhat guard against this by requiring a word boundary after ru
:
:0
* ^Received:.*\.ru\>
.Spam/
Notice also how the regular expression for "any text" is .*
, where .
matches any one character, and *
says to repeat the previous expression as many times as possible, but accept zero repetitions, too. (Thus your attempt would permit zero or more colons after Received
but only if immediately followed by the literal text .ru
.)
You might be able to tighten this up some more, but Received:
headers are notoriously poorly standardized. Many servers run Postfix or Sendmail which both create Received:
headers where the first portion after the colon indicate from
HELONAME (RDNS [IP]) where IP is the actual IP address, RDNS is the result of a reverse DNS lookup (possibly empty) and HELONAME is tha name the remote client indicated for itself when it initiated the SMTP transaction with a HELO
or EHLO
command (the client can put anything they want here; some put obvious forgeries, which are good spam filter fodder!) ... but also, many other servers run different software which uses a different format, or encourages the local admin to configure their own format (ugh, Exim). The Received:
headers near the top of each message are from servers which are the most local to you, and thus somewhat more predictable and reliable than the more distant ones, which could easily contain entirely made-up information.
Anyway, this is ultimately somewhat unsatisfying. If you can convince your ISP to block these unwanted messages during SMTP transmission (effictively killing the delivery attempt as if your mailbox was unavailable) that's far more efficent. Some providers offer their users access to e.g. SpamAssassin which lets you block messages in the Russian language (though the precision is not exactly stellar; the mechanism is not very good at distinguishing between e.g. Russian and Serbian, so you'll want to block or allow all Cyrillic languages actually) and some will block traffic on the IP level so that known spammers are unable to even connect. But as a last line of defense against the vandals, Procmail is obviously vastly better than nothing.