Spam folder creation

Credits

Various bits of code, scripts, and procedures were put together with information from John Simpson's qmail.jms1.net website. It's an excellent resource on managing and setting up a Qmail server.

Automatically create set of IMAP folders for sorting detected spam out of users' Inboxes and/or training a spam filter.

Spam folder creation for all users
For training a spam folder such as dspam, we'll give our users a "Spam" folder with subfolders for messages that were spam as well as folders for false positives and false negatives. We can install a global mailfilter on a domain that will make sure that those IMAP folders exist.
Create the Maildrop filter /home/vpopmail/domains/.mailfilter:

SHELL="/bin/sh"
import EXT
import HOST
VPOP="| /home/vpopmail/bin/vdelivermail '' bounce-no-mailbox"
VHOME=`/home/vpopmail/bin/vuserinfo -d $EXT@$HOST`
VDHOME=`dirname "$VHOME"`

# Test for the existance of the Spam directories.
# Create them if they don't exist

`test -d $VHOME/Maildir/.Spam`
if ( $RETURNCODE == 1 )
{
  `/var/qmail/bin/maildirmake -f Spam $VHOME/Maildir`
  `echo INBOX.Spam >> $VHOME/Maildir/courierimapsubscribed`
  `echo "Spam" >> $VHOME/Maildir/subscriptions`
}

`test -d $VHOME/Maildir/.Spam.MissedSpam`
if ( $RETURNCODE == 1 )
{
  `/var/qmail/bin/maildirmake -f Spam.MissedSpam $VHOME/Maildir`
  `echo INBOX.Spam.MissedSpam >> $VHOME/Maildir/courierimapsubscribed`
  `echo "Spam.MissedSpam" >> $VHOME/Maildir/subscriptions`
}

`test -d $VHOME/Maildir/.Spam.NotSpam`
if ( $RETURNCODE == 1 )
{
  `/var/qmail/bin/maildirmake -f Spam.NotSpam $VHOME/Maildir`
  `echo INBOX.Spam.NotSpam >> $VHOME/Maildir/courierimapsubscribed`
  `echo "Spam.NotSpam" >> $VHOME/Maildir/subscriptions`
}

# Pass unfiltered messages to $VPOP for delivery to user

to "$VPOP"

Adjust the ownership and permissions on the filter:

chown vpopmail:vchkpw /home/vpopmail/domains/.mailfilter
chmod go-r /home/vpopmail/domains/.mailfilter

Edit the .qmail-default file for the domain that you want to implement the filter on (i.e. /home/vpopmail/domains/example.com/.qmail-default). Normally, the file will contain:

| /home/vpopmail/bin/vdelivermail '' bounce-no-mailbox

To run the filter instead, change the contents to:

|/var/qmail/bin/preline /usr/local/bin/maildrop /home/vpopmail/domains/.mailfilter

Sample filter
Interesting example of maildrop coding:

# Auto-Create new mailbox
if (/^Received:/)
{
  `test -d $DEFAULT`
  if ($RETURNCODE == 1)
  {
    `/usr/local/bin/maildirmake $DEFAULT`
    `/usr/local/bin/maildirmake -f Spam $DEFAULT`
    `echo "Spam" $DEFAULT/subscriptions`
  }
}

# Deliver to Inbox or Spam box (create spam box if it does not exist)
if (/^X-Spam-Flag: YES/:h)
{
  `test -d $DEFAULT/.Spam`
  if ($RETURNCODE == 1)
  {
    `/usr/local/bin/maildirmake -f Spam $DEFAULT`
    `echo "Spam" $DEFAULT/subscriptions`
  }
  exception {
    to "$DEFAULT/.Spam"
  }
  # if all else fails, do regular delivery
  exception {
    to "$DEFAULT"
  }
}
else
{
  exception {
    to "$DEFAULT"
  }
}

Recent Updates

  • 6 months 3 weeks ago
    1.27.2 update
  • 7 months 5 days ago
    Drupal 10/11 config
  • 7 months 6 days ago
  • PHP
    7 months 6 days ago
    PHP 8.3.11 and AlmaLinux
  • 7 months 1 week ago
    New version of Pound