Can you do if-then style statements in SpamAssassin?
I get spam sent to me that uses my email address for the sender's name and I would like to write a general rule for this.
For example, I receive spam messages with From: and To: lines like this:
From: "[email protected]" <[email protected]>
To: <[email protected]>
Below I refer to this format as:
From: "Name" <address>
To: <address>
Is it possible to write a rule that says:
if
the (From: name)
is equal to (To: email address)
but not the (From: email address)
then
give it a score?
I am thinking this specifically in case my server automatically sends messages in a similar format, such as: "[email protected]" <[email protected]>.
I don't want the rule to accidentally score emails like that.
I only see how to write positive rules. So I can look for these kinds of simple matches
header LOCAL_FROM_NAME_MyAddress From =~ /\"[email protected]\"/
header LOCAL_FROM_Address_MyAddress From =~ /<[email protected]>/
header LOCAL_TO_Address_MyAddress From =~ /<[email protected]>/
So I could create a score if they all produced a match:
meta LOCAL_FROM_ME_TO_ME ((LOCAL_FROM_NAME_MyAddress + LOCAL_FROM_Address_MyAddress + LOCAL_TO_Address_MyAddress) >2)
score LOCAL_FROM_ME_TO_ME -0.1
But that is as far as I can go. I haven't seen any way to do something more complex.
SpamAssassin
metarules support boolean expressions, so you can use the&&,||, and!operators to create more complex matches. In the specific example you've given, the rule is logically equivalent to:A ruleset to express this could be:
If meta rules and boolean expressions are not enough, you can write a Perl plugin. Check out the many examples on CPAN, and perhaps specifically Mail::SpamAssassin:FromMailSpoof.
Notes
You can write
:nameand:addrto parse specific parts of theFromandToheaders.You can prefix your sub-rules with
__so that they will not score on their own.Special characters like
@and.should be escaped in regex patterns.