Someone here recently asked about creating MIME attachments with
procmail. I've included here my "reject" recipe. This is called from
another recipe via INCLUDERC.
'reject' returns the original message to the sender along with an
explanatory note of some sort; both are parts of a MIME formatted
message, modelled after a sendmail error response. The explanatory note
is type 'multipart/report', and the message is returned as
'message/rfc822'.
This relies -- heavily -- on another script of mine, called
'environment'. I've left it out to reduce the size of this message;
among other things, it defines the TO, FROM, SUBJECT, and RECEIVE_DATE
variables that are used in this reject script. Additionally, it defines
REPLYTO to be the mail address I
use for replies (lars(_at_)bu(_dot_)edu). Define this to something else, please
:-).
There are two important parts to this recipe:
(1) The sed call, which does simple variable substitution on
the message you're using as an explanatory note. Occurrences
of "$FROM", "$SUBJECT", "$TO", and "$DATE" in the text are
replaced with the value of the appropriate variables.
(2) The metasend call. metasend is part of the metamail
distribution; it's a shell script (csh) for generating
MIME formatted responses.
In theory, the last recipe -- the call to sendmail -- could be
eliminated, since by default metamail will mail out the response. It's
been quite a while since I last worked on this, and I'm not sure why I
did it this way.
That said:
# reject
# Lars Kellogg-Stedman <lars(_at_)bu(_dot_)edu>
#
# Bounces mail back to sender with explanatory text (using MIME).
# This recipe does not perform any logging.
#
# Requires 'metasend', part of the metamail package.
# Check to make sure the environment script has been run. If not,
# include it.
:0
* $ ${RCSTANDARD:+!}
{
INCLUDERC=$HOME/etc/rc.procmail/environment
}
# ----------------------------------------------------------------------
# Settings
# ----------------------------------------------------------------------
# REJECT_MESSAGE needs to be passed in from the calling rc file.
REJECT_FROM="Mail Daemon <$REPLYTO>"
REJECT_TMPFILE=/tmp/.rj.$$
SENDMAILFLAGS=${SENDMAILFLAGS:--oi}
# ----------------------------------------------------------------------
# Binaries
# ----------------------------------------------------------------------
TEST=${TEST:-/bin/test}
SENDMAIL=${SENDMAIL:-/usr/lib/sendmail}
FORMAIL=${FORMAIL:-/opt/local/bin/formail}
ECHO=${ECHO:-/bin/echo}
RM=${RM:-/bin/rm}
METASEND=${METASEND:-/opt/local/bin/metasend}
SED=${SED:-/bin/sed}
# ----------------------------------------------------------------------
# The Reject Recipe
# ----------------------------------------------------------------------
# We first check to make sure REJECT_MESSAGE is (a) defined, and
# (b) a valid filename.
:0
* $ ! ${REJECT_MESSAGE:+!}
* ? $TEST -f $REJECT_MESSAGE
{
# Save the message in $REJECT_TMPFILE
:0c:
$REJECT_TMPFILE
# Do variable substitution on the response text.
:0Wc
| $SED -e "s/\$DATE/$RECEIVED_DATE/g" \
-e "s/\$TO/$TO/g" \
-e "s/\$FROM/$FROM/g" \
-e "s/\$SUBJECT/$SUBJECT/g" \
< $REJECT_MESSAGE > $REJECT_TMPFILE.response
# Generate the mime-formatted reply.
:0Wc
| $METASEND -b -/ "report; report-type=delivery-status" \
-t "$FROM" -z -s "Re: $SUBJECT [rejected]" -F "$REJECT_FROM" \
-e 7bit -m "text/plain" \
-f "$REJECT_TMPFILE.response" -D "Delivery status report" \
-n \
-e 7bit -m "message/rfc822" \
-f "$REJECT_TMPFILE" -D "$SUBJECT" \
-o $REJECT_TMPFILE.mime
# Send the message and remove temporary files.
:0
| ( $SENDMAIL $SENDMAILFLAGS -t < $REJECT_TMPFILE.mime; \
$RM $REJECT_TMPFILE $REJECT_TMPFILE.mime $REJECT_TMPFILE.response)
}
# The next recipe only executes if the REJECT_MESSAGE had problems.
Maybe
# the user forgot to define it in the calling recipe, or maybe the
# file couldn't be found.
#
# We log an error and sink the message to /dev/null.
:0E
{
LOG="$SYSLOG_DATE $HOST reject: response text undefined or missing.
"
:0
/dev/null
}
---
Lars Kellogg-Stedman * lars(_at_)bu(_dot_)edu * (617)353-8277
Boston University Office of Information Technology