summaryrefslogtreecommitdiffstats
path: root/content/posts/remote-spamassassin.rst
blob: 999e28cb9e1b18443a65652b246504d032320a20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
Using spamassassin on a remote IMAP Host
##################################
:date: 2014-04-15 14:20
:tags: spamassassin, mutt,offlineimap,imapfilter


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 isbg.py_ 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:
.. code-block:: bash
   
    #?/bin/sh
    # 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"#'  /etc/default/spamassassin

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

    # install IMAP spam begone
    pip install isbg

Configuration and learning:
.. code-block:: bash
   
    #?/bin/sh
    # 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
    score SUBJ_ILLEGAL_CHARS      0
    EOF
    /etc/init.d/spamassassin restart
    
    # make SA learn your spam folder via isbg,also save the password
    isbg.py --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
    isbg.py --verbose --imaphost <imap-domain> --ssl --imapuser <imap-username> --spaminbox INBOX.Spam --delete --expunge --noninteractive  --spamc
    imapfilter
    EOF
    # 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: disbale 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.