How To Send Email With Perl, Part III
Part I contained an introduction and a working Perl script for sending email. Part II showed how to send either plain text or HTML email. This Part III will demonstrate how to send both plain text and HTML formats into one email.
Sending email with both formats ensures that those with plain text email programs can read your email just as well as those with HTML email programs. It's one way to utilize the advantages of HTML email, yet have your message be readable in plain text email programs.
However, there are a few drawbacks:
-
The email's size is larger than it would be if only one or the other format is used. Some people are sensitive to email size, those who pay for the time they use their Internet connection, for example. And some people automatically block larger email because spam is often delivered in larger emails.
-
A few email programs will display both the text and the HTML portion, the latter with codes and all.
Part III assumes you understand the basic concepts presented in Part I and are familiar with Part II. You can find the previous parts to this series at Library
The script for Part III is similar to the script presented in Part II. The differences are:
-
The subroutine SendEmail has been changed to send an email with both text and HTML formats.
-
A choice between text format and HTML format is no longer available because the outgoing email includes both.
Download the script for this Part III by clicking this link .
Have the subroutine "SendEmail" part of the script within visual reach when you read this article. It will aid comprehension.
Because the scripts have only the above differences, I'll let Part II talk about the script. Thus, Part III can concentrate on how one goes about creating an email with both plain text and HTML content.
You'll be able to use the information to create dual format emails for other scripts, including email templates for Master Form to use.
It all starts in the email header.
The email header is where "To:", "From:", "Subject:" and other routing and identifying information is kept. For our dual format email, the header also contains "Content-Type:" and "MIME-Version:". It might look something like this:
From: someone@domain.com To: name@isp.com MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="_jkkdsffds32432dlkjifewks_" Subject: Widget information
Notice how every line of the header (with one exception) contains a word that identifies the type of information, a colon character, a space, and then the information itself. The exception is when a line begins with a space; in that case, the line is actually a continuation of the information from the line above.
Headers have no blank lines. The first blank line indicates the end of the header and the beginning of the email.
Notice the boundary="______" information that belongs to "Content-Type:". The boundary is a sequence of characters that will mark the beginning and the end of the different sections of the email, the text formatted section and the HTML formatted section.
The boundary marker itself can be different than in the example. It must, however, be a sequence of characters that are unique to the entire email, and all boundary markers in the email must be identical.
Our example boundary marker is: _jkkdsffds32432dlkjifewks_
Here is an example dual format email. Explanations follow.
From: someone@domain.com To: name@isp.com MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="_jkkdsffds32432dlkjifewks_" Subject: Widget information --_jkkdsffds32432dlkjifewks_ Content-Type: text/plain; charset="iso-8859-1" Text email. --_jkkdsffds32432dlkjifewks_ Content-Type: text/html; charset="iso-8859-1" <HTML> <BODY> <p> HTML email. </p> </BODY> </HTML> --_jkkdsffds32432dlkjifewks_--
Below the email header, after at least one blank line, begins the first part of the email message, the plain text part.
The HTML part could come first, but for email programs that understand only plain text it is best to present the plain text part first.
Notice that the plain text part begins with two hyphens and then the boundary characters. Two hyphens precede all boundary markers below the header.
The line immediately below the boundary marker describes the type of content that follows, in this case text/plain. The charset="_____" value could be "us-ascii", but that character set severely limits the use of accent characters.
The boundary marker and the "Content-Type:" line are like a mini-header within the email. There must be at least one blank line above and below each of these "mini-headers".
(Note: "Mini-header" is my descriptive name for these email sections. I've not seen mini-header used as a technical term.)
After the first mini-header comes your plain text email. Type it like you would any email, including paragraphs.
Below the plain text email, put a blank line. That's to provide a blank line above the next mini-header.
This next mini-header begins with the boundary marker, as before. The "Content-Type:" line, in this case, specifies that the following content is text/html, the HTML portion of the email.
After at least one blank line, type your HTML email.
Below the HTML email, put a blank line. The reason for that is to provide a blank line above the last mini-header, the one that tells the email program that all parts of the multipart email are now done.
This last mini-header is only one line, the boundary marker followed by two hyphens. The two hyphens tell the email program that the boundary marker is closing all parts.
And that's all there's to it.
The above can be used in Master Form email templates. It can be used in the script provided for this Part III exercise. And it can be used to modify other email sending scripts.
Will Bontrager