Active Web Hosting Logo
FAQ Index Getting Started Your Account Domains Web Site & Hosting E-Mail
FTP CGI PHP Scripting MySQL Database Script Installation Legal Information

How To Use SendMail In Your Web Site Forms

SendMail is an external program which allows you to send email from within a Perl script. This is useful for sending email from a web form, results of surveys and quizzes, and other useful applications. This document will give you an example of how to use SendMail in your perl script. This is not a method for beginners to use. If you are looking for an easy way to send mail from your web site, please see Using Form Mail For Visitor Feedback E-Mail. The code in this document is very basic. It is up to you to add any security features necessary to prevent abuse of your script. Active Web Hosting will shut down any domain that is being used to send spam.

Using SendMail In The Perl Script

First you'll want to create a perl script that your web site forms can use to send the mail. Below is example code you can use to build your script. You will need to alter the script to suit your own needs for it to work. Save your final script in a subdirectory on your CGI server, and set the file permissions to 755. For this example, we'll use sendmailform.cgi for the filename.

#! /usr/bin/perl

use strict;
use CGI;
my $mailer = '/usr/sbin/sendmail -t';

# ---- Read values from the form ----
my $query = new CGI;
my $thanks_url = $query->param('thanks');
my $sendto = $query->param('sendto');
my $name = $query->param('name');
my $email = $query->param('email');
my $subject = $query->param('subject');
my $message = $query->param('message');
my $from = $name." <".$email.">";

# ---- Send Mail ----
open(SENDMAIL, "|$mailer") or die "Cannot open $mailer: $!";
print(SENDMAIL "To: $sendto\n");
print(SENDMAIL "From: $from\n");
print(SENDMAIL "Reply-To: $email\n");
print(SENDMAIL "Subject: $subject\n");
print(SENDMAIL "\n\n");
print SENDMAIL <<EndMail;
$comments
EndMail
close(SENDMAIL);

# ---- Show Confirmation ----
print "Content-type: text/html\n\n";
print "<html><head>";
print "<META HTTP-EQUIV=\"refresh\" CONTENT=\"1; URL=".$thanks_url."\">";
print "</head></html>";

Creating the Web Form

When you create the web form, be sure that the form action points to your script file. You can use the code below as an example to build your web form. Change the highlighted areas to suit your needs.

<html>
<head>
<title>Contact Us</title>
<link rel="stylesheet" href="template.css" type="text/css">
</head>
<body>

<p align="center" class="lghitext">Contact Us</p>

<form action="http://yourdomain.com/your_dir/sendmailform.cgi" method="post">
<input type="hidden" name="sendto" value="[Your_Email_Address]">
<input type="hidden" name="thanks" value="http://yourdomain.com/thanks.html">

<div align="center">
<table cellpadding="3" cellspacing="3" border="0"><tr>

<td align="right">Name:</td>
<td align="left">
<input class="formstyle" type="text" name="name" size="30" maxlength="50">
</td></tr><tr>

<td align="right">Email:</td><td align="left">
<input class="formstyle" type="text" name="email" size="30" maxlength="50">
</td></tr><tr>

<td align="right">Subject:</td><td align="left">
<input class="formstyle" type="text" name="subject" size="30" maxlength="50">
</td></tr><tr>

<td align="right" valign="top">Comments:</td>
<td valign="top" align="left">
<textarea class="formstyle" name="comments" cols="70" rows="10"></textarea>
</td></tr><tr>

<td align="center" colspan="2">
<input class="button" type="submit" name="submit" value="Send">
&nbsp;&nbsp;&nbsp;&nbsp;
<input class="button" type="reset" name="clear" value="Reset">
</td></tr>

</table></div></form>
<p>&nbsp;</p>
</body>
</html>

Finishing The Project

All that is left to do is make a thanks.html file which visitors will be directed to after submitting the email form. Do note that there are "hidden" areas in the form, which are then passed and used in the script. Careful study of the examples above will give you a good idea how SendMail works from within a Perl script.

Can I have my visitors send file attachments?

It is possible for visitors to send you file attachments from your web form. However, we will not cover this process here. Due to the constant threat of spam and viruses, extra care should be taken when allowing visitors to send file attachments. We can not take responsibility for the use of your scripts. It is up to you to use your discression and to create your scripts with the proper security features built-in to eliminate any possible misuses of your email form.

Can I have my visitors send email to others from my site?

It is not a good idea to allow your visitors to email others from your email forms. This opens your account to misuse which you will be held responsible for. Any domain that is found to be generating spam will be taken offline. It is best to hard-code the receiving email addresses into your script so that visitors can not send to just anybody. There are also other ways to ensure script security which are beyond the scope of this document.



Home - Support - Management - About Us
... Active Web Hosting, 213 N Stephanie St G318, Henderson, NV 89074 ...
(702) 449-2337