Article

Programming Perl 101

Page: 1 2 3 4 5 6 7 8

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: $from\n";
print MAIL "Subject: Confirmation Email\n";
print MAIL "Thank you for signing up. Your email address ($email) has been successfully ";
print MAIL "stored. Have a nice day.\n";
close (MAIL);
sub ErrorMessage {
print "<P>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 backslach 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 on 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 "$email\n";
flock(EMAILS, 8);
close (EMAILS);

print "Content-type: text/html\n\n";
print "Your email address ($email) has been successfully entered. ";
print "Thank you. You will receive a confirmation email shortly.";

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

4) Wrap-Up

Congratulations - assuming I didn't lose you back 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!

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links

Rate This Article

  • 1
    Poor
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
    Great

Post A Comment

You need to be a member of the SitePoint Forums to comment on this post. Sign Up

Already a member? Post using your SitePoint Forums account:

Follow SitePoint on...