Skip to content. | Skip to navigation

Personal tools


You are here: Home / Notes / Move spam mails to users accessible folders

Move spam mails to users accessible folders

EDIT. Per user spam filters in addition - "".

Thanks to all who share their knowledge and experience.


1. amavisd.conf
List all hosted mail domains in local_domains_maps, so that Amavisd will insert mail headers for them if necessary.
@local_domains_maps = ['.'];
Do not quarantine to out of reach directory for users and do not bounce messages:
$spam_quarantine_to  = undef;
$final_spam_destiny = D_PASS; # (defaults to D_BOUNCE)
2. dovecot.conf
To use Sieve, you will first need to make sure you are using Dovecot LDA or LMTP for delivering incoming mail to users' mailboxes. Then, you need to enable the Pigeonhole Sieve plugin in your configuration:
protocol lda {
mail_plugins = $mail_plugins sieve
protocol lmtp {
mail_plugins = $mail_plugins sieve
Define Junk folder in dovecot.conf, as described "" :
mailbox Junk {
auto = subscribe # autocreate and autosubscribe the Junk mailbox
special_use = \Junk
Like this sample of namespace with defined Junk folder:
namespace {
inbox = yes
location =
prefix =
separator = .
type = private
mailbox Junk {
auto = subscribe # autocreate and autosubscribe the Junk mailbox
special_use = \Junk
Add large quota rule for Junk folder to dovecot's plugin section:
plugin {
quota_rule = Junk:storage=+1000%%
Execute sieve global script before user scripts:
plugin {
# Scripts executed before the user's script.
#   E.g. handling messages marked as dangerous
sieve_extensions = +spamtest +spamtestplus
sieve_spamtest_status_type = score
sieve_spamtest_status_header = X-Spam-Score: (-?[[:digit:]]+\.[[:digit:]]).*
sieve_spamtest_max_value = 15
sieve_before = /usr/local/etc/dovecot/sieve/global.sieve
3. Sieve script.
/usr/local/etc/dovecot/sieve/global.sieve contains rules to filter messages by "X-Spam-Score" (found in message header):
require "spamtestplus";
require "fileinto";
require "relational";
require "comparator-i;ascii-numeric";

if spamtest :value "gt" :comparator "i;ascii-numeric" :percent "85" {
fileinto "Trash";

/* If the spamtest score (in the range 1-10) is larger than or equal to 3,
* file it into the spam folder:
} elsif spamtest :value "ge" :comparator "i;ascii-numeric" "3" {
fileinto "Junk";
/* Other messages get processed by next sieve filters or filed into INBOX */
Test sieve script:
$ sieve-test -t - -Tlevel=matching /usr/local/etc/dovecot/sieve/global.sieve ~/test.eml sure to pre-compile the specified script manually in that case using the sievec command line tool, as explained here.
# sievec /usr/local/etc/dovecot/sieve/global.sieve
4. Cleanup.
Now all SPAM marked emails are moved into the spam folder. To clean up space, schedule cleaning spam emails older than ten days for all users. Add following line  to crontab
0  4    * * *   root    find /var/vmail -type f -a \( -wholename "*/.Junk/cur/*" -o -wholename "*/.Junk/new/*" \) -a -mtime +7 -delete &> /dev/null

5. Reload dovecot and amavis.