Send HTML in an email message.

Sending email is quite common on the Internet. Adding some jazz to your email messages can be quite a challenge. Plain ASCII text may not adequately convey the message you're trying to send. Using MIME-enabled Email will allow you to send more than just plain ASCII in email. This article will discuss how to retrieve and then send an HTML document to an email address. To get started, you'll need access to sendmail, a common Unix command for sending and receiving electronic mail across a network. (In case you're wondering, Microsoft's Windows 95/98/NT do not include this powerful program. Sorry.)

First, you can retrieve this week's script and follow along with the rest of the article. After installing the script in your CGI-BIN directory and doing chmod a+x sendurl.pl. Next, you'll need to edit the script for your site. Basically, look at the following section of the code:

# path and switches for the sendmail program on your system...
$SENDMAIL="/usr/sbin/sendmail -i -t -U -O ErrorMode=q";

# url to be displayed after sending in the form
$successUrl="http://localhost/";

# default URL to send if HTTP_REFERER isn't defined
$defaultUrl="http://www.abiglime.com/webmaster";

For the $SENDMAIL variable, you'll need to change /usr/sbin/ to the actual directory where sendmail is stored on your system. You can find out by typing where sendmail or locate sendmail or if that doesn't work, try find / -name 'sendmail' -print at the shell prompt. Also, look at man sendmail to find out what parameters your sendmail program will accept. The parameters instruct sendmail to read the headers and message from standard input (-i -t -U) and stop on any errors (-O ErrorMode=q).

After that, change the $successUrl to point to a URL that should be displayed after the email message has been sent. It should probably contain a "Thank You" message and then link the user back into the web site. Also, you'll need to change the $defaultUrl. It will be sent if a URL isn't given to the sendurl.pl script.

The script can be invoked with the following HTML code:

<a href="/cgi-bin/sendurl.pl">Send this page to someone</a>
By clicking on the link, the user will be taken to a form that will allow entering an email address and short message to be sent along with the web page. Alternatively, the script can be invoked with the following optional parameter:
<a href="/cgi-bin/sendurl.pl?sendurl=http://someplace.net/somefile.html">
Send somefile.html to a friend</a>
This way, the web page designer can have another html document, rather than the current one, sent via email. The form that is displayed to users can be customized by editing the HTML in the perl script. Search for sub displayForm in the source file.

The CGI script takes the following approach to collect data, retrieve the web page, and send the result. Very little error checking is performed.

  1. Collect any variables submitted via a POST or GET method. Convert all %xx url escape sequences.
  2. Look for a URL to retrieve, if none is given, use HTTP_REFERER or, if that's blank, use $defaultUrl.
  3. Check the email addresses and message. If email addresses are blank, display the form asking for them to be entered.
  4. Open a connection to the remote URL and download the first 100k bytes of the document stored there. If this fails, we'll end up sending a blank HTML document or possibly one containing an error message from the remote server.
  5. Generate the multipart MIME email message. It will contain headers, the plain text message and the HTML document. For more information on MIME email messages, check this out.
  6. Use sendmail to send the email message.
  7. Redirect the user to the $successUrl

Feel free to expand and extend this script. There are countless features that can be added. Using MIME, an entire web page, including all the images, could be packaged and sent via a single email message. The approach used in the script requires that the receiver of the message is connected to the Internet to download the images and other content that the HTML document points at. Some email readers may not correctly interpret the Content-Base: header and therefore all the links on a web page may appear broken. An alternative approach would require reforming all of the URLs contained in the HTML document to include the server name. Instead of using <img src="flower.gif">, the full URL is needed: <img src="http://servername.com/imgs/flower.gif"> .

Author: Doug Steinwand
Date: [01/13/97]
More articles about CGI
More articles by Doug Steinwand
Author Biography