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.

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.

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
Personal tools