Perl Mailto Form Script
If your site does have CGI access, a Perl mailto script is a reliable, spam-free way to send your web form results by email. Unlike a JavaScript mailto form, the visitor's browser never needs to launch its own mail program — the server sends the email directly to you.
What the script does
This small CGI script reads the fields submitted by your HTML form, formats them into a readable message, and emails the results to the address you specify. It works with any standard HTML form that posts to the script with the POST method.
The Perl script
Save the following as mailto.pl and upload it to your cgi-bin directory in ASCII (text) mode. Then change the three configuration lines at the top to match your server and email address.
#!/usr/bin/perl -w
# mailto.pl - send web form results by email
# Provided by Web Designs -4- You
use CGI qw(:standard);
use strict;
# --- CONFIGURE THESE THREE LINES ---
my $recipient = 'you@yourdomain.com'; # where the mail is sent
my $subject = 'Web Site Contact Form'; # subject line
my $smtp = 'localhost'; # usually 'localhost'
print header(-type => 'text/html', -charset => 'UTF-8');
my $name = param('name');
my $email = param('email');
my $message = param('message');
my $body = "Name: $name\n"
. "Email: $email\n"
. "Message:\n$message\n";
open(MAIL, "| /usr/sbin/sendmail -t") or die "Cannot open sendmail\n";
print MAIL "To: $recipient\n";
print MAIL "Subject: $subject\n\n";
print MAIL $body;
close(MAIL);
print "<h1>Thank You</h1>";
print "<p>Your message has been sent. We will reply soon.</p>";
Your HTML form
Create a form on any page that points to your Perl script. Replace path/to/mailto.pl with the correct URL to your script.
<form method="post" action="path/to/mailto.pl">
<p><label>Name:<br><input type="text" name="name" size="40"></label></p>
<p><label>Email:<br><input type="text" name="email" size="40"></label></p>
<p><label>Message:<br><textarea name="message" rows="6" cols="40"></textarea></label></p>
<p><input type="submit" value="Send"></p>
</form>
Installation tips
- Upload the script in ASCII (text) mode so line endings are preserved.
- Set the file permission (CHMOD) to 755 so the server can execute it.
- Some hosts use a different mail program. If
sendmailis not available, ask your host for the correct path. - Always change the recipient address — otherwise the mail will go to the sample address in the script.