[TUTORIAL] Train SpamAsssassin with mail of an IMAP mailbox

Discussion in 'Mail Gateway: Installation and configuration' started by magenbrot, Nov 24, 2018.

  1. magenbrot

    magenbrot New Member

    Joined:
    Mar 1, 2018
    Messages:
    22
    Likes Received:
    7
    Hi,

    I've created a script which fetches mails from a special IMAP mailbox, where I forward spam and ham to, and feeds them to sa-learn as spam or ham. If anyone is interested to try out you can get all the details in the github repository: https://github.com/magenbrot/sa_learn_from_imap

    Here's a more detailed description:

    First up you'll need an IMAP mailbox where you and your customers redirect SPAM and HAM to. I've a mailbox called filter@mymails.org. Since I use postfix (I don't know what other MTAs do), I can use a + in my recipient address (see recipient_delimiter documentation of Postfix). So I'm forwarding all spam to filter+spam@mymailbox.org and all ham to filter+ham@mymailbox.org.

    Furthermore I have a sieve-script running on my server, which will move the incoming mails by the recipient to the correct folder.

    Now about training the SpamAssassin: The script has 3 required, positional arguments. The IMAP-host, -user and the -password. By default it assumes that your SPAM mails are sorted in a folder called 'spam' and your HAM mails are filed in 'ham'.

    This can be changed with the parameters --spam-folder and --ham-folder. It needs a working directory, this is /tmp/sa-learn by default and can be changed with the --directory parameter. Finally you can specify the sa-learn script it should use with --sa-learn. This defaults to /usr/bin/sa-learn.

    Please call the script with the user you're running SpamAssassin with.

    Example (run with python3, either directly or in the venv from above):

    Code:
    ./sa_learn_from_imap.py mail.mymails.org 'filter@mymails.org' 'password123$'
    A cronjob could look like this, put this in /etc/cron.d/sa-learn-ham-spam:

    Code:
    #MAILTO="postmaster@mymails.org"
    MAILTO=""
    
    # run with bash, as sh doesn't support 'source'
    SHELL=/bin/bash
    
    15 * * * * root source /opt/sa_learn_from_imap/env/python3/bin/activate && \\
              /opt/sa_learn_from_imap/sa_learn_from_imap.py mail.mymails.org 'filter@mymails.org' 'password123$'
    
     
    #1 magenbrot, Nov 24, 2018
    Last edited: Nov 26, 2018
    killmasta93 likes this.
  2. killmasta93

    killmasta93 Member

    Joined:
    Aug 13, 2017
    Messages:
    255
    Likes Received:
    8
    very interesting could you post your github repo?
     
  3. magenbrot

    magenbrot New Member

    Joined:
    Mar 1, 2018
    Messages:
    22
    Likes Received:
    7
    killmasta93 likes this.
  4. killmasta93

    killmasta93 Member

    Joined:
    Aug 13, 2017
    Messages:
    255
    Likes Received:
    8
    very nice, theres few things im somewhat lost correct me if im wrong.

    1) So you used recipient_delimiter instead of filterspam@mymailbox.org and filterham@mymailbox.org? so when users have spam emails
    they would forward that email to filterspam@mymailbox.org?
    2) then you have a sieve-script which would be located on your postfix email server and not proxmox? how does it know what folder to put in? even if in the sieve filter says if header contains but doesn't the header change when the user forwards the email?


    Thanks
     
  5. magenbrot

    magenbrot New Member

    Joined:
    Mar 1, 2018
    Messages:
    22
    Likes Received:
    7
    I've called the mailbox just filter@mydomain.com. With the delimiter you can add +ham or +spam for example to distinguish between Ham and Spam. So the complete address would be filter+spam@mydomain.com or filter+ham@mydomain.com.

    You can use that for example with roundcube and the markasjunk plugin, so your users can select the mail and simply click the Ham or Spam button. markasjunk will then forward it to the address in the configuration.

    Yes, the mailbox is on my "normal" IMAP server, not on proxmox. But the script runs on my proxmox gateway because there's my spamassassin installation. The script doesn't depend on PMG, it can be used with every SA based mail filter.

    The sieve filter just matches if the recipient address contains +spam or +ham and moves the mail to the according folder.

    Code:
    if header :contains "Received" "+spam" {
      fileinto "spam";
      stop;
    } elsif header :contains "Received" "+ham" {
      fileinto "ham";
      stop;
    }
     
    killmasta93 likes this.
  6. killmasta93

    killmasta93 Member

    Joined:
    Aug 13, 2017
    Messages:
    255
    Likes Received:
    8
    Thanks for the reply, now i get it, out of curiosity im guessing it only works for IMAP? and not POP? also if the user have IMAP could they use outlook or they have to use Roundcube for the plugin?
     
  7. magenbrot

    magenbrot New Member

    Joined:
    Mar 1, 2018
    Messages:
    22
    Likes Received:
    7
    killmasta93 likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice