Naskov.com
Home Family Faith Origins Contact

Using sendmail with CGI for HTML server side base attachment enabled email.

So I was trying to figure out how I could implement a feature
for one of my projects. I needed to provide a HTML page that would
give the 'surfer' the capability to send an email with an attachment
without involving the local email program. The recepients where chosen
by the user from a list that they would previously create and at the time
of creating the email would simply check off the ones that the mail
should go to.
Well sendmail came to the rescue. I put otgether a simple CGI script
implemented in bash/ksh that provided all of the values from the e-mail
form as environment variables. I use uncgi for this so the
variables are all prepended with 'WWW_' (so I have WWW_sender, WWW_recepients, WWW_subject etc.).
The form also allows upload of a file to be used as the attachment to
the email. So in order to compose the email I have the following shell
code do the work:

...
#If no attachment has been provided show the upload screen
#	WWW_attach_file is the name of the file that contains
#	the path to the uploaded attachment file
#	If no file was uploaded it's value is "0" (zero)
attach_file_path="" #the path to the attachment file is blank
if test -z "$WWW_attach_file"
then
	#provide the file upload screen
	exit 0
elif test "0" != "$WWW_attach_file"
then
	#this is a preset pattern for the attachment file's
	#	the WWW_attach_file only provides the unique file id
	#
	attach_file_path=/tmp/tmp_attach_$WWW_attach_file

	#Generate a unique boudry separator using the surrent date
	#	represented as seconds since 1970-01-01 00:00:00 appended
	#	with the CGI's process ID
	#
	boundary="------------$(date +%s)"A"$$"
fi
##############


#If the user clicked on the send button format the mail
#	message and use sendmail to send it off...
#	WWW_Send is the name of the SUBMIT button on the mail form
#
if test -n "$WWW_Send"
then
	#remove leading and trailing spaces. uncgi separates lists
	#	of values with '#' so I am replacing it with ',' so
	#	sendmail can treat the emails properly
	#
	recepients=$(echo $WWW_recepients | sed -e 's/^\ *#//g')
	recepients=$(echo $recepients | sed -e 's/\ *#$//g')

	#remove spaces
	recepients=$(echo $recepients | sed -e 's/\ //g')

	#replace #'s with ','s
	recepients=$(echo $recepients | sed -e 's/#/,/g')

	#Compose the message.
	#NOTE: it is very important that the spacing be preserved as
	#	in this example... Sendmail uses spacing to determine special
	#	meaning of fields such as Send, From etc.
	#
	(echo 'From: '$WWW_from'
BCC: '$recepients'
Subject: '$WWW_subject'
MIME-Version: 1.0'

	#Check if the file containing the path to the attachment exists
	#	and that the path it contains is valid
	#
	#If the file is proper then print the multipart message
	#	signature and boundry
	if ((test -f "$attach_file_path") && (test -f "$(cat $attach_file_path 2>/dev/null)"))
	then
		tmpfile=$(cat $attach_file_path)
		echo 'Content-Type: multipart/mixed;
 boundary="'$boundary'"

This is a multi-part message in MIME format.
--'$boundary
	fi
	
	#This is the actual message body composition
	#
	echo 'Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

'

	#print the message body
	echo $WWW_body | sed -e 'a\
'

	#if this was a message with an attachment
	#	print out the attachment part of the message
	#	consisting of the attachment header and the mime encoded
	#	attachment file data. The mime encoding done with 'mimencode'
	#
	if test -n "$tmpfile"
	then
		echo '

--'$boundary'
Content-Type: application/octet-stream;
 name="'$(basename $tmpfile)'"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
 filename="'$(basename $tmpfile)'"

'
		mimencode $tmpfile
		echo "--"$boundary"--"
		rm -f $tmpfile
		rm -f $attach_file_path
	fi
	
	echo '

.') |/usr/sbin/sendmail -t 
	
	if test $? -ne 0
	then
		echo '
		<CENTER>
			<BR>
			<BR>
			<H3>Error while sending message:</H3>
			<BR>'
<BR>'
		
		echo '
		</CENTER>'
	else
		echo '
		<CENTER>
			<BR>
			<BR>
			<H3>Message sent.</H3>
		</CENTER>'
	fi

	exit 0
fi

Page viewed times. Page cmposed in ms...
Site design concept by Barry Hus