Procmail configuration

From Gentoo Linux Wiki

Jump to: navigation, search
This article is part of the Tips & Tricks series.
Terminals / Shells Network X Window System Portage System Filesystems Kernel Other

Contents

[edit] 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. This is an example configuration with fetchmail:

poll pop.correo.yahoo.es protocol POP3 service 110
       user "username" password "password" fetchall
       mda "/usr/bin/procmail -a YAHOO -d %T"
poll pop.gmail.com protocol POP3 service 995
       user "username" password "password"
       fetchall ssl
       mda "/usr/bin/procmail -a GMAIL -d %T"
poll imap.uab.es protocol IMAP service 143
       user "username" password "password" fetchall
       mda "/usr/bin/procmail -a UAB -d %T

In this example the -a flag passes its value to procmail (variable $1) in order to recognise from which mail server the messages come.

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).

[edit] 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:

  • The name of maildir folders end in "/".
  • You do not need to lock the file when using maildir format (:0 instead of :0:).

For example. In mbox the format is,

:0:   
recipe
directory_name

while in maildir would be:

:0   
recipe
directory_name/

[edit] Default Settings

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

[edit] 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

[edit] Fix Headers

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

[edit] Fix incorrect Subject Lines

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

[edit] Fix sig-dashes

# 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/^--$/-- /'

[edit] 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"
}

[edit] 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;"

[edit] For or From root

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

[edit] 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/

[edit] 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"
    }
}

[edit] The Last one

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

[edit] References

Personal tools