#! /bin/csh
# Shell script, amail, for sending anonymous mail.
# 1. Prepare a message with the desired To and From fields.
#    Using ' at ' instead of '@'.
# 2. Invoke amail with the name of the file containing the message.
#
# Example:
#	cat > tmp
#	From: santa at north.pole (Jolly Saint Nick)
#	To: root at athena
#
#	You've been a naughty girl, so there'll be
#	no presents for you this year!!
#			-Nick
#	^D
#	amail tmp
#

set msg=$1
set pid=$$
set scratchf=/usr/tmp/am.$pid

# The mail is sent via the Simple Mail Transfer Protocol port
# of telnet on some host.  It is best to pick a viahost that uses
# the BSD 4.3 mailer, since it doesn't check the source name against
# the source internet address (4.2 does).
set SMTP=25
set viahost=athena.mit.edu

# Get the From field.  It must be of the form 'username at hostname'.
set fromline=(`grep -i \^From: $msg`)
set fromuser=$fromline[2]
set fromhost=$fromline[4]
echo From: ${fromuser}@${fromhost}

# Get the To field.  It must be of the form 'username at hostname'.
set toline=(`grep -i \^To: $msg`)
set touser=$toline[2]
set tohost=$toline[4]
echo To: ${touser}@${tohost}

# Ask for confirmation.
echo Sending via $viahost
echo -n Is this correct\? \(type y send, or n to abort\)' '
set reply=( $< )
if ( $reply[1] =~ 'n') exit

# Build the SMTP file
onintr cleanup
echo HELO $fromhost 			> $scratchf
echo MAIL FROM: ${fromuser}@${fromhost}	>> $scratchf
echo RCPT TO: ${touser}@${tohost}	>> $scratchf
echo DATA				>> $scratchf
cat $msg 				>> $scratchf
echo \.					>> $scratchf
# Send two dots in case the file doesn't end with a newline.
echo \.					>> $scratchf
echo QUIT				>> $scratchf
# Include lowercase quit in case the connection is refused.
echo quit				>> $scratchf

# Send it.  Let user see error messages if there are any.
telnet $viahost $SMTP < $scratchf

# Cleanup
cleanup:
echo Removing scratch file.
rm $scratchf
