Toivo Talikka

Total Data Pty Ltd

Computer system installation, support and IT management consultancy

Forestville NSW Australia        tel 0410 532 923       email toivo@totaldata.biz

Toivo's TechTips Corner

PROCMAIL SAMPLE RECIPES

I have used Procmail in production for a year. The Procmail recipes described below in detail were inspired by Timo Salmi's Procmail Tips and Recipes.

OVERVIEW

The environment consists of a Fedora server running Sendmail, Dovecot IMAP, Fetchmail and Procmail. The server filters messages received in the 'main' mailbox and transfers relevant messages to the user's 'mobile' mailbox, accessible through the mobile phone. On arrival of a particular message the system sends an alert to a mobile phone, which can then access the 'mobile' mailbox over the internet.

MAIN MAILBOX

The main mailbox is user@totaldata.biz on the mail server running Sendmail. Fetchmail collects email messages from a POP mailbox hosted at an ISP, and also from another POP mailbox at a different ISP. All the messages get forwarded to user@totaldata.biz.

The script is stored in /home/user/.procmailrc. In the following description, or pseudo-code if you like, after the received email message matches the pattern, the action is taken and the processing stops.

Description of Recipe 1

Recipe 1

SHELL=/bin/bash
MAILDIR=/home/user/mail
THIS_MAILBOX=user@totaldata.biz
ROOT_MAIL=root@mail.totaldata.biz
POSTMASTER=postmaster@totaldata.biz
# my POP3 address
MY_POP1=user@zzzzzz.com.au
MY_POP2=user@zzzzzz.net.au

LOGDIR=/var/log
LOGFILE=${LOGDIR}/procmail.log

# troubleshooting
VERBOSE=yes
LOGABSTRACT=all
LOG="---Logging for ${THIS_MAILBOX}, "

# save all received messages into Backup while testing
# :0c:
# Backup

# loop detected, stop processing  -- not tested
:0
* $ ^X-Loop: ${THIS_MAILBOX}
/dev/null

# If cc: to me, store in mailbox
:0:
* $ ^Cc:.*${THIS_MAILBOX}
${DEFAULT}

# If from Zzzzzzz, send to mobuser@totaldata.biz

:0
* ^From.*zzzzzzz
{
 :0c
 ! mobuser@totaldata.biz

 :0
 ${DEFAULT}
}

# If sent to this mailbox directly, store in mailbox
:0:
* $ ^TO_${THIS_MAILBOX}
${DEFAULT}

# If sent to root@mail.totaldata.biz, store in mailbox
:0:
* $ ^TO_${ROOT_MAIL}
${DEFAULT}

# If sent to postmaster@totaldata.biz, store in mailbox
:0:
* $ ^TO_${POSTMASTER}
${DEFAULT}

# If sent to POP address, store in mailbox
:0:
* $ ^TO_${MY_POP1}
${DEFAULT}

# If sent to 2nd POP address, store in mailbox
:0:
* $ ^TO_${MY_POP2}
${DEFAULT}
  
# Otherwise save to Spam folder
:0:
Spam

MOBILE MAILBOX

The 'mobile' mailbox mobuser@totaldata.biz is on the mail server running Sendmail. Fetchmail collects mail from a POP mailbox hosted at the Exchange server of one of our clients, and posts the messages to mobuser@totaldata.biz.

The script is stored in /home/mobuser/.procmailrc. In the following description, or pseudo code, after the received email message matches the pattern, the action is taken and the processing stops.

"An alert is sent to the mobile phone" in the following means that the script creates from the subject of the received message a short email message which Sendmail subsequently sends to an auxiliary mailbox hosted by the mobile service provider. This mailbox is purely a tool to get an alert to the user's mobile phone. The mobile service provider has an email notification service where each received message triggers an SMS text message which is forwarded to the mobile phone. When the user's phone sounds an alert, the user knows that he/she needs to select the Messaging application in the mobile phone (in my case Nokia 6600) and connect to the 'mobile' mailbox residing on the mail server running Sendmail.

The benefit of filtering the messages is that I do not have to react when someone from the client's office has sent a message to the All Recipients distribution list, consisting of all the staff in their company.

Description of Recipe 2

Recipe 2

