Article

Programming Perl 101

Page: 1 2 3 4 5 6 7 8 Next

3) Sending email with Perl

Perl scripts, thankfully, have the ability to send email. Not only that, but the text inside the email and the address it is sent to can be specified using variables. You know what that means: users entering their email addresses, and receiving almost instant confirmation emails! Don't worry, doing this is not at all. Create a file called email.html, and use this HTML inside of it:

<html>
<head>
<title>Signup Page</title>
</head>

<body>
<h3>Signup Today!</h3>
<form action="http://www.yourdomain.com/cgi-bin/email.cgi" method="post">
<br>
<b>Email Address:</b> <input type="text" name="email">
<br>
<input type="submit" value="Signup">
</body>
</html>

Once again, don't forget to substitute the form action URL with the path to your email.cgi file. Next, create a new text file, name it "email.cgi", and enter this into it (the first few lines or so should look familiar):

#!/usr/local/bin/perl
require "subparseform.lib";
&Parse_Form;
$email = $formdata{'email'};

Alright, now we move into uncharted territory. Brace yourself:

open (EMAILS, ">>emails.txt");
flock(EMAILS, 2);
print EMAILS "$email\n";
flock(EMAILS, 8);
close (EMAILS);

Let's take this one line at a time. The first line tells the script to open a file - emails.txt. If this file does not exist, Perl will create it on it's own. If you plan to let Perl create the file, make sure your directory has the necessary permissions set. If you don't know what I mean by directory permission, than just upload a blank text file named emails.txt to the same directory as email.html and email.cgi. The first line in the script also assigns the file a name: "EMAILS."

The second like makes use of something called "file locking." This is done to make sure that the file being opened does not change while the script is still processing this code. Imagine what could happen if someone made use of this script by trying to enter their information into emails.txt just as someone else is also doing the same thing? Which would go first? This leaves potential for corruption and a bunch of mixed up text.

File locking is obviously shortened here to "flock." In this case, it is being used to make sure that no other script calling upon the emails.txt file will be allowed to open it for use until this script is doing using it, or until the file-locking command has ended.

Line three is simple: it prints whatever the user has entered in the "email" field into emails.txt, followed by a line break -represented here by a newline character: "\n".

Enter this next:

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

Line four unlocks the emails.txt file for use elsewhere, and line five officially closes it. In case it isn't obvious yet, so far, this script takes the email address entered by the user enters it into the emails.txt file, starts a new line, and does the whole thing over again the next time an email is entered. Because of this, you can allow people to fill out a form, have their email stored in a text file, and come view it whenever you please. The addresses will be broken up so that one address is on each line of the file.

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

Sponsored Links

Follow SitePoint on...