There are a number of methods you can use to send an email from within a Perl script. The easy way (and nicest way too J ), is to use the UNIX mail command since we are working on a UNIX system. How does one do that? Well, let's analyze the following example.
Here is the form we are going to use:
<html>
<head><title>Anonymous Mail CGI</title></head>
<body bgcolor=#ffffff>
<h1>Anonymous Mail CGI</h1>
<form method=post
action=http://mainline.brynmawr.edu/cgi-bin/bbutoi/cs246/email.perl>
Please enter the e-mail address: <input name="username" cols=50><br>
Please enter the Subject of your email: <input name="subject"><br>
Please type the email in the space below:<br>
<textarea name="comments" wrap="physical" rows=18
cols=75 ></textarea><P>
Please enter your name: <input name="realname"><br>
<input type="reset" value=" Reset ">
<input type="submit" value=" Send email ">
</form>
</body>
</html>
And now, let's take a look at the Perl program that will do the emailing.
#!/usr/local/bin/perl
require "/export/home/http/cgi-bin/cgi-lib.pl";
&ReadParse;
if ($in{realname} eq "") {
$name = "anonymous";
} else {
$name = $in{realname};
}
if ($in{username} ne "") {
open (MAIL, "|mail $in{username}");
print MAIL "--------------------------------------------\n";
print MAIL "This message was send by Anonymous Email CGI\n";
$date = `date`;
print MAIL "Date: $date \n";
print MAIL "From: $name \n";
print MAIL "Subject: $in{subject} \n";
print MAIL "--------------------------------------------\n";
print MAIL "\n\n";
print MAIL "Comments: \n $in{comments}";
print MAIL "\n";
close (MAIL);
}
print <<"EOT1";
<html>
<head><title>Anonymous Mail CGI</title></head>
<body bgcolor=#ffffff>
<h1> The following message was sent to $in{username}:</h1>
EOT1
print "--------------------------------------------<br>\n";
print "This message was send by Anonymous Email CGI<br>\n";
$date = `date`;
print "Date: $date <br>";
print "From: $name <br>\n";
print "Subject: $in{subject} <br>\n";
print "--------------------------------------------<br>\n";
print "\n\n";
print "Comments: <br>\n $comm <br>";
print "<br>\n";
print <<"EOT2";
<hr>
Thanks for using our <a href="/Courses/cs246/spring99/week6/email.html">
<b>Anonymous Mail CGI</b></a>.
</body>
</html>
EOT2
Let's see how it works. We will use this form to send an anonymous email. The email will appear to come from http@mainline.brynmawr.edu, which is the owner of the HTTP daemon. No other information will be sent (unless you want to include some in your Perl script, such as IP number). When you are writing such a program, you have to make sure that no one will use it to harm somebody's privacy. Usually, this kind of tool is used to send information to the administrator of the CGI program. For example, you can set the program to email you every time somebody posts something. But you should not allow users to send anonymous email since you can get in problems. Spamers may use it to distribute unsolicited email. This is the reason why this program will be unavailable for people outside BMC, by the end of the semester.
This example will show you how to keep a Comments Book for your web site. This is a very basic version of the posting server I rewrote for Serendip couple of years ago. It uses two Perl scripts (even if you can use only one and use some hidden variables). The first script (comments-post.perl) will post a comment filled in a form (comments-form.html). The second script (comments-read.perl) will read and display previous postings. This posting server uses one file to keep all the comments. These comments are already formatted with HTML tags, so all we have to do is to display them. However, to be able to append more comments at the end of this file, we can not have the closing tags </body></html> at the end, thus our read script has to provide all the interface: start the HTML tags, print the content of the comments file and close the HTML tags. The posting script will retrieve the data from the form, insert the necessary HTML tags and the print it to the comments file. Here is the HTML for the comments-form.html file:
<html>
<head><title>Comments Posting CGI</title></head>
<body bgcolor=#ffffff>
<h1>Comments Posting CGI</h1>
<form method=post
action=http://mainline.brynmawr.edu/cgi-bin/bbutoi/cs246/comments-post.perl>
Please enter the Subject for your comments: <input name="subject"><br>
Please type the comments in the space below:<br>
<textarea name="comments" wrap="physical" rows=18
cols=75 ></textarea><P>
Please enter your name: <input name="realname"><br>
Please enter your e-mail address: <input name="username" cols=50><br>
<input type="reset" value=" Reset ">
<input type="submit" value=" Post Comments ">
</form>
</body>
</html>
Let's look now at the comments-post.perl script:
#!/usr/local/bin/perl
require "/export/home/http/cgi-bin/cgi-lib.pl";
&ReadParse;
if ($in{realname} eq "") {
$name = "anonymous";
} else {
$name = $in{realname};
}
if(!open (POSTING_FILE, ">>comments-file.html")) {
print <<"EOT1";
<html>
<head><title>Posting Server CGI</title></head>
<body bgcolor=#ffffff>
<h1>ERROR: Can not open posting file.</h1>
Please contact the System Administrator about this problem at
<a href="mailto:bbutoi\@brynmawr.edu">bbutoi\@brynmawr.edu</a>.<p>
<a href="/Courses/cs246/spring99/week6/comments.html">
Back to the Posting Server main page.</a>
</body>
</html>
EOT1
exit;
}
print POSTING_FILE "<hr>\n";
$date = `date`;
print POSTING_FILE "<b>Date</b>: $date <br>";
print POSTING_FILE "<b>From:</b> $name <br>\n";
print POSTING_FILE "<b>Email address:</b>
<a
href=\"mailto:$in{username}\">$in{username}</a><br>\n";
print POSTING_FILE "<b>Subject</b>: $in{subject} <br>\n";
print POSTING_FILE "-------------------------<br>\n";
print POSTING_FILE "<b>Comments:</b><br>\n $in{comments}\n";
print POSTING_FILE "<br>\n";
close (POSTING_FILE);
print <<"EOT2";
<html>
<head><title>Posting Server CGI</title></head>
<body bgcolor=#ffffff>
<h1> The following message was posted:</h1>
EOT2
print "<hr>\n";
$date = `date`;
print "<b>Date</b>: $date <br>";
print "<b>From:</b> $name <br>\n";
print "<b>Email address:</b> <a href=\"mailto:$in{username}\">
$in{username}</a><br>\n";
print "<b>Subject</b>: $in{subject} <br>\n";
print "-------------------------<br>\n";
print "<b>Comments:</b><br>\n $in{comments}\n";
print "<br>\n";
print <<"EOT3";
<hr>
Thanks for using our <a href="/Courses/cs246/spring99/week6/comments.html">
<b>Posting Server CGI</b></a>.
</body>
</html>
EOT3
Let's look now at the comments-read.perl script:
#!/usr/local/bin/perl
if(!open (POSTING_FILE, "comments-file.html")) {
print <<"EOT1";
<html>
<head><title>Posting Server CGI</title></head>
<body bgcolor=#ffffff>
<h1>ERROR: Can not open posting file.</h1>
Please contact the System Administrator about this problem at
<a href="mailto:bbutoi\@brynmawr.edu">bbutoi\@brynmawr.edu</a>.<p>
<a href="/Courses/cs246/spring99/week6/comments.html">
Back to the Posting Server main page.</a>
</body>
</html>
EOT1
exit;
}
print <<"EOT2";
<html>
<head><title>Posting Server CGI</title></head>
<body bgcolor=#ffffff>
<h1>Posting Server CGI</h1>
The following are the comments posted on this server:<p>
EOT2
while(<POSTING_FILE>) {
print $_;
}
close(POSTING_FILE);
print <<"EOT3";
<hr><p>
<a href="/Courses/cs246/spring99/week6/comments.html">
Back to the Posting Server main page.</a>
</body>
</html>
EOT3
Let's take a look at the program on the web. The main page, comments.html, shown below, offers 2 options: posting a new comment or reading the existing ones. When the user choses posting, he will be redirected to comments-form.html, our posting form that will call comments-post.perl. If he choses reading, he will go directely to comments-read.perl, which will format the output using the file comments-file.html. The extension for the comments file does not have to be .html. It was chosen like that just to remember as that the text is stored there in HTML format.
<html>
<head><title>Posting Server CGI</title></head>
<body bgcolor=#ffffff>
<h1>Posting Server CGI</h1>
Choose one of the options below:<p>
<a href="comments-form.html">Post a comment</a> or
<a href="http://mainline.brynmawr.edu/cgi-bin/bbutoi/cs246/comments-read.perl">
Read other comments</a>
<p>
</body>
</html>
Always test your CGI program from the command line
before making it public for the web
This will :
* ease the debugging (since you see
the errors from the Perl interpreter)
* prevent any server crashes due to
error in your program
* prevent any unwanted data to be displayed to
the user coming from the internet
Start building your program slowly. Test all the new functions.
If you type the whole program at once and the you try to test it, it
will be harder to debug it then when you test each function you have implemented.
Think before choosing the name for a variable.
Choosing the right name for your variable, (something that will help
you remember what that variable does) will help you during the development
process as well as later, when you will try to add new features to your
program.
Try to write reusable code.
If you have to write a function that you think you may need later,
try to do it in such a way that will allow you to reuse the same function
with minimal changes. For example, you can write a set of functions for
things that you use in almost any program: file opening error, generate
page header, generate page footer, etc. You can then store this functions
in a separate file and load it as a library, using the require statement.
Always write enough instructions and directions.
If your program does great things but no one knows how to use it, it
is useless!!! Try to write enough informations on you forms and
web pages so that you make your program fool-proof. This is like
advertising: is not only about the quality of the product, it's also about
the way you present the product!