Customizing Emails Your Scripts Send
A common item on wish lists for form handling scripts is the ability to customize the emails the script sends out.
This article will show you how to do that in detail with Master Feedback version 2.3 (other versions have different line numbers) and in general with other form handling scripts. You can obtain the free Master Feedback from /a/14/pl.pl?msmf
You'll find out how to add email header lines, such as Cc: and Bcc:, and how to format the email as you please.
First, I'll show you an easy way to send email. Then I'll address the modification of existing scripts.
(Note: If you want the ability to customize emails but don't want to bother with modifying your current scripts, the $45 Master Form from /a/14/pl.pl?mf has easy custom emails and other features.)
For Unix/Linux servers with sendmail or qmail on board, sending email is relatively easy.
The example below assumes the web page form has fields named "realname", "email", "age", "FavColor", "max", and "message".
Many scripts, when receiving information from a form, put the information into variables of format: $VName{____} (It's called a hash array, but you don't really need to remember that.) You'll use those variables when you specify the information to be put into your email.
The underline between the curly bracket pair is where the form field name goes.
The variable name, $VName, can be something else, but is usually consistent throughout the script. In Master Feedback, it's $In{____}. In some scripts, it may be $FORM{____} or something else the programmer decided was appropriate.
See how it's used in this example:
open MAIL,"|/usr/bin/sendmail -t";
print MAIL <<THISEMAIL;
To: "Will Bontrager" <will\@domain.com>
From: "$In{realname}" <$In{email}>
Subject: This is a custom formatted email
Cc: will2\@domain.com,will3\@domain.com
Bcc: secret\@domain.com
X-Custom: My custom header line
X-Whatever: Another custom header line
X-My_Mailer: Customized mailer script
Someone named $In{realname} used the form. This is the
information that was submitted:
Name: $In{realname}
Email: $In{email}
Age: $In{age}
Favorite Color: $In{FavColor}
Maximum they want to spend: \$ $In{max}
$In{realname}'s message is:
$In{message}
THISEMAIL
close MAIL;
The above example will send the form submission information to will@domain.com, several addresses on the Cc: line, and the address on the Bcc: line. The email will have a From: line with the form user's name, if provided, and the user's email address.
Note that the first blank line is right below the header. The blank line must be there to separate the header from the body content.
It doesn't matter in what order the header lines appear, so long as they are all above the first blank line.
The absolute minimum header such an email may have is the To: line.
You can make your own custom header lines. The rule is that the first space in the header line must be the space following the colon after the header line name. Thus, the header line name must be all one word, which may contain hyphens and underscores. Custom header line names must begin with X-
An explanation about the email:
The above method of quoting the header and content to be emailed is called a "'here' document". The format is two left angle brackets, then any string of characters, and then a semi-colon. In the above examples, we used THISEMAIL, for the string of characters. Everything following the semi-colon to the point where the same string of characters is found *on a line by itself* (THISEMAIL in our example) will be quoted.
Whenever you include the "@" character in the quoted material, you'll need to escape @ with a back slash. Examples are in the email header, above.
The value of a "$" sign or the value of variables names beginning with a "$" sign, will be inserted into the quoted content -- unless you escape $ with a backslash. An example is in the email body content, above.
The "'here' document" method is one of the easiest methods of sending a chunk of data to sendmail. Otherwise, each line would need a separate "print ..." line. The script you're customizing might or might not use that method. If it does, you're in luck, because you simply replace the current email with the one of your liking. If it doesn't, you'll need to modify the script's "print ..." lines until it sends the email you want.
Master Feedback uses the "'here' document" method.
With Master Feedback, you can replace line 104 through line 116:
print MAIL <<TO_END;
...
TO_END
You can replace those lines with the lines
print MAIL <<THISEMAIL;
...
THISEMAIL
from the example above, and have it work the first time. Of course, your form field names and the names between the curly bracket pairs must match. (The names are case sensitive.) You can change your form or change the email or both, have more fields or less fields, and format the email as you wish.
Question:
Did you find this article interesting and understandable? How can it be improved?
Your response is anonymous.
When done typing, click anywhere outside the box. [more info]
Will Bontrager
©2002 Bontrager Connection, LLC
Please note:
Articles on this website are presented "as is". However -
If you have a question about a CGI script, HTML, CSS, PHP, or JavaScript
Ask one of our Experts and you'll have your answer!
Click here for details.