Filtering Recipients with a Shell Script
The -n
command-line
switch can also be used to suppress aliasing when
delivering to a list of recipients that has already
been aliased. For example, consider the following
script, which attempts to restrict delivery to users
who have mail delivered locally and to skip users
who have mail forwarded offsite:
#!/bin/sh EX_OK=0 # From <sysexits.h> EX_NOUSER=67 # From <sysexits.h> EX_SOFTWARE=70 # From <sysexits.h> if [ ${#} -ne 2 ]; then echo Usage: $0 list-name exit $EX_USAGE fi trap "exit 70" 1 2 13 15 LIST= "`/usr/sbin/sendmail -bv $1 \ | grep "mailer local" 2>&1'" \ | sed 's/\.\.\..*$//' if [ -z "$LIST" ] echo "$1 expanded to an empty list" exit $EX_NOUSER fi if /usr/sbin/sendmail -n $LIST >/dev/null 2>&1 then exit $EX_OK fi exit $EX_SOFTWARE
The sendmail program is called
twice inside this script. First, it is given the
-bv
switch,
which causes it to expand the list of recipients in
$1
. That
expansion includes aliasing (and
~/.forward aliasing) for each
name in the list. The output produced looks like
this:
user1... deliverable: mailer local, user user1 user2@otherhost... deliverable: mailer smtp, host otherhost, user user2@otherhost
The grep(1) program selects only
those lines that contain the expression "mailer local"
, thus
indicating a local user. The
sed(1) program then discards
from the ... to the end of each selected line. The
result, a list of local recipients only, is saved in
the shell variable LIST
.
The sendmail program is called with ...
Get sendmail, 4th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.