#!/bin/csh -f

onintr intrend				# if interrupted go to intrend

echo 'Type ^C any time to quit.'

touch $HOME/smtp			# create smtp file
echo 'HELO' `hostname` > $HOME/smtp	# send dummy line
echo -n "Fake sender: "			# get fake sender
echo 'MAIL From:' $< >> $HOME/smtp	# dump fake sender (appropriately
					# formatted, of course) into smtp

echo 'Enter real addresses, one at a time.  Return after last recipient.'
echo -n 'Recipient: '			# get first address
set rec = $<
while ($rec == '')			# if first address is null, bitch.
	echo 'You must have at least one recipient.'
	echo -n 'Recipient: '		# try again for first address
	set rec = $<
end

while ($rec != '')				# if last address wasn't null
	echo 'RCPT To:' $rec >> $HOME/smtp	# put (n-1)th address in smtp
	echo -n 'Recipient: '			# get nth address
	set rec = $<
end

echo 'data' >> $HOME/smtp		# formatting smtp again
echo -n 'Recipient as it should appear in the message: '
echo 'To:' $< >> $HOME/smtp		# put dummy From field in smtp
echo -n 'Subject: '			# put subject line in smtp
echo 'Subject:' $< >> $HOME/smtp
echo -n 'Input file (return for standard input): '
set file = $<
if ($file != '') then
	while (! -e $file)
		echo "Can't find file" $file
		echo -n 'Input file (return for standard input): '
		set file = $<
	end
	cat $file >> $HOME/smtp
else
	echo 'Enter message.  End with ^D'
	cat >> $HOME/smtp
endif
echo '.' >> $HOME/smtp
echo 'quit' >> $HOME/smtp
echo -n 'Sending machine (default is athena): '
set machine = $<
if ($machine != '') then
	telnet $machine 25 < $HOME/smtp
else
	telnet athena 25 < $HOME/smtp
endif
\rm -f $HOME/smtp
unset file
unset machine
exit 0

intrend:
	echo 'fmail interrupted...exiting'
	\rm -f $HOME/smtp
	unset file
	unset machine
	exit 1
