Helping ordinary people create extraordinary websites!
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Email:

How To Send Email With Perl, Part II

By Will Bontrager
2004-06-09


3. Sending the Email

The email is sent within subroutine SendEmail. It has a number of useful features, each addressed below.

Email can be sent whether or not a form is used to submit information to the script. For reference, a complete example auto-responder's web page form is near the end of this article.

Email Address --

If you use a form to submit information to the script, there must be a form field name="email" that will contain the email address where the email will be sent to. Example:

Email: <input type="text" name="email">

Type of Email --

As previously stated, the script can send plain text or HTML email.

If a form is used to submit information to the script, you can specify plain text or HTML email with a hidden, radio, or checkbox name="html" field. To specify HTML, the value is "yes". Otherwise, "no". Here is an example of each:

Hidden (to specify HTML):

<input type="hidden" name="html" value="yes">

Hidden (to specify plain text):

<input type="hidden" name="html" value="no">

Checkbox:

<input type="checkbox" name="html" value="yes">
Check for HTML email

Radio buttons:

<input type="radio" name="html" value="yes">HTML
<input type="radio" name="html" value="no">plain text

If no name="html" form field is present, plain text email is assumed.


Personalization --

If you use a form to submit information to the script, the information typed into the form fields can be used to personalize the outgoing email.

When you specify the text of the outgoing email (see below), use the form field name enclosed between double square brackets to mark the place where that form field's contents shall be inserted. Here is an example of such a placeholder:

[[firstname]]

The above placeholder will be replaced with the information typed into a form field name="firstname", like:

First Name: <input type="text" name="firstname">

If no information exists to replace a placeholder with, the placeholder will be removed before the email is sent.


Specifying and Formatting the Plain Text Email --

Put the plain text email between the lines containing the characters: THE_PLAIN_EMAIL

You'll notice that the plain text email is formatted almost exactly the same as the email in the script presented in Part I of this tutorial. The differences are that the placeholder [[email]] is used in the "To:" header line and that additional placeholders may be used in the email body content.

A placeholder may be used more than once in the same email.


Specifying and Formatting the HTML Email --

Put the HTML email between the lines containing the characters: THE_HTML_EMAIL

Like the plain text email, the "To:" header line uses the [[email]] placeholder.

You'll notice two additional header lines for HTML email:

Mime-Version: 1.0
Content-type: text/html; charset="iso-8859-1"


The email program that displays the email for the recipient uses those lines to determine the type of email. If the email program is capable of displaying HTML email, it will do so.

The body content of the HTML email begins and ends with HEAD and BODY tags, just like a regular web page. In between, you can format the text as you please. You may use image tags. You can create a form. In fact, you can do anything you would with a regular web page, with two considerations:

~~ All URLs in the body content must be complete
http://... URLs.

~~ Some email programs are limited in the HTML tags
they recognize, which can cause the page to be
displayed differently than you had intended. If this
consideration is important to you, you may wish to
use only basic paragraph, header, and font tags
to format your HTML email.

Like the plain text email, HTML email may have placeholders where specific form field information is to be inserted. A placeholder may be used more than once in the same email.

Tutorial Pages:
» How To Send Email With Perl, Part II
» Overview
» 1. Form Field Values
» 2. Checking For Required Fields
» 3. Sending the Email
» 4. The ThankYou Page
» An Example Auto-responder's Web Page Form
» Still To Come


Copyright 2004 Bontrager Connection, LLC


 | Bookmark
Related Tutorials:
» Random subroutines in Perl
» Log Script Use
» Creating Perl Modules for Web Sites
» Bit Vector, Using Perl Vec
» Build a Perl/CGI Voting System
» Perl Range Operator