SHELL=/bin/bash
MAILDIR=$HOME/mail
THIS_MAILBOX=mobuser@totaldata.biz
# my address at the client's postoffice
MY_ADDRESS=t.talikka@myclient.com.au
EVERYONE=AllRecipients@myclient.com.au
HELPDESK=ITHelpDesk@myclient.com.au
SMS_MAILBOX=user@mymobileprovider.net.au

LOGDIR=/var/log
LOGFILE=${LOGDIR}/procmail.log

# troubleshooting
VERBOSE=yes
LOGABSTRACT=all
LOG="---Logging for ${THIS_MAILBOX}, "

# when testing, save all received messages into Inbox first
# the 'c' flag in 0c tells procmail to continue with the recipe
# :0c:
# ${DEFAULT}

:0:
# message sent to everyone
* $ ^TO_${EVERYONE}
Everyone

# loop detected, stop processing  -- not tested
:0
* $ ^X-Loop: ${THIS_MAILBOX}
/dev/null

# If cc: to me, store it and stop processing
:0:
* $ ^Cc:.*${MY_ADDRESS}
${DEFAULT}

:0
{
# Subject: tabs to spaces, remove leading and trailing
 SUBJ_=`formail -c -xSubject: \
       | expand | sed -e 's/^[ ]*//g' -e 's/[ ]*$//g'`

# Body: remove blank lines and newlines, limit to 100 char
# gets 100 char from the start incl headers   -- TEST
# BODY_=`sed -e '1,/^$/ d' | tr '\n' ' ' |  dd bs=1 count=100`
}

# If sent to this mailbox directly, save to mailbox, forward SMS
:0
* $ ^TO_${THIS_MAILBOX}
{
 :0c:
 ${DEFAULT}
 :0 
 # -X extracts the header and discards the body
 # prevent loop, insert subject, remove return receipt, send
  | formail -X "" \
      -A"X-Loop: ${THIS_MAILBOX}" \
      -I"Subject: ${SUBJ_}" \
      -i"Content-Type:" \
      -i"Content-Length:" \
      -I"To: ${SMS_MAILBOX}" \
      -I"Return-Receipt-To:" \
  | ${SENDMAIL} -t
}
  
# If sent to me at client's PO, save intact to this mailbox, forward SMS
:0
* $ ^TO_${MY_ADDRESS}
{
 :0c:
 ${DEFAULT}
 :0 
 # -X extracts the header and discards the body
 # prevent loop, insert subject, remove return receipt 
 | formail -X "" \
      -A"X-Loop: ${THIS_MAILBOX}" \
      -I"Subject: ${SUBJ_}" \
      -i"Content-Type:" \
      -i"Content-Length:" \
      -I"To: ${SMS_MAILBOX}" \
      -I"Return-Receipt-To:" \
  | ${SENDMAIL} -t
}

# If sent to ITHelpDesk, save intact to this mailbox, forward SMS
:0
* $ ^TO_${HELPDESK}
{
 :0c:
 ${DEFAULT}
 :0 
 # -X extracts the header and discards the body
 # prevent loop, insert subject, remove return receipt 
 | formail -X "" \
      -A"X-Loop: ${THIS_MAILBOX}" \
      -I"Subject: ${SUBJ_}" \
      -i"Content-Type:" \
      -i"Content-Length:" \
      -I"To: ${SMS_MAILBOX}" \
      -I"Return-Receipt-To:" \
  | ${SENDMAIL} -t
}

# If sent by Zzzzzzz, save to mailbox, forward SMS
:0
* ^From.*zzzzzzz
{
 :0c:
 ${DEFAULT}
 :0 
 # -X extracts the header and discards the body
 # prevent loop, insert subject, remove return receipt, send
  | formail -X "" \
      -A"X-Loop: ${THIS_MAILBOX}" \
      -I"Subject: ${SUBJ_}" \
      -i"Content-Type:" \
      -i"Content-Length:" \
      -I"To: ${SMS_MAILBOX}" \
      -I"Return-Receipt-To:" \
  | ${SENDMAIL} -t
}

# Otherwise save to Spam folder
:0
Spam

Useful Links


Made With Cascading Style Sheets Valid CSS! Valid XHTML 1.1!