How To Send Email With Perl, Part II
Part I contained an introduction and a working Perl script for sending email. Part II assumes you understand the basic concepts presented in Part I.
The script accompanying this part of the tutorial can send text or HTML email, form user's choice or your choice. It can be used as an auto-responder and can send personalized emails. (Something to consider before sending HTML email is that some email programs can't display HTML formatted pages.)
The script is too large to be embedded within this article. I'll be referring to it throughout. You can download it by clicking this link .
Please download the script before continuing. Without the script to refer to, the rest of this article can be confusing.
Unlike the basic script of Part I, this script uses Perl module "strict" to enforce certain variable naming conventions, which is good programming practice, and to be usable on servers that run mod_perl. (mod_perl is an Apache server module that speeds up Perl programs by a considerable amount. Apache software is used by virtually all Unix/Linux servers.)
Overview
First, the script checks whether or not an email address was specified as part of the script's URL, in the manner presented in Part I. If yes, the script stores the email address where it can be referred to and used a bit later.
Then, in this order, the program:
-
Checks for and stores the values of any form fields that might have been submitted to it.
-
Checks if any required form fields have been left blank and whether or not the submitted address is a valid email address, and displays error messages as appropriate.
-
Sends the email, either plain text or HTML.
-
Displays the thankyou page in the user's browser.
Now, some details about each of the above four steps.
1. Form Field Values
The subroutine ParseValues is where the program checks for and stores the values of form fields. It stores the values in the %In hash variable, the key being the form field name and the value being the form field value.
If a form is used to submit information to the program, the method can be either POST or GET.
A form is not required for this program. An email address can be submitted in the manner of the script in Part I.
2. Checking For Required Fields
If a form is used to submit information to the script, it can have a hidden field name="required" with a value containing the field name(s) that are required to have information before the form can successfully be submitted. If more than one form field name is required, separate them with a comma. Example:
<input type="hidden" name="required" value="firstname, email">
The subroutine CheckRequired ensures that all required form fields contain information. The subroutine also uses another subroutine, ValidEmail, to verify that the email address stored in $In{email} is a validly formatted address. If appropriate, subroutine CheckRequired presents an error message to the user.
Subroutine WebPageErrorMessage is where error message web pages are sent to the user's browser. You'll notice that the last line of the subroutine is
goto BOTTOM;
That lines tells the script to jump to the label named "BOTTOM" located near the bottom of the script. (The label could be named anything you please. By convention, it is all capital letters. I chose "BOTTOM" simply because it is descriptive of where the label is located.)
The goto label method is used instead of the exit() function so the script can run within mod_perl.
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.
4. The ThankYou Page
Once the email is sent, a "thank you" page is presented to the user.
If a form is used to submit information to the script, you can specify the thank you page with a hidden field name="thankyoupage" and the URL of the thank you page as the value. Example:
<input type="hidden" name="thankyoupage" value="http://domain.com/thankyou.html">
If no thank you page URL is specified, a generic thank you page is presented to the user.
Those were the four main subroutines the script uses to process and respond to the information it receives.
An Example Auto-responder's Web Page Form
<form method="POST" action="/cgi-bin/part2.cgi"> <input type="hidden" name="thankyoupage" value="http://domain.com/thankyou.html"> <input type="hidden" name="required" value="firstname, email"> First Name: <input type="text" name="firstname"> <br> Email: <input type="text" name="email"> <br> <input type="radio" name="html" value="yes">HTML <input type="radio" name="html" value="no">plain text <br> <input type="submit"> </form>
Still To Come
Still to come in this tutorial:
Obtaining the email content from a file instead of embedded in the script. (You'll be able to change the emails without modifying the script.)
The ability to send both text and HTML formatting in the same email, so those with HTML email readers see the HTML content and those with plain text readers see the plain text content.
The ability to send email with one or more attachments.
The ability receive and process email sent to an email address like subscribe@domain.com
Watch for additional installments.
Will Bontrager