Procmail

From DreamHost
Jump to: navigation, search
WARNING

This no longer works due to a system change as of April, 2008.
New and existing email address may no longer be tied to a shell user if they are not already, though email may be forwarded to a shell account.
See Shell-linked E-mail for details.

See also: maildrop, Procmail + SpamAssassin

Procmail is a mail delivery agent (MDA) or mail filter; a program to process incoming emails on a computer, widely used on Unix systems. It is typically invoked from an mail transfer agent like Sendmail; this makes the mail processing event-driven. The companion-tool formail allows procmail to be used in batch-processing on mail that already is in your mailbox.

Common operations carried out with procmail include filtering and sorting of emails into different folders according to keywords in from, to, subject, text of the mail, or sending autoreplies, but more sophisticated operations are also possible.

A common practice is to let procmail call an external spam filter program, such as SpamAssassin. This method can allow for spam to be filtered or even deleted.

Please also keep in mind that for procmail to work, the email address it is being set up on must be tied to a shell user.

Can I use procmail to filter my mail?

Only some customers can do this. This feature is not available to new customers or new shell accounts.

As of shift to Ubuntu (circa October 2014) This feature no longer works at all. You cannot forward mail to shell accounts. If you do, your correspondent will get a bounce message saying "550 5.7.1 Sender domain not allowed. Please read: http://dhurl.org/20b" It's a shame that DreamHost has disabled this very useful feature. Update: On Oct 17, 2014, Anthony S of DH Support re-enabled forwarding to my shell account. After going 3 weeks where it was broken (this time), and having several DH support people saying it couldn't be done, I'm not sure whether to trust it remaining so.

NOTE: Please vote for the suggestion to re-support procmail if you want procmail support and do not have it on your account. Search for 'procmail' at the above link and vote for it.


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. ( Ergo=> Home Directory = the one with the MailDir in it if you are on the new system. )

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" 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:

Quick and Dirty Procmail Set Up on Dreamhost

Things to Know

  • If you only want a few simple rules, use the existing web based interface in the DH panel under Mail > Keyword Filter.
  • If you must have your own custom .procmailrc, you will only be able to set one up for mail accounts that are tied to shell/ftp accounts. You cannot access the .procmailrc file for mail accounts that use the autogenerated mXXXXXXX accounts.
  • If you are using IMAP to access your email, the subfolders under your INBOX are "dot" files under the Maildir in your home folder. So if you have a folder that is INBOX > Foo then there will be folder named .Foo under ~/Maildir.
  • Because DH stores your email in Maildir format, your .procmailrc file rules must tell Procmail to deliver matching messages using the Maildir format by adding a trailing / to name of the folder to deliver to: (e.g. MAILDIR/.Foo/)

Setup

  • Create your .procmailrc file in your home directory. A basic example that works on DH servers looks like this:
# Empty DH compatible .procmailrc file
# Last updated 7/25/2007
PATH=/bin:/usr/bin:/usr/local/bin
MAILDIR=$HOME/Maildir
LOGFILE=/dev/null # specify something else if you want log files generated
SHELL=/bin/sh

# Add your rules here

# Catch-all rule for all unmatched email
:0
$HOME/Maildir/
  • Create a file named .forward.postfix in your home directory containing (the quotes are necessary):
"|/usr/bin/procmail -t"
  • You're good to go. Have fun.

Using Procmail to Forward all Shell Account mail to an External Address

Some Web application software sends email notifications (including error reports) to the local user account. On DreamHost, Web applications run as the shell user set up for that account (user@machine.dreamhost.com). This is not a fully-hosted email account, and you must log into the shell to read the mail (with a command-line email client, such as pine or mutt).

For easier retrieval of these messages, a .procmailrc rule can be used to forward all email sent to the shell account to some other (probably fully hosted) account. First, set up your .procmailrc and .forward.postfix files as described above. Then, add this rule to the .procmailrc file (substituting the email address to which to forward).

# Add your rules here
:0
! youraddress@somedomain.com

NOTE: This technique works regardless of the WARNING above, since this procmail rule applies to the shell account (not a fully-hosted email address).


Using Procmail to Pipe Emails to Ruby on Rails' ActionMailer

Things to Know

  • For applications receiving lots of emails, you may want to consider a different method: Stress-free Incoming E-Mail Processing with Rails -- no guarantee that the method suggested in that article works well on Dreamhost.
  • There are instructions in Agile Web Development with Rails, second edition, page 578. These are worth looking over, but not specific enough to get it working on dreamhost.
  • The .procfilerc file is not shown by default since the file name starts with a period. Use 'ls -a' to see all files.
  • Here's the .procfilerc contents that worked for me:
LOGFILE=MailLog.log
VERBOSE=off

# Catch-all rule for all unmatched email
:0
| /home/username/userapp.com/userapp/script/runner 'Mailman.receive(STDIN.read)'
  • Next, setup the Mailman class as described here: HowToReceiveEmailsWithActionMailer
  • Turn 'on' the verbose tag to help you diagnose problems and see what's going on.
  • The development.log file is also good to watch, as it'll show you whether the emalis are actually being are passed in to your web app.
  • I also created a file named .forward.postfix as suggested above (not sure if this is necessary).
  • Once you have everything setup, you will still probably get this error message:
Cannot find gem for Rails ~>1.2.3.0:
Install the missing gem with 'gem install -v=1.2.3 rails', or
change environment.rb to define RAILS_GEM_VERSION with your desired version. 
  • It took me forever to figure out how to solve the problem. The solution that worked for me is to freeze rails (which will install the rails gem in your /vendor/ directory. Use this command:
rake rails:freeze:gems

Trouble shooting

Is your log writable and clean?

On dreamhost systems if your procmail log file (e.g. ~/.procmail/log) is not properly newlined terminated, is too big, or is not writable, procmail might not run correctly.

See also