Keywords: procmail postfix route subdomain to one email address, change subject and forward, bugs subdomain, trac email2trac, master.cf main.cf

Subdomain to one email address

If you are running virtual domains already then you might be already using a Postfix transport file. Edit /etc/postfix/transport and add your subdomain

contracts.example.com    mycontracts:
bugs.example.com.com    mybugs:

Route emails to procmail - Postfix master.cf

I'm using user list because I already have mailman setup and this user already exists and is setup.

mycontracts unix - n n - - pipe
        flags=DROhu user=list argv=/usr/bin/procmail -t -m USER=${user} EXTENSION=${extension} NEXTHOP=${nexthop} /etc/postfix/procmail.mycontracts
mybugs unix - n n - - pipe
        flags=DROhu user=list argv=/usr/bin/procmail -t -m USER=${user} EXTENSION=${extension} NEXTHOP=${nexthop} /etc/postfix/procmail.mybugs

Procmail change subject and forward email

SHELL="/bin/bash"
LOGFILE="/var/log/procmail.log"
# fallback:
DEFAULT="/var/lib/procmail"
#MAILDIR="$HOME/Maildir/"
#|sed -e 's/^Subject:[ ]*/Subject: [UNQ] /'
#:0:
#Get the recipient's address
##TO_=`formail -xTo: \
##         | expand | sed -e 's/^[ ]*//g' -e 's/[ ]*$//g'`
#Get the recipient's local-part, e.g. fax number
##FAXNO_=`echo "${TO_}" | awk -F@ '{ print $1 }'`
##LOCALPART=${FAXNO_}
SUBJECT=`formail -xSubject:`
TO=`formail -xTo:`
LOCALPART=`echo "${TO}" | awk -F@ '{ print $1 }'`
:0 fh
* ^To:.*contracts.example.com
| /usr/bin/formail -i "Subject: Contract For $LOCALPART $SUBJECT"
:0A
! incoming@example.com

procmail explained

procmail log

LOGFILE="/var/log/procmail.log"

This wirites log info to this file. Since I'm using user lists we need to create it and make that user the owner.

vi /var/log/procmail.log

chown list:list /var/log/procmail.log

procmail lockfile

Add this to procmail script. This will tell procmail where to keep the lock file

DEFAULT="/var/lib/procmail"

mkdir /var/lib/procmail
chown list:list /var/lib/procmail

procmail get subject

SUBJECT=`formail -xSubject:`

This creates a variable that you can call by $SUBJECT

procmail To:

TO=`formail -xTo:`

This will actually get the To portion of the email email address

procmail get local part

LOCALPART=`echo "${TO}" | awk -F@ '{ print $1 }'`

This gets the local part of the email. So if the email in variable $TO was contract432@contracts.example.com the localpart would be set to contract432. or if it was 3421@bugs.example.com the localpart would be 3421

procmail first job

:0 fh

This means a first job is coming and to use fh as a lock file

procmail if statement

* ^To:.*contracts.example.com

This means if To contains *contracts.example.com then do what follows next

procmail pipe through

| /usr/bin/formail -i "Subject: Contract For $LOCALPART $SUBJECT"

If the if statement is true then email will be piped through formail (part of procmail) and subject will be replaced by "Contract for" + variable LOCALPART + original subject If you are using email2trac you can make the subject contain #LOCALPART: so that email is routed to that bug number.

If prior case was successful then

:0A

This means if prior statement was successful then do what is next.

forward email to

:0A
! incoming@example.com

This will forward the email to incoming@example.com

bugs subdomain using email2trac

SHELL="/bin/bash"
LOGFILE="/var/log/procmail.log"
DEFAULT="/var/lib/procmail"
SUBJECT=`formail -xSubject:`
TO=`formail -xTo:`
LOCALPART=`echo "${TO}" | awk -F@ '{ print $1 }'`
:0 fh
* ^To:.*bugs.example.com
| /usr/bin/formail -i "Subject: #$LOCALPART: $SUBJECT"
:0A
! mybugs@example.com

This will add local part of the email aka the ticket# to subject #bugnumber: example #123: which is recognized by email2trac plugin which is setup to receive emails under mybugs@example.com

References

MyWiki: procmail (last edited 2014-10-14 03:13:37 by LukaszSzybalski)