Using spamassassin on a remote IMAP Host

I have the fortunate situation that i have hosted mail on a trusted second party (my father) so i do not have to deal with anything mail server related. I have a domain for receiving mails and i use a wildcard address to receive all the mails.

Until now

... I had the following setup:

  1. Wherever i sign up with a mail address i use something like mailto:<service-domain>@<my-domain>
  2. imapfilter pre-sorts mails based on the signup address
  3. offlineimap retrieves new mails and puts them in my $HOME/Mail
  4. mutt + msmtp to read and send mails

Everything worked fine but my imapfilter list for spam grew above the 100 entries because some of my addresses were leaked by the web service i was using. Most spam mails got sorted correctly into my Spam folder but some survived the sorting.

Spamassassin to the rescue

Everybody seems to be using spamassassin and i always thought it can only be used with procmail and real mail retrieval. Turns out is what may solve all my spam problems. It logs into the imap host and filters new mails with the help of spamassassin.

Installation and preparation:

# install sa
aptitude install spamassassin pyzor
# enable SA, activate 'allow-tell'
sed -i -e 's#ENABLED=.*#ENABLED=1#' \
       -e 's#OPTIONS=.*#OPTIONS="-4 --create-prefs --max-children 5 --helper-home-dir --allow-tell"#' \

update-rc.d spamassassin enable
/etc/init.d/spamassassin start

# install IMAP spam begone
pip install isbg

# download pyzor db for spamassassin
pyzor --homedir /etc/mail/spamassassin discover

Configuration and learning:

# configure SA to your liking
cat > $HOME/.spamassassin <<EOF
required_score          5.0
report_safe             0
use_bayes               1
bayes_auto_learn        1
skip_rbl_checks         0
use_razor2              1
use_dcc                 1
use_pyzor               1
ok_languages            en de
ok_locales              en de
/etc/init.d/spamassassin restart

# make SA learn your spam folder via isbg,also save the password --imaphost <imap-domain> --ssl --imapuser <imap-username> --spaminbox INBOX.Spam --spamc --teachonly --learnspambox INBOX.Spam --savepw

vim $HOME/.offlineimaprc
# change Presynchook = imapfilter to $HOME/bin/filter_mail

cat > $HOME/bin/filter_mail <<EOF --verbose --imaphost <imap-domain> --ssl --imapuser <imap-username> --spaminbox INBOX.Spam --delete --expunge --noninteractive  --spamc
# dont worry, --delete --expurge only deletes the messages from your inbox (and essentially moves them to your Spam folder)

# create mutt macro to mark and move spam in your inbox
echo 'macro index,pager S "| sa-learn --spam\ns=INBOX.Spam\n\n" "file as Spam"' >> $HOME/.muttrc
# optional: disable the 'press any key to continue' :
# echo 'set wait_key = no' >> $HOME/.muttrc

After learning my >9000 spam mails in the spam folder the spamassassin bayes filter was trained pretty well. Now i was able to remove all the 'spam' rules from .imapfilter/config.lua, and it felt GREAT.