Procmail

From Peyton Hall Documentation
Jump to navigation Jump to search

Procmail is a mail filtering program which is quite powerful for processing emails. While it cannot be used for filtering your incoming mail here (see SmartSieve for that), it can be used in conjunction with Fetchmail, for example, to process messages in other ways.



Examples

my.pmrc

This file can be used in conjunction with Fetchmail to pull your email off the IMAP server and store it in flat files somewhere in your home directory.

MAILDIR=$HOME/mail
DEFAULT=$MAILDIR/INBOX
LOGFILE=$MAILDIR/delivery-log
LOCKFILE=$MAILDIR/.default.lock
:0 fhw
|formail
#
# Any other rules the user wishes to either include with INCLUDERC,
# or hardcode into this file, would go here.
# --------------------------------------------------------------------------
# If we're here, the mail didn't match any other rules, so deliver normally.
:0:
$DEFAULT
# If that fails, report an error and throw the mail away.
EXITCODE=75
:0
/dev/null

These lines mean:

  • Lines 1-4 set some environment variables, making the default destination ~/mail/INBOX, setting a log destination and a lock file
    NOTE:
    If the file exists, and is a mailbox file, all downloaded messages will be appended to it. Also, if you want the logging to be more verbose (and much larger), specify "VERBOSE=ON" in the file.
  • :0 fhw starts a Procmail recipe
  • |formail tells Procmail to pipe every message through 'formail'
    This adds a "From " header to the top of each message, required for some mail programs to determine where one mail stops and the next one starts. This header is different than the standard "From:" header (note the space, and lack of colon). Do not remove this line unless you know what you're doing, or if you don't need the emails in standard Unix mbox format. Since most programs that don't need this line will ignore it if it exists, it's safe to have it added to the messages.
  • A few comments, which serve as placeholders.
    If you want to get fancy and insert normal Procmail recipes here, go right ahead. One example would be if you told Fetchmail to gather messages from every folder on the IMAP server, and want it to filter the messages into multiple files, you would put the proper procmail recipes there. Do note that forwarding messages from Procmail at this point is a bad idea; it would be much better to do this directly on the mail server instead with SmartSieve.
  • :0: sets up another recipe, this time locking the file (the trailing colon).
  • $DEFAULT tells procmail to write the message to the file named in $DEFAULT (which you defined above).
  • Should the write above fail for some reason, the last few lines in the script take care of it:
    • Set the EXITCODE to 75
      When Procmail exits it will be with a non-zero status. Exit code 75 specifically tells the parent process that the message could not be delivered, and that it should try again some other time. This will pass the information on to fetchmail that it should *not* delete the mail from the server (if it's configured to do so), or mark the mail as read.
    • Throw the message away to /dev/null since it couldn't deliver it.