server-side mail filtering using qmail/procmail under plesk

July 14, 2007

Technology, Work

After enabling Spam Assassin on my Plesk Managed Media Temple DV3 Server, I had been relying on email clients to move spam-tagged emails into a “Junk” folder.

This was not working out on my newly purchased Iphone, I was receiving Spam mail in my inbox, over 100+ every so many hours.

It only worked when the e-mail client was open, rules/filters had to be set up on the email client, and the rules/filters do not transfer to the phone mail client.

After a lot of digging around I worked out a server-side solution. The key app here is procmail, which runs rules on incoming mails and can file them into folders. Procmail should already be present on your Plesk installation.

The following steps apply to my mailbox “russ” on domain “wittmannwebweaving.com” You will need to substitute your own mailbox and domain.

Step One:
Create /var/qmail/mailnames/wittmannwebweaving.com/russ/.procmailrc

MAILDIR=/var/qmail/mailnames/wittmannwebweaving.com/russ/Maildir
DEFAULT=${MAILDIR}/
SPAMDIR=${MAILDIR}/.Junk/
LOGFILE=${MAILDIR}/procmail.log
LOG="--- Logging ${LOGFILE} for ${LOGNAME} "
# All mail tagged as spam (eg. with a score higher than the set threshold)
# is moved to the designated spam folder
:0
* ^X-Spam-Status: Yes.*
${SPAMDIR}

Step Two:
Edit /var/qmail/mailnames/wittmannwebweaving.com/russ/.qmail
so that it contains only the following two lines:

| /usr/local/psa/bin/psa-spamc accept
|preline /usr/bin/procmail -m -o .procmailrc

That’s it pretty simple! Let me know your feedback, its working great for me.

About Russ Wittmann

Russ Wittmann is Principal of Wittmann Web Weaving Established in 1999. The firm Specializes in XHTM, CSS, Ruby on Rails, and Design.

View all posts by Russ Wittmann

31 Responses to “server-side mail filtering using qmail/procmail under plesk”

  1. Dain Says:

    Thanks Russ!

    Procmail is a great little app that will makes life much easier for (dv) users, those that can figure it out that is… but this is a very well done and simple article! I’ll see what I can do about getting it into the KB.

    Cheers,
    Dain

    Reply

  2. Russ Wittmann Says:

    No Problem,

    I’m also working on getting a SVN article up soon as-well

    Reply

  3. Ed Shull Says:

    This is a great suggestion.

    Reply

  4. David Kaneda Says:

    Great tip – just wondering, is there a way to add this to all accounts automatically?

    Reply

  5. Russ Wittmann Says:

    I’ll have to see, right now its a manual process

    Reply

  6. David Kaneda Says:

    Also, I just realized I’m getting this error:

    :
    Usage: spamc [options]

    Reply

  7. David Kaneda Says:

    Crud, that didn’t come through:

    Usage: spamc [options]

    Reply

  8. Russ Wittmann Says:

    What’s the error? What kind of server are you on? Do you have spam assassin?

    Reply

  9. Jonathan Says:

    Do you have to make that “Junk” folder ahead of time or do these scripts take care of that?

    Reply

  10. Russ Wittmann Says:

    Nope, the scrip makes it when Junk is received, send your self an email with this is the message body to test it out.
    XJS*C4JDBQADN1.NSBN3*2IDNEN*GTUBE-STANDARD-ANTI-UBE-TEST-EMAIL*C.34X

    Reply

  11. Jonathan Says:

    Ok that test message came in marked as spam but it did not go into a Junk folder. I did want to point out that I am on one of the new DV 3.5 servers and I’m wondering if that changes some of the setup here. Thanks for all your help.

    Reply

  12. Tarak Says:

    Hi ,
    Hi Friends,
    After implementing procmail my mails are stucking in th queue. here is the .procmailrc file

    MAILDIR=/home/vpopmail/domains/example.com/tarak.ranjan/Maildir
    DEFAULT=${MAILDIR}
    SPAMDIR=${MAILDIR}/.Spam/
    LOGFILE=${MAILDIR}/procmail.log
    LOG=”- Logging ${LOFILE} for ${LOGNAME} ”

    :0
    * ^X-Spam-Status: Yes.*
    ${SPAMDIR}

    and my .qmail file contain

    |preline /usr/bin/procmail -m -o .procmailrc
    #/home/vpopmail/domains/liqwidkrystal.com/tarak.ranjan/Maildir/

    , im not able to find out where going wrong.
    qmail-send log
    @4000000048130959266bb75c status: local 1/10 remote 0/30
    @400000004813095926f34e4c delivery 159: deferral: procmail:_Couldn’t_create_”/var/mail/vpopmail”/
    @400000004813095926f47b14 status: local 0/10 remote 0/30

    Reply

  13. lee Says:

    What about when mchk runs? That rebuilds all the .qmail files, and either will drop the changes, or fail if the file is immutable:
    “/usr/local/psa/admin/sbin/mchk –with-spam
    mchk: Unable to truncate or create file .qmail: Permission denied
    System error 13: Permission denied
    mchk: Unable to truncate or create file .qmail: Permission denied
    System error 13: Permission denied
    mchk: Unable to create .qmail file

    System error 13: Permission denied
    mchk: Unable to create .qmail file

    System error 13: Permission denied”

    That will leave the qmail system in an inconsistent state, and bork any upgrades.

    I find with Plesk it is best to color within the lines.

    Reply

  14. Russ Wittmann Says:

    @lee I have had no issues with this solution at all running on a DV3 server at mediatemple.

    What are you running on?

    Reply

  15. Keith Says:

    Thanks so much for the tip. One question and one comment:

    Comment: I had to ‘chmod popuser:popuser .procmailrc’ so procmail could read it’s own rc file.

    Question: My existing .qmail file looked like:
    | /usr/local/psa/bin/psa-spamc accept
    | true
    | /usr/bin/deliverquota ./Maildir
    Any idea on what lines 2 & 3 are for? Will they be missed a/o overwritten by plesk?

    Thanks again,
    Keith

    Reply

  16. Russ Wittmann Says:

    It looks like line three is the quota for the account, does the account have a quota?

    Reply

  17. Brandon M Says:

    Thank you very much. This is exactly what I’ve been looking for!

    I want to make SpamAssassin MARK all messages scored at 1.8 or higher, but I want it to THROW OUT (in this case, redirect) all mail with a score of 5.0 or higher.

    My variation uses:
    * ^X-Spam-Level: \*\*\*\*\*
    instead of
    * ^X-Spam-Status: Yes.*
    to only filter things that are at least 5.0

    The rest comes to the inbox so any ham marked as spam can be identified. Several of my bosses use Treo Smartphones and will love the fact that I’ve just removed 60% of their spam.

    Thank you, thank you, thank you!

    Reply

  18. Derek Says:

    Thanks for publishing this little tutorial. After experiencing up and down service with Google over the past few weeks I decided to move back to Media Temple for email. Everything was going well until I decided to setup the iPhone. Like you, I immediately received a barrage of spam to my iPhone inbox. Hopefully this filter will reduce the number of worthless emails from getting through. Do you have any other tips for teaching SpamAssassin? I’m just beginning to familiarize myself with server side email administration and found things about sa-learn. Are you running any automated scripts to scan spam / ham directories to improve spam filtering?

    Reply

  19. Erik Says:

    What if I want what to process mail such that if it doe NOT have “Spam-Status: Yes” in the header that it sends the mail to junk?

    -Erik

    Reply

  20. Teclo Says:

    Russ, you really saved my life.
    I’ve been forced to move my company website to a VPS in no time, due to problems with my dmz. It’s two days i’m having that annoying spam tag on mail titles and the think of setting up client rules in more then 40 machines made me sick. Your guide is so damn clear and short, and it works like a charm. Now all the spam go into a junk folder into my account, and using webmail i’m able to forward or delete the junk.
    Lovely!

    Reply

  21. Norm Says:

    I know this has been up a while but I’ve just switched to Media Temple and want to filter my junk the way you describe.

    I followed all the steps but my question is how do I access that Junk folder? Do I have to go in through the webmail?

    I can’t seem to find the junk folder or figure out how to access it. Do I need to set it up in the webmail?

    Thanks.

    Reply

