Search:  
Gentoo Wiki

Procmail

Contents

Introduction

Procmail can be used to create mail-servers, mailing lists, sort your incoming mail into separate folders/files (real convenient when subscribing to one or more mailing lists or for prioritising your mail), preprocess your mail, start any programs upon mail arrival (e.g. to generate different chimes on your workstation for different types of mail) or selectively forward certain incoming mail automatically to someone.

A program must feed mail to procmail. See fetchmail and getmail.

Install

Install procmail using emerge -av procmail.

Configure

This will use the Spam filter bogofilter and move the incoming mail according to its output:

File: ~/.procmailrc
MAILDIR = $HOME/.maildir/
DEFAULT = $MAILDIR
LOCKFILE = $HOME/.lockmail
VERBOSE = no

# Run bogofilter on each incoming message
:0fw
| bogofilter -u -e -p

# If there is an error, the mail is requeued for later delivery
:0e
{ EXITCODE=75 HOST }

# If bogofilter has marked the message as spam, it will be delivered to the .Spam folder in the mail directory ($MAILDIR)
:0:
* ^X-Bogosity: Spam, tests=bogofilter
$MAILDIR/.Spam/

The configuration file has different recipes to filter the mail messages. It is possible to test the configuration file by saving a single message into a file (the example uses p.mbox) and pass it to procmail using one of the following commands (tip from this page):

cat p.mbox | /usr/sbin/sendmail -oi myuser@localhost 
cat p.mbox | formail -ds procmail 
cat p.mbox | procmail

To understand the recipes it would be convenient to previously read some introductory text to procmail (like this one).

Recipes for procmailrc

Important: before copying the recipes from this page into your procmailrc file, remember to adapt them to your particular maildir/mbox format, taking into consideration that:

For example. In mbox the format is,

:0:   
recipe
directory_name

while in maildir would be:

:0   
recipe
directory_name/

Default Settings

File: Header of the procmailrc
MAILDIR=$HOME/.maildir/
DEFAULT=$MAILDIR
LINEBUF=4096
SHELL=/bin/bash
LOGFILE=$HOME/.getmail/.procmail.log

Beep on email

beep is the advanced PC speaker beeper, so

emerge beep

# Beep on any email arrival
:0 ic
|beep -r 3 -l 25 -D 25 -n -f 1000 -l 25

Fix Headers

#********
# Fix Headers != 0
# http://linux.kiev.ua/materials/Mutt-FAQ.html
:0 Bfh
* H ?? ~^Lines:
* -1^0
*  1^1 ^.*$
| formail -A "Lines: $="

Fix incorrect Subject Lines

# Correct incorrect Subject lines
# change "AW:" ("Antwort") to "Re:" (reply)
:0 fHw
* ^Subject:.*AW:
| sed -e 's/AW:/Re:/g'

Fix sig-dashes

A very common rule is to fix sig-dashes.

Warning: Unfortunately this eventually breaks PDF attachements. Don't use it unless you find out how to apply this rule outside the MIME parts.
# DO NOT USE : THIS BREAKS PDF ATTACHMENTS
# Correct wrong sig-dashes, ie add a space for lines with only "--" in them:
# http://linuxbrit.co.uk/downloads/dot.procmailrc
# from: ^--$
# to:   ^-- $
#:0 fBw
#* ^--$
#| sed -e 's/^--$/-- /'

Ref. http://pbraun.nethence.com/doc/mail/procmail.html

Conversion MIME to 8-bit

# preconverts all plain-text mail arriving in certain encoded
# MIME formats into a more compact 8-bit format which can be
# used and displayed more easily by most programs.
:0
* ^Content-Type: *text/plain
{
        :0 fbw
        * ^Content-Transfer-Encoding: *quoted-printable
        | mimencode -u -q
        :0 Afhw
        | formail -I "Content-Transfer-Encoding: 8bit"
        :0 fbw
        * ^Content-Transfer-Encoding: *base64
        | mimencode -u -b
        :0 Afhw
        | formail -I "Content-Transfer-Encoding: 8bit"
}

PGP messages conversion

