erikb

Prev Next TECH TECH
Email subject line this [2003-05-11]

We all get a lot of email through a number of aliases. It would be convenient to see what alias the mail was sent to in the list of new mails. This way you don't have to open the mail to find out exactly what alias the mail was sent to.

Many mailing lists do exactly this. They append the name of the list on the subject line, so that it is easy to do filtering or just recognize that some email comes from a certain list.

I want to do this for a number of aliases, such as "webmaster", "abuse" or whatever. These are aliases that are important and I want to highlight those mails. Also, I don't want to mess with per-user .procmailrc files because that is more difficult and might interfere with the delivery of mails. It is also difficult to setup for someone not familiar with procmail. Instead, I want to do this processing in one single central place, for all users.

The image below shows what I want: some mails have a tag first on the subject line. This way I can, for example, see that someone sent a message to the abuse alias.
mail reader

I run sendmail 8.12.X on RedHat, right out of the box. No modification of sendmail at all.
You'll also need procmail and formail. I think they are installed in most UNIX systems.

In my definition of aliases (in /etc/mail/aliases) I wrote:

abuse:   "| /usr/bin/procmail -m /etc/procmailrcs/tag.rc [abuse] abusereal"
abusereal: postmaster, kenny, "| /path/to/archiver abuse"      

Here I define the desired public alias "abuse", and forward the email to procmail to be processed. -m means procmail runs as a mail filter. Then follows the path to the procmail instructions, plus two arguments.

The file /etc/procmailrcs/tag.rc is my way to rewrite the subject line and resend the mail. It needs 2 arguments:

  1. the text to put on the subject line, [abuse] in the above example,
  2. where to resend the mail, abusereal in the example.

Note that I resend to another alias, which is the next line in the aliases file. This way I can define almost all behaviour in this single file (the aliases file) instead of having this information all over the place. It also enables further processing should that be desired.

This resource file for procmail looks like this:

# ----- tag.rc -----
# Rewrite subject line of the mail and resend:
#   arg1 is text to pre-pend to the subject line,
#   arg2 is the address to re-send the mail to
#
# get the text to prepend to the subject line 
TAG=$1 
 
# find out who to forward this email to 
FORWARD=$2 
 
# extract the original subject (this also eliminates leading whitespace) 
:0 
* ^Subject:[    ]*\/[^  ].* 
{ 
         SUBJECT=$MATCH 
} 
 
# create the new subject token 
SUBJECT="$TAG $MATCH" 
 
# insert it into the headers 
:0hf 
| formail -I "Subject: $SUBJECT" 
 
# and forward 
:0 
! $FORWARD 
# ----- END tag.rc ----- 
Prev Next