Welcome, Guest. [ Log In ]
Question   Can I use procmail to filter my mail?
Search KBase


Related Links:
· Mail :: Filters

Top 5 in this Area:
1. What is the name of my outgoing mail (SMTP) server?
2. Does my ISP block port 25?
3. Setting up Microsoft Outlook Express with DreamHost email
4. How do I access other mail folders on the server besides my inbox?
5. What's the difference between POP3 and IMAP?

 
Can I use procmail to filter my mail?
The short answer is 'yes'.

NOTE: You can not do this with a mailbox user (those m###### accounts) you have to do it with a real user (with shell access).

The new mail system (if you have a directory named 'Maildir' in your home directory, you're on the new mail system) works a bit differently however, and the previous documentation is incorrect.

Procmail isn't used as the LDA anymore, so you will need to pipe your mail through procmail. To do this, create a file (in your home directory) called ".forward.postfix". This file should have the single line (quotes included):
"|/usr/bin/procmail -t"
Be very careful not to use an editor that inserts DOS or Mac carriage returns to edit these files; ideally you should create them on the server using a text editor like Pico, vi, or emacs.

Note that procmail can be very picky about permissions. In particular, don't leave any procmail files or directories set to be group or world writable or procmail won't work properly.

You'll need to create a file named ".procmailrc", also in your home directory.

Here are some common variables that go in this file, and our suggestions on how to set them (username refers to your username):

DEFAULT=$HOME/Maildir/
MAILDIR=$HOME/Maildir
PMDIR=$HOME/.procmail
LOGFILE=$PMDIR/log
SHELL=/bin/sh

You may wish to omit the LOGFILE variable unless you need these logs; they can get quite large if you don't remember to remove them periodically.

You can include rc files with this statement:

INCLUDERC=$PMDIR/filters.rc
where filters.rc is the name of the file with your filters (this can be whatever you want).

Advanced users may wish to omit 'DEFAULT', and instead put the following after *all* other recipes:
:0
$HOME/Maildir/

This will avoid using a lockfile.

The new mail system uses a file format called 'Maildir' to store messages, and the Courier IMAP server. A few notes about adapting procmail recipes for use with this format:

1. Lockfiles are not necessary since Maildir uses an individual file for each message. Thus:
:0:
becomes
:0

2. With courier IMAP, folders are stored inside Maildir and are all subfolders of the INBOX folder. Folders begin with a leading period ("."), so:
mail/foo
becomes:
Maildir/.foo/

Your recipes (assuming you have already defined $MAILDIR in .procmailrc) might look something like this:

# put any mail from newdream or dreamhost in the folder 'junk'
# (we don't recommend that you actually use this one, of course)
:0
* ^From:.*(newdream\.net|dreamhost\.com)
.junk/

# filter mutt-users list
# this will create 'mutt' as a subfolder of 'lists', which, in turn is
# a subfolder of "INBOX"
:0
* ^Return-Path: <mutt-users-owner
.lists.mutt/

# Filter mail to or CC my 'spam@example.com' address to "blah"
:0
* ^TO_spam@example\.com
.blah/

# Send this jerk straight to the bit-bucket
:0
* ^From:.*william@hq\.newdream\.net
/dev/null

Note that a '.' will match any character; if you want to match a literal period, you should escape it with a backslash (as in the examples above).

Please contact support via the Account Control Panel if you have any further questions regarding procmail. Use the links below to learn more about procmail:

http://www.ii.com/internet/robots/procmail/qs/
http://www.procmail.org
http://www.sektorn.mooo.com/era/procmail/
http://www.uwasa.fi/~ts/info/proctips.html

Last updated: Feb 26, 2005.

User Post (2004-08-02 16:45:33 by haxial)
By the way fellow webmasters, a great technique for avoiding SPAM, viruses etc is to remove all email addresses from your website and instead provide a form for people to contact you. You can use formmail for the form. Unfortunately formmail discloses the email address it sends the message to -- you can view it in the HTML where it can be picked up by the 'bots/webcrawlers used by spammers. The solution is procmail! With procmail you can set up a mailbox that accepts mail that was submitted via the form on the website, and deletes all the rest. Here is the .procmailrc I use, which was written with the help of the wonderful people behind dreamhosts tech support!

---------------------
DEFAULT=$HOME/Maildir/
MAILDIR=$HOME/Maildir
PMDIR=$HOME/.procmail

:0
* ^Return-Path: <dhapache@.*dreamhost\.com
$HOME/Maildir/

:0
/dev/null
---------------------

User Post (2004-08-02 16:45:00 by lafromage)
I couldn't get recipes like the ones listed above to work without putting "$MAILDIR/" in front of the folder names. For example:

:0
* ^From:.*(newdream\.net|dreamhost\.com)
$MAILDIR/.junk/
User Post (2005-10-28 19:11:07 by warlord)
how would I go about piping an email to a perl script?
User Post (2005-09-25 19:14:20 by rmgw)
The www.evolt.org article mentioned in a user post by hunsvotti, below, can now be found at the following URL:

http://evolt.org/node/27914

Hope this helps! I spent a while looking for it.
User Post (2005-03-15 23:24:06 by cpeterso)

I wanted to use DreamHost's Junk Filter, but I did not want to give up my handy catch-all email addresses. After some research, I was able to enable DreamHost's pre-installed SpamAssassin without installing any new software.
To enable SpamAssassin, just add the following three lines to your ~/.procmailrc file *BEFORE* the "catch all for inbox" code at the end of the file.


# pipe email through spamassassin
:0fw
| spamassassin -P


You can then edit your SpamAssassin settings (as described at spamassassin.apache.org) in ~/.spamassassin/user_prefs. Your user_prefs file should be auto-generated after SpamAssassin scans its first email, so send yourself a test email to create the file. For example, my user_prefs file includes:

# add [SPAM] to spam's subject line
rewrite_subject 1
subject_tag [SPAM]

# non-english email is always spam (for me)
ok_languages en
ok_locales en

# whitelist_to -6
whitelist_to me@example.com

# whitelist_from -100
whitelist_from friend@example.com


User Post (2005-03-03 13:20:17 by tenadmin)
<bump> that last User Post! I'm thinking I'm going to have to delete the m99* account(s) and recreate the mail address as part of adding a "Full New User"? I hope that works though and I don't get any *&^% messages about "That mail account already exists" due to some lag in DH's systems. Anyways -- just thought I'd share my current thinking/predicament.
User Post (2005-02-25 22:15:34 by dainsorg)
Just a note that might save somebody dumb like me some time. If you want things to go into a folder called spam for example your rule action has to be

.spam/

NOT

.spam

The ending slash tells procmail that its a maildir format directory so it actually puts the mail in the right place (in the "new" subdirectory of .spam). Otherwise it'll put it directly in .spam where courier, and hence your IMAP client, won't see it.
User Post (2004-08-08 06:09:41 by hunsvotti)
Someone in the support forums wanted to know how to use procmail to redirect e-mail to a PHP script, rather than sending it to a mailbox. I wanted to do this myself, and I just succeeded, so here's how to do it. (This will also work with perl or sh or whatever else you want to use, but you don't have to mess around with copying the PHP binary and libs in that case.)

Make a directory, say $HOME/email/php. (An empty directory, you don't want to use a directory you have another PHP binary in or anything wierd like that.) Change to this directory and run the following:
cp /usr/local/bin/php .
for i in `ldd php |awk '{print $3}'`; do cp $i .; done

This will place a copy of the PHP binary in the directory, AND all the libraries that it depends on (which are not the same on the mail machines.)

Next, change to your home directory and make sure you have a file called .forward.postfix which contains this single line, including the quotes:
"|/usr/bin/procmail -t"

Now, still in your home directory, create your .procmailrc file:
----------------------------------------------------------
DEFAULT=$HOME/Maildir/
MAILDIR=$HOME/Maildir
PMDIR=$HOME/.procmail
SHELL=/bin/sh
VERBOSE=yes
LOGFILE=$HOME/logfile
LD_LIBRARY_PATH=$HOME/email/php
PHP=$HOME/email/php/php

:0
* ^TO_youraddress@yoursite\.net
|$PHP $HOME/email/email.php
----------------------------------------------------------

Notice the LD_LIBRARY_PATH environment variable. This will force the PHP binary to search in the specified directory for any dynamic libraries that it needs to load. This is how we get the PHP binary to use the library files it actually works with, rather than trying to use the ones on the mail server (which doesn't have all the libraries it needs.)

Now, in your ~/email/ directory, create a .php file to process incoming email. I found the one at [url]http://www.evolt.org/article/Incoming_Mail_and_PHP/18/27914[/url] works perfectly. All this does is read the incoming mail over stdin and parse it for headers, subject, and body. After that you can do whatever you want with it.

After you get everything up and running, you may want to comment out (put a # in front of) the VERBOSE and LOGFILE variables in .procmailrc.

Good luck!
User Post (2002-10-13 09:26:08 by haxial)
Apparently you cannot use procmail with m999999 accounts. Procmail is only supported for the full user accounts.
User Post (2002-10-05 16:32:50 by ianh)
What if your mail account is one of the m999999 accounts though? How do you add the relevant files?