# Convert old-style PGP messages to MIME
:0
* !^Content-Type: message/
* !^Content-Type: multipart/
* !^Content-Type: application/pgp
{
       :0 fBw
       * ^-----BEGIN PGP MESSAGE-----
       * ^-----END PGP MESSAGE-----
       | formail \
               -i "Content-Type: application/pgp; format=text; x-action=encrypt"
       :0 fBw
       * ^-----BEGIN PGP SIGNED MESSAGE-----
       * ^-----BEGIN PGP SIGNATURE-----
       * ^-----END PGP SIGNATURE-----
       | formail \
               -i "Content-Type: application/pgp; format=text; x-action=sign"
}
:0 fBw
* ^-----BEGIN PGP PUBLIC KEY BLOCK-----
* ^-----END PGP PUBLIC KEY BLOCK-----
| formail -i "Content-Type: application/pgp-keys; format=text;"

For or From root

#********
# root
:0
* ^(To|From|Subject):.*(root|[Cc]ron)
.2root/

Spam filter

some lines a broken for wiki, so in you ~/.procmailrc make them as one line in places marked as ~~join here~~

#********
# Spam
:0
* ^(To|CC):.*(res05564|[Uu]ndisclosed-[Rr]ecipients|undisclosed-recipients|spambot)\
* ^From:.*(galamail\.com|agentoflove|absoluteagency\.com|delphi\.com|238525)\
* ^Subject:.*(offer|free|pay|[Vv][Ii1][Aa][Gg][Rr][Aa]|wanted|penis|~~join here~~
offer|chip|inch|please|pill|[Hh]ydrocodone|remember|f(w:|wd|orward))\
* ^X-Advertisement:.*\
* ^X-Mailer:.*(Advanced Mass)\
* ^X-Spam-Status: Yes\
* ^X-Spam-Flag: YES\
* ^Message-ID.*<>
.Trash/

:0B
* ([Vv]iagra|prescription|1-800|inch|remove|XXX|sex|adults|business opportunity|unsuscribe|~~join here~~
LANGUAGE CENTER|visit today|penis|longer|size|~~join here~~
GUARANTEE|[Ll] [Aa] [Nn] [Gg] [Uu] [Aa] [Gg] [Ee]|[Ll][Aa][Nn][Gg][Uu][Aa][Gg][Ee])
.Trash/

Auto-Respond

File: autorespond recipe
## auto-vacation recipe by Louis-David Mitterrand <ldm at apartia dot org>
## BEGIN constants, do not touch
XLOOP=$LOGNAME@$HOST
VAC_MESSAGE=$HOME/.vacation.msg
VAC_CACHE=$HOME/.vacation.cache
VAC_LOCK=$HOME/.vacation.lock
FWD_ADDRESS=$HOME/.fwd.email
NL="
"
## END constants

# test if message is to and from the same address and if suject is
# "vacation on"
:0
* ^TO_\/[-\.a-z_]+@
*$ ^From:.*$\MATCH
* ^Subject: *vacation *\/o(ff|n) *$

## make $MATCH lowercase
* MATCH ?? ()\/o(ff|n)
{
    ACTION=$MATCH
    LOG="ACTION: vacation $ACTION, USER: $LOGNAME, HOME: $HOME $NL"

    # whatever the action clear the cache and remove old message
    :0c
    | rm -f $VAC_MESSAGE $VAC_CACHE
    :0
    * ACTION ?? on
    # deliver
    {
        :0 bc
        | cat > $VAC_MESSAGE

        :0 fhw
        * Subject:\/.*
        | formail -i "Subject: [VACATION ON]$MATCH"
    }

    :0 fhw
    * ACTION ?? off
    * Subject:\/.*
    | formail -i "Subject: [VACATION OFF]$MATCH"
}

:0
*$? "test -e $VAC_MESSAGE"
{
    :0 Whc: $VAC_LOCK
    # Perform a quick check to see if the mail was addressed to us
    # *$ ^To:.*\<$\LOGNAME\>
    # *$ ^TO_$\LOGNAME@
    # Don't reply to daemons and mailinglists
    * !^FROM_DAEMON
    * !^From: savemail@diotec\.com
    # Mail loops are evil
    *$ !^X-Loop: $XLOOP
    | formail -rD 8192 $VAC_CACHE

    :0 e # if the name was not in the cache
    {
        :0 hc
        | (formail -rI"Precedence: junk" -A"X-Loop: $XLOOP" ; \
        cat $VAC_MESSAGE ) | $SENDMAIL -oi -t

        :0 fhw
        * Subject:\/.*
        | formail -i "Subject: [AUTOREP]$MATCH"
    }
}

The Last one

#********
# Finally Everything else
:0:
$DEFAULT

See also

Retrieved from "http://www.gentoo-wiki.info/Procmail"

Last modified: Wed, 01 Oct 2008 07:41:00 +0000 Hits: 17,704