the complete webmaster
tutorials reviews reference
ASP
CGI
FrontPage
HTML
Java
JavaScript

Sponsored by El Scripto

Visit the Mortgage Loan Place for Home Loans and also click here to find VA Loans on our site.

Get thousands of people selling your product online for FREE

home / articles / cgi

How to encapsulate HTML in an email message.

Sending email is quite common on the Internet. On PCs, sending and receiving mail requires a client program like Eudora, Netscape Mail, or Microsoft Outlook. That program must in turn connect to another machine to actually send and receive email. On Unix, the sendmail program handles all the complex tasks of sending and receiving messages. Using this program, discover how to encapsulate an HTML page in an email message that can be read by anyone with a MIME-enabled email client. Most newer email readers available for Windows, Macintosh and Unix have some form of MIME capabilities.

To send a message using sendmail, take a look at its man page. An email message is really just a text file. It can be separated into two separate parts. The headers and the body. Here's an example:

Return-Path: <dzs@iname.com>
Received: from iname.com (localhost [127.0.0.1])
        by localhost.localdomain (8.8.5/8.8.5) with ESMTP id RAA01110
        for <dzs@localhost>; Sat, 3 Jan 1998 17:46:55 -0800
Sender: dzs@localhost.localdomain
Message-ID: <34AEEA0E.71EA3BCC@iname.com>
Date: Sat, 03 Jan 1998 17:46:55 -0800
From: Doug Steinwand <dzs@iname.com>
To: dzs@localhost.localdomain
Subject: Test Message

Here's the body of my test message

 - Doug
In the message above, the lines that begin with Keyword: are the headers. Next, comes a blank line, then the message body. The message above is an example of a message that I have received. As you can see, it has Received:, Sender:, and Message-ID: that were added in transport.

Using sendmail to send a message is quite simple. Basically, it can be invoked with the following command and switches; the switches instruct sendmail to read the headers and message from standard input (-i -t -U) and stop on any errors (-O ErrorMode=q).

[dzs ~]# sendmail -i -t -U -O ErrorMode=q
From: Doug Steinwand <dzs@iname.com>
To: dzs@localhost.localdomain
Subject: Test Message

Here's the body of my test message

 - Doug
Ctrl-D
[dzs ~]#
Using this simple interface, very complex messages can be sent. To do that, Multipurpose Internet Mail Extensions (MIME) have been developed. For a thorough description of all the features and capabilities, see RFC 2045.

MIME version 1.0 uses the Content-Type: header field to specify the type of data that is in the body of the message. This article will discuss using the following header to allow encapsulating both plain text and HTML in a single MIME message:

Content-type: multipart/mixed; boundary="someMagicString"

The general format of a multipart MIME message will be the following:

To: whoever@someplace.com
Subject: MIME test
Content-type: multipart/mixed; boundary="theBoundaryString"

--theBoundaryString

Plain text message goes in this part. Notice that it
has a blank line before it starts, meaning that this
part has no additional headers.

--theBoundaryString
Content-Type: text/html
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
Content-Base: "http://somewebsite.com/"

<body><font size=4>This</font> is a 
<i>test</i>.
--theBoundaryString--
Notice each of the blank lines above. Each one is important because it separates the header from the body in each part of the multipart document. First, the standard email headers are listed. The string theBoundaryString is used to separate each part of the multipart document. For the plain text area, there are no additional headers. Thus, it simply has a blank line before the actual content starts. After that, the HTML file with its headers starts.

The an important header there is Content-Base. It specifies the relative address for links within the HTML document. In other words, the following hyperlink:

 <a href="apage.html">
will be interpreted relative to http://somewebsite.com/. Therefore, its full URL is:
 http://somewebsite.com/apage.html
If you use full URLs in the HTML document, then there will be no need to specify Content-Base. For more information on this, see RFC 2110.

Next week, I'll discuss how to retrieve a URL from a web site and encapsulate it in a MIME message. Until then, you can experiment with Netscape. Go to the File menu and choose Send Page. Send the web page to yourself and then view the results.

Author: Doug Steinwand
Date: [01/06/97]
More articles about CGI
More articles by Doug Steinwand
Author Biography
Promote your website
write for us about us advertise

Copyright 1997, 1998 A Big Lime. All rights reserved.