Programming Perl 101

But what about the confirmation email we promised the user in the above print statement? Have no fear, here's the code:

$from = "your@email.com";
open (MAIL, "|/usr/sbin/sendmail -t") || &ErrorMessage;
print MAIL "To: $email nFrom: $fromn";
print MAIL "Subject: Confirmation Emailn";
print MAIL "Thank you for signing up. Your email address ($email) has been ";
print MAIL "successfully stored. Have a nice day.n";
close (MAIL);
sub ErrorMessage {
print "The server has a problem. Aborting script. n";
exit;
}

The first line gives the $from variable a value - change the "you@email.com" text to reflect your address, but make sure to keep the backslash in front of the "@" symbol. This is necessary to avoid a parse error.

The second line specifies the path to "sendmail" on your server, and if it is incorrect (or not found) for some reason, it displays the error message found under the heading "sub ErrorMessage."

Lines 3, 4, and 5 print text into the email…it prints a little header showing who the email was sent TO ($email, the email the user entered into the form originally), and the address of the person sending the email ($from). Line 6 closes the email, and the rest is the aforementioned error message.

Here is the entire email.cgi script:

#!/usr/local/bin/perl
require "subparseform.lib";
&Parse_Form;
$email = $formdata{'email'};
open (EMAILS, ">>emails.txt");
flock(EMAILS, 2);
print EMAILS "$emailn";
flock(EMAILS, 8);
close (EMAILS);
print "Content-type: text/html

";
print "Your email address ($email) has been ";
print "successfully entered. ";
print "Thank you. You will receive a confirmation ";
print "email shortly.";
$from = "your@email.com";
open (MAIL, "|/usr/sbin/sendmail -t") || &ErrorMessage;
print MAIL "To: $email nFrom: $fromn";
print MAIL "Confirmation Emailnn";
print MAIL "Thank you for signing up. Your email address ($email) has been ";
print MAIL "successfully stored. Have a nice day.n";
close (MAIL);
sub ErrorMessage {
print "The server has a problem. Aborting script. n";
exit;
}

4 - Wrap-Up
Congratulations - assuming I didn't lose you somewhere back in Part one, you now know Perl's basic syntax, how to print text, handle user-submitted information via an HTML form, send email with Perl, display different messages depending on the information submitted, and store user variables in a text file for your viewing convenience.

Hopefully you will now look at Perl and other programming languages with confidence rather than fear. Feel free to use and modify the code provided here almost as if it were your own.

Good luck, and happy coding!