Trackbacks/Pingbacks

  1. Tips For Social Media Marketing - Wittmann Web Weaving - July 30, 2007

    [...] Sometimes a quick email or note in private telling them you appreciate them can go a long way. Some folks have done that to me, and I do that as well. Only do if sincere. Perhaps this is not truly SMM, but it will help to keep [...]

  2. Racker Hacker » Mail, Plesk » Sort e-mail in Plesk with procmail - November 27, 2007

    [...] Credit for this trick goes to Russ Wittmann. [...]

  3. Sanity check: Media Temple, SpamAssasin, IMAP, & the iPhone - ZachBrowne.com - November 16, 2008

    [...] shares the two step process for automatically filtering spam tagged by SpamAssassin [server side] with procmail. Shared here for my own personal archiving. You’ll need access to [...]

  4. ramblings of the village idiot » Blog Archive » using procmail with plesk - January 6, 2009

    [...] http://www.russwittmann.com/2007/07/14/server-side-mail-filtering-using-qmailprocmail-under-plesk/ http://rackerhacker.com/2007/11/27/sort-e-mail-in-plesk-with-procmail/ [...]

  5. NetWebLogic » Blog Archive » Filter spam to a folder with procmail and qmail - February 25, 2009

    [...] post is inspired by Russ Whittmann and Europheus’ solutions , with these goals/improvements in [...]

  6. Sanity check: Media Temple, SpamAssasin, IMAP, & the iPhone - Derek Punsalan - 5ThirtyOne - March 10, 2009

    [...] shares the two step process for automatically filtering spam tagged by SpamAssassin [server side] with procmail. Shared here for my own personal archiving. You’ll need access to [...]

  7. place spam in a junk folder in plesk « Life Notes - March 21, 2009

    [...] Credit for this trick goes to Russ Wittmann. [...]

  8. Tips For Social Media Marketing | Wittmann Web Weaving - July 10, 2011

    [...] Sometimes a quick email or note in private telling them you appreciate them can go a long way. Some folks have done that to me, and I do that as well. Only do if sincere. Perhaps this is not truly SMM, but it will help to keep [...]

Leave a Reply