SENDMAIL=/usr/sbin/sendmail
RM=/bin/rm
FORMAIL=/usr/bin/formail
DQ='"'
MAILHOST='mail.scenespot.org'

# example bounce
:0
* breakthrough
* anti-aging
{ BOUNCETHIS=yes }

# BOUNCE ROUTINE
:0
* BOUNCETHIS ?? yes
{
	:0
	{ BOUNCEPID=`echo $$` }
	
	:0 c:
	bouncetemp.${BOUNCEPID}

	:0
	{ SPAMFROM=`${FORMAIL} -rt -xTo:` }

	:0
	{ SPAMTO=`${FORMAIL} -xTo: | sed -e 's,^ *,,' -e 's, *$,,'` }

	:0
	{ SPAMTOUSER=`${FORMAIL} -xTo: | sed -e 's,^ *,,' -e 's, *$,,' | cut -d'@' -f1` }

	:0
	* ^Errors-to:[ 	]\/.*
	{ SPAMERROR=$MATCH }

	:0
	{ SPAMDATE=`date -R` }

	:0
	{
		# For FreeBSD, maybe Linux too:
		MIMEBREAK="`echo \"$SPAMDATE $$\" | /usr/bin/md5sum | sed -e 's, *$,,'`/${HOST}"
		# For Solaris (could be better):
		#MIMEBREAK="${BOUNCEPID}`date '+%s'"
	}

	:0 c: bouncetemp.${BOUNCEPID}.lock
	| ( ${FORMAIL} -rt \
		-I"From: MAILER-DAEMON@${MAILHOST} (Mail Delivery Subsystem)" \
		-I"Subject: Undelivered Mail Returned to Sender" \
		-I"MIME-Version: 1.0" \
		-I"Content-Type: multipart/report; report-type=delivery-status; boundary=${DQ}${MIMEBREAK}${DQ}" \
		-A"X-Loop: MAILER-DAEMON@"${MAILHOST}";\
		printf "\nThis is a MIME-encapsulated message\n";\
		printf "\n--%s\n" "${MIMEBREAK}";\
		printf "\nContent-Description: Notification";\
		printf "\nContent-Type: text/plain\n";\
		printf "\nThis is the Postfix program at host %s." "${MAILHOST}";\
		printf "\n\nI'm sorry to have to inform you that the message returned";\
		printf "\nbelow could not be delivered to one or more destinations.";\
		printf "\nFor further assistance, please send mail to <postmaster>";\
		printf "\n\nIf you do so, please include this problem report. You can";\
		printf "\ndelete your own text from the message returned below.";\
		printf "\n\nThe Postfix program";\
		printf "\n\n<%s>: unknown user: \"%s\"\n" "${SPAMTO}" "${SPAMTOUSER}";\
		printf "\n--%s\n" "${MIMEBREAK}";\
		printf "\nContent-Description: Delivery error report";\
		printf "\nContent-Type: message/delivery-status";\
		printf "\n\nReporting-MTA: dns; %s" "${MAILHOST}";\
		printf "\nArrival-Date: %s" "${SPAMDATE}";\
		printf "\n\nFinal-Recipient: rfc822; %s" "${SPAMTO}";\
		printf "\nAction: failed";\
		printf "\nStatus: 5.0.0";\
		printf "\nDiagnostic-Code: X-Postfix; unknown user: \"%s\"" "${SPAMTOUSER}";\
		printf "\n\n--%s\n" "${MIMEBREAK}";\
		printf "\nContent-Description: Undelivered Message";\
		printf "\nContent-Type: message/rfc822\n\n";\
		cat "bouncetemp.${BOUNCEPID}";\
		printf "\n\n--%s--\n\n" "${MIMEBREAK}";\
		${RM} -f "bouncetemp.${BOUNCEPID}" \
	) | ${SENDMAIL} -od -t
}
