SUMMARY : Forwarding selective mail

Pravin Chavan (prchavan@pcsbom.patni.com)
Thu, 23 Oct 1997 17:24:16 +0500

--Boundary_(ID_NtQG2kYMXCzINE2Noo5Jgg)
Content-type: TEXT/PLAIN; CHARSET=US-ASCII; NAME=text
Content-description: text
Content-disposition: ATTACHMENT; FILENAME=text
X-Sun-Charset: us-ascii

Hello all sunners,

Time for the summary after an overwhelming response to my query.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The original question:

> Hi Sun-Managers,
>
> Need help on a simple topic. I am using mail tool V3.5.1. This is installed
> on Ultra1, Ultra2, SS10 and SS20. The Solaris versions are 2.4, 2.5 and
> 2.5.1.
>
> I receive hundreds of mails every day. Some of these have to be sent to
> certain project leaders (the sender's address of all these mails is the same).
>
> I would like to know if there is any way in which these (selected) mails
> can be forwarded to the concerned persons.
>
> Please help. Answers will be summarised to the list.
>
>
> thanks & regards
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Thanks to following who responded -

Stephen Harris <sweh@mpn.com>
Rich Kulawiec <rsk@itw.com>
David Thorburn-Gundlach <dtg@cae091.ed.ray.com>
Christopher L. Barnard cbarnard@tsg.cbot.com
Peter Watkins <peter@jrc.nl>
Matt Reynolds <reynolmd@aston.ac.uk>
Padmanabhan Ramadurai <durai@head-cfa.harvard.edu>
Fabien Reygrobellet <freygro@agora.fdn.fr>
John Bradley <john.bradley@sr5.chinalake.navy.mil>
Claus Assmann <ca@informatik.uni-kiel.de>
Benjamin Cline <benji@hnt.com>
Eugene Kramer <eugene@uniteq.com>
Frank Cusack <frank+sun@fore.com>
Cheryl L. Southard" <cld@astro.caltech.edu>
Michael Hill <Hill.Michael@tci.com>
Nicky Ayoub <nicky@neta.com>

The unanimous option suggested was the usage of procmail. Rich Kulawiec was
kind enough to send me README file of procmail.

Eugene Kramer provided the following script - attached in EKscript.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Cheryl L. Southard provided the following solution -

You can get "elm" mail reading program, then use the "filter" program
that comes with it. With the "filter" program, you can forward, save,
and delete e-mails depending on various aspects of the incoming
mail.

See http://www.math.fu-berlin.de/~guckes/elm/guides/filter for info
on the filter program.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Nicky Ayoub's suggestion

Hello, I used to use a PD tool called mailagent to do this. I am sure you can search for it with archie. I don't have a URL handy for it now. Sorry...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The best, elegant and simple solution came from David Thorburn (Thanks a tonne
David) -

If you just want to forward them on (and not keep a copy yourself), then
you can easily use a filter program (like the one that comes with elm)
to check the sender and forward appropriately. If you want to keep a
copy as well, then you will probably have to split your input and feed
every letter to a script smart enough to decide whether or not to
forward the letter on. If you have lots of these, you might consider
setting up local mail aliases or a mailing list server (majordomo,
listserv[er], etcetc) and having the sender send the mail to this
address instead of to you.

Scenario 1: get the filter program, compile it up, build some filter
rules (probably one simple rule which says "if (from = yourfromperson)
then forward name,name,name..."), and modify your .forward file to send
all mail through the filter. [The default filter rule should be to drop
mail in your mailbox if it doesn't fit a rule, so you should get all
mail not from this person as normal. Be sure to *not* include yourself
in the forwarding list until you've tested it to see from whom the new
letter appears to be sent; if it's still from the original sender,
you'll loop and send a million copies :-]

Scenario 2: write a shell script and modify your .forward file to send
all mail to you *and* to the shell script (see the vacation man page; it
explains such things pretty well); because you will actually be
resending the letter, it will appear to come from you and then shell
script will throw it away when it comes around again.

Scenario 3: create aliases in /etc/mail/aliases (or a YP/NIS+ aliases
map) that redirect to your users and have the sender send the mail
there, or go and get a mailing list program and configure it to catch
these letters and rebroadcast them.

The elm mail system comes with an application named "filter", which
filters your incoming mail based on rules you specify and then performs
certain actions. The filter program itself knows how to <save>,
<forward>, <delete>, and <pipe_to_a_program>, amongst other things...

You can pick up the elm package from
ftp://ftp.myxa.com/pub/elm/ and various other ftp servers; I have
attached the file "elm.ftp" from the distribution to get you started.
Extract it and take a look at filter. You should be able to run
Configure and then make to build the entire package, and then pull out
filter (if that's all you want -- I *much* prefer elm over something
like mailtool :-) and put the executable wherever you can find it.
You'll find the doc for filter in doc/Filter.guid (as nroff source);
it's quite helpful.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Thanks to everyone of you'll for the help.

Pravin
prchavan@pcsbom.patni.com

--Boundary_(ID_NtQG2kYMXCzINE2Noo5Jgg)
Content-type: TEXT/PLAIN; CHARSET=US-ASCII; NAME=EKscript
Content-description: default
Content-disposition: ATTACHMENT; FILENAME=EKscript
X-Sun-Charset: us-ascii
X-Sun-Data-type: default

#!/usr/local/bin/perl

# $Source: /home/swamp/usr/eugene/RCS/perl/mailhandler,v $
# $Revision: 1.1L $

# A perl mail agent. The basic premise is that mail from certain
# people demands attention immediately, so it gets put in the normal
# system mailbox. All other mail is sorted based on who it's from and
# placed in the appropriate files. This is, of course, easily
# customizable.
#
# Use a .forward file like:
# "|/home4/kaul/bin/ppmd /home4/kaul kaul >> /home4/kaul/.maillog 2>&1"
#
# Author: Rich Kaul (kaul@icarus.eng.ohio-state.edu)
# Date: 12/2/90
# The credit where credit is due department: the core of the message
# header parsing is based on Larry Wall's mailagent script.

($HOME, $USER, $EXT) = @ARGV; # Gotten from the .forward line

########################################
# CONFIGURE HERE
# The value of $root is where incoming mail will be spooled.
# The value of $box is where mail from the list of people you want to
# be able to crash into your normal system mailbox and set off all
# your xbiff flags.
# The value of $dest is where the mail will be delivered. It's set
# here to be the default location.
# $GIVE_UP_BOX is where things go when things are going badly. I set
# it back to the mail spool, because when things go bad it's usually
# because my partition has filled up.

$root = "$HOME/MyMail"; # root of personal mail tree.
$box = "/var/mail/$USER"; # normal system mail box.
$GIVE_UP_BOX = "$box"; # emergency dumping box.
$dest = "$box"; # where things go by default
$openmode = '>>'; # append to the box
$date = `date`; chop $date;

# END CONFIGURE
########################################

$LOCK_SH = 1; # Values for flock() calls.
$LOCK_EX = 2;
$LOCK_NB = 4;
$LOCK_UN = 8;

umask(077); # Get a little privacy.

# Now run and get the headers. We have to parse the headers before we
# can figure out delivery.
while (<stdin>) {
# Do this on the From_ line only.
if (1 .. 1) {
# if (/^From\s+(\S+)\s+[^\n]*(\w{3}\s+\d+\s+\d+:\d+)/) { $from = $1; }
if (/^From\s+(\S+)\s+.*\d+$/) { $from = $1; }
$from =~ s/@.*//; # remove trailing @machine
$from =~ s/%.*//; # remove trailing %machine
$from = $1 if $from =~ /.*!([^\n]+)/; # remove leading ! paths
$from =~ s/-//g;
}

# This section operates on the header of the message. We create
# an array of header keys to work on later.
if (1 .. /^\s*$/) {
$Oheader .= $_; # do not mess with the header !
s/^From: ([^<]*)\s+<(.*)>$/From: $2 ($1)/; # rewrite ugly header
$header .= $_;
chop;
if (/^\s*$/) {
foreach $key (keys(header)) {
eval "\$H$key = \$header{'$key'}";
}
}
else {
if (s/^([-\w]+):\s*//) {
($headline = $1) =~ y/A-Z-/a-z_/;
$header{$headline} .= "\n" if $header{$headline} ne '';
}
else {
s/^\s+/ /;
}
$header{$headline} .= $_;
}
}
# And here we make the body of the message.
else {
s/^(From .*)/>$1/;
$body .= $_;
}
}

################################
# CONFIGURE HERE
# This is the section that actually decides where the mail is going to
# be delivered. You can check the $from variable to see the name of
# the person who sent the message, or you can look at any other
# header by prefixing it with $H. For example, to see if something's
# being sent to me, I do $Hto =~ /kaul/. Note that the header line
# title is in lower case. To do case independent matching do /kaul/i.
#
# By changing $dest you can change where the mail will be delivered.
# I keep an incoming mail area called $root, so you'll see lots of
# $root/xxx as I split the incoming mail.
#
# Order is important, since this structure exits at the first match.
#
# First, junk to/from the system
if
($Hfrom =~ /trakker/ && $Hsubject =~ /^Area/ ) {
$dest = "$root/Builds/drnout"; $teemode++;}
elsif
($Hfrom =~ /asd/ && $Hsubject =~ /release coming/) { undef $dest;}
elsif
($Hfrom =~ /qwe/ && $Hsubject =~ /^(.*) Build (.*)/) {
$dest = "$root/Builds/builds_$1"; $filtermode++;
$filter = "/home/swamp/usr/eugene/bin/bugrpt | /bin/mailx -s \"Bug fix $1 $2\" bugreport";
}
elsif
($Hsubject =~ /^Bug fix (\S+).*/) { $dest = "$root/Builds/bugf_$1"; }
elsif
($Hfrom =~ /rty/ && $Hsubject =~ /^(.*) release notes/) { $dest =
"$root/Builds/relno_$1";}
elsif
($Hsubject =~ /^(newfs.*)$/) { $dest = "$root/.Newfs/$1";}
elsif
( $Hsubject =~ /^PureLA: / ) { undef $dest; }
elsif
( $Hsubject =~ /^Delivery Report/ ) { undef $dest; }
elsif
( $Hsubject =~ /^cc:Mail Link to SMTP U/ ) { undef $dest; }
elsif
( $Hfrom =~ /^\d+.aol.com/ ) { undef $dest; }
elsif
( $Hto =~ /krm.*/ || $Hcc =~ /^krm/ || $Hbcc =~ /^krm/) {
$dest = "$root/____________"; }

# END CONFIGURE
##############################################

$| = 1; # do not mess the log file
$afrom = $Hfrom;
if ( $afrom =~ /.*@.*/ ) {
$afrom =~ s/^[^@]+\s+//;
$afrom =~ s/(\S+@\S+).*/$1/;
}

open (LOGGER, ">>$HOME/.mail_log") || warn "cannot open log\n";
do flocker(LOGGER,$LOCK_EX);
if ( ! defined $dest ) {
print LOGGER $date, ' null ', $afrom, ' ', $Hsubject, "\n";
} else {
print LOGGER $date, ' ', $afrom, ' ', $Hsubject, "\n";
}
do flocker(LOGGER,$LOCK_UN);
close LOGGER;

exit 0 if ! defined $dest; # do not try to lock /dev/null

# Now we do the delivery. It's to a *file*.
#$all = "From $from $Hdate\nTo: $Hto\nSubject: $Hsubject\n\n" . $body;
$all = $Oheader . $body;
{
open(BOX, "$openmode$dest") || do gag ("Can't open $dest ($< $>): $!");

do flocker(BOX,$LOCK_EX);
print BOX $all,"\n\n";
do flocker(BOX,$LOCK_UN);
close BOX;

if ( defined $teemode ) {
undef $teemode;
$dest = $box;
redo;
}

if ( defined $filtermode ) {
$openmode = '|';
$dest = $filter;
undef $filtermode;
redo;
}
}

# File locking subroutine.
sub flocker {
local ($file, $mode) = @_;
eval 'flock($file,$mode);';
seek($file, 0, 2); # in case it was appended while we were waiting
}

# For some reason mail couldn't get delivered. Dump the message in the
# emergency mailbox and exit.
sub gag {
open(ERR_BOX, ">>$GIVE_UP_BOX") || die "I'm really hosed. I can't even open the emergency box $GIVE_UP_BOX\n";

print @_;
do flocker(ERR_BOX,$LOCK_EX);
print ERR_BOX $all,"\n\n";
do flocker(ERR_BOX,$LOCK_UN);
close ERR_BOX;
exit 1;
}

--Boundary_(ID_NtQG2kYMXCzINE2Noo5Jgg)
Content-type: TEXT/PLAIN; CHARSET=US-ASCII; NAME=text
Content-disposition: ATTACHMENT; FILENAME=text
X-Sun-Charset: us-ascii

The following sites have agreed to make Elm available via anonymous ftp.

Site Contact
In the US/Canada:
wuarchive.wustl.edu chris@wugate.wustl.edu (Chris Myers)
(128.252.135.4)
/packages/mail/elm

ftp.uu.net
(137.39.1.9, 192.48.96.9)
/networking/mail/elm

In Europe:
ftp.cs.ruu.nl Edwin Kremer, edwin@cs.ruu.nl
(131.211.80.17)
/pub/ELM-2.4

ftp.th-darmstadt.de ftpadmin@ftp.th-darmstadt.de
(130.83.55.75)
/pub/networking/mail/elm

ftp.th-darmstadt.de ftpadmin@ftp.th-darmstadt.de
(130.83.55.75)
pub/networking/mail/elm

In the UK:
ftp.ecs.soton.ac.uk T.Chown@ecs.soton.ac.uk (bitnet)
(152.78.64.201) T.Chown@uk.ac.soton.ecs (JANET)
/pub/elm

ftp.demon.co.uk Cliff Stanford, cliff@demon.co.uk
(158.152.1.65)
/pub/unix/mail/elm

src.doc.ic.ac.uk L.McLoughlin@doc.ic.ac.uk
(146.169.2.10)
computing/mail/elm

In Australia:
ftp.adelaide.edu.au Mark Prior, mrp@itd.adelaide.edu.au
(129.127.40.3)
/pub/mailers

In Taiwan:
NCTUCCCA.edu.tw Huang, Chih-Hsien hch@NCTUCCCA.edu.tw
(140.111.3.21)
/packages/mail/elm

This site (dsinc.myxa.com) also has Elm 2.4 available already patched
to the current revision level. It is pub/elm/elm2.4.tar.Z and
if you retrieve that file, you will not need the patches.

--Boundary_(ID_NtQG2kYMXCzINE2Noo5Jgg)--