Week 6 – February 23, 1999

6.1 Sending email from Perl
6.2 Posting and retrieving data in HTML format
6.3 Tips and tricks

6.1 Sending emails from Perl

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.

6.2 Posting and retrieving data in HTML format

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>

6.3 Tips and tricks