Gmail – Making Gmail filter out spam based on spamassassin headers

gmailgmail-filtersspam-prevention

I have an email account that forwards mail to my Gmail account. It looks like that Gmail is less strict to the emails that are forwarded to it compared to those that come to Gmail directly. It means that a lot of spam forwarded from non-Gmail account end up in the Gmail inbox.

The forwarded messages are processed by SpamAssassin that adds following headers:

X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on myhost.org
X-Spam-Flag: YES
X-Spam-Level: ******
X-Spam-Status: Yes, score=6.4 required=5.0 tests=BAYES_99,[...]
X-Spam-Report: 
    *  4.0 BAYES_99 BODY: Bayes spam probability is 99 to 100%
    [more rules that matched]

I don't see any way to filter based on any of those headers. Gmail only looks for a few specific keywords in the header. When matching arbitrary words, Gmail checks the message body but not the headers.

I have shell access to that system, so I created a .procmailrc file that I'm going to post. It solves my problem by adding a fake Delivered-To. I would welcome refinements to that file and even more so suggestions that don't rely on the shell access.

Best Answer

That's my .procmailrc with irrelevant parts removed

SENDER=`formail -c -x Return-Path`
SENDMAILFLAGS="-oi -f $SENDER"

:0
* ^X-Spam-Level: \*\*\*\*\*\*\*\*\*\*\*\*
| formail -A 'Delivered-To: spam@gmail.com' | \
  $SENDMAIL -oi myname@gmail.com

:0
! myname@gmail.com

The number of asterisks after X-Spam-Level corresponds to the SpamAssassin score (12 for now, I'll probably decrease it). That is, if the score is 12 or above, the recipe would match.

myname@gmail.com stands for my real Gmail address.

spam@gmail.com is written literally. It's just a token for the Gmail filter.

The Gmail filter is created on the Gmail page:

Sprocket icon -> Settings -> Filters tab -> Create a new filter

Has the words: deliveredto: spam@gmail.com

Check "Skip the Inbox (Archive it)"

Check "Apply the label" -> "New Label..." -> spamdetected

Press "Create Filter" button

With that filter, the email forwarded by my other system goes to the "spamdetected" folder, not to the in the inbox. There is no notification on my phone, yet I can access the filtered messages from my phone and nothing is permanently lost.

It turns out most spam is actually detected by Gmail and goes to the Spam folder. I report the messages from the "spamdetected" folder as spam manually. I hope it helps Gmail deal with future spam.