Filtering spam in Postfix is pretty simple. There’s some advanced techniques you can use, but simply setting up Spamassassin will suit many people. One downside is seeing all the ***** SPAM ***** mails in your inbox. It took a while to come up with a solution, but the best fit so far has been implementing Maildrop to automatically move those files to a Junk folder. Here’s the steps to set this up on a Debian 5.0 system with Postfix and Spamassassin.
First, setup your /etc/maildroprc file:
# commands and variables for making the mail directories maildirmake=/usr/bin/maildirmake mkdir=/bin/mkdir rmdir=/bin/rmdir MAILDIR=$DEFAULT # make the user's mail directory if it doesn't exist `test -e $MAILDIR` if ($RETURNCODE != 0) { `$mkdir -p $MAILDIR` `$rmdir $MAILDIR` `$maildirmake $MAILDIR` } # make the .Junk folder if it doesn't exist JUNK_FOLDER=.Junk _JUNK_DEST=$MAILDIR/$JUNK_FOLDER/ `test -d $_JUNK_DEST` if ($RETURNCODE != 0 ) { `$maildirmake $_JUNK_DEST` #auto subscribe. the following works for courier-imap `echo INBOX.Junk >> $MAILDIR/courierimapsubscribed` } # If the Spam-Flag is set, move the mail to the Junk folder if (/^X-Spam-Flag:.*YES/) { exception { to $DEFAULT/.Junk/ } }
The comments clearly state what’s going on there.
Once that’s setup, you will go into your /etc/postfix/master.cf and make sure the
maildrop unix - n n - - pipe flags=DRhu user=vmail argv=/usr/bin/maildrop -d ${recipient}
is not commented out.
Next you will have to set the /usr/bin/maildrop file setuid root. This is so maildrop can interact with authdaemon and the mail folders.
#chmod +s /usr/bin/maildrop
Then you have to add this to your /etc/postfix/main.cf file:
virtual_transport = maildrop maildrop_destination_recipient_limit = 1
If there is another virtual_transport line, be sure to comment that out first.
Last, set the permissions on the authdaemon so that maildrop can access it.
chown vmail /var/run/courier/authdaemon
And that’s all. Nice and simple way to handle all that Junk mail.
#1 by Mario on February 21, 2010 - 11:04 pm
Hello, I have a Postfix with MySQL virtual domains, using this guide: http://bit.ly/gHxde but when I change the last two lines in main.cf (virtual_transport = maildrop
maildrop_destination_recipient_limit = 1) I got this error when sending mail to my users:
I’m sorry to have to inform you that your message could not
be delivered to one or more recipients. It’s attached below.
For further assistance, please send mail to postmaster.
If you do so, please include this problem report. You can
delete your own text from the attached returned message.
The mail system
: user unknown. Command output: ERR: authdaemon:
s_connect() failed: Permission denied Invalid user specified.
Final-Recipient: rfc822; [email protected]
Original-Recipient: rfc822;[email protected]
Action: failed
Status: 5.1.1
Diagnostic-Code: x-unix; ERR: authdaemon: s_connect() failed: Permission denied
Invalid user specified.
#2 by steve on February 22, 2010 - 12:27 am
In this line right here:
maildrop unix – n n – – pipe
flags=DRhu user=vmail argv=/usr/bin/maildrop -d ${recipient}
that is placed in your master.cf you notice a user=vmail argument. Change this user= to the user appropriate for your system. This is the user that actually writes to the disk where the mail directory is stored.
I hope this helps.
#3 by Matt on March 13, 2010 - 8:23 pm
Hi,
I have my mail server setup like http://www.howtoforge.com/virtual-users-domains-postfix-courier-mysql-squirrelmail-ubuntu9.04
I’ve setup and got maildrop working on as far as moving the junk to the right place etc.
However it’s creating /home/vmail/Maildir/
and putting it all in there, Do you know why that would be?
#4 by steve on March 13, 2010 - 8:51 pm
The system thinks the home directory is /home/vmail. I wish I could tell you why but I can’t without more information.
#5 by Matt on March 13, 2010 - 8:54 pm
FYI i’m set up as follows
/home/vmail/domain/user
I’d appreciate the help with this, I can’t find anything on the net :(
#6 by steve on May 13, 2011 - 4:07 pm
Hi there – I’m not sure if you found this out or not, but I have recently deployed a new server and ran into this same issue.
The problem is that maildrop can’t access the authdaemon socket to check to see if the user exists.
On my system the authdaemon socket dir is: /var/run/courier/authdaemon
By default the permissions are:
drwxr-x— 2 daemon daemon 4096 May 12 17:49 authdaemon
and I ran: chown vmail /var/run/courier/authdaemon
to fix the issue.
I hope this helps someone.
#7 by Blaze on March 3, 2012 - 12:44 am
Hi!
First of all, thanks for this file, I would have never been able to figure it out myself.
I have a Postfix+Courier server with virtual users’ data stored in mysql database.
Maildrop can figure out the correct Maildir, if yoy add one more line to the beginning of maildroprc:
DEFAULT=”$HOME/Maildir”
In /etc/courier/authmysqlrc there is a line MYSQL_HOME_FIELD. My guess is that $HOME is set to this field’s value. To be exact, the value the query gets from mysql.
Thanks once more!
#8 by esprit on January 27, 2013 - 8:05 pm
Hi, thanks for the guide. It almost seems to work for me. For whatever reason all mails regardless of domain get thrown into one file named /home/vmail/Maildir where Maildir is that file where I find all emails.
Do you have an idea why that happens and how to solve it?
Thank you!
#9 by Badijon on October 9, 2013 - 4:21 am
Hi !
Just wonderfull !! It works like a charm for me (Postfix + Mysql Virtual Users / Domains).
But I prefer to use the folder name “Spam” for the spams.
So I replace every “Junk” by “Spam” in /etc/maildroprc file and it works !
Thank you very much for this nice and clear guide !