PERL CGI Tutorial
for Writing Interactive Form Scripts

Sendmail - Perl or PHP

Sendmail is a sophisticated email program used by many web hosts. The code below can be used with sendmail and many of its clones. If your web host uses formmail, follow instructions on your control panel for email operations.

Notice that the MAIL function in PERL uses the same content header that is used to return an HTML document. To demonstrate we placed a return message at the bottom of the CGI script. Be sure that you don't place more than one content header on the same page without enclosing one of them in a subroutine. The second will be printed to the browser window.

To use this code you would replace the word path with the path to the sendmail program on your web host's server. Replace the to and from strings with legitimate email addresses. Be sure to use single quotation marks to enclose them.

test_mail.pl

#!/usr/bin/perl
print "Content-type: text/html\n\n";


$to='example@com.com';
$from= 'example@com.com';
$subject='Using Sendmail';

open(MAIL, "|/path/sendmail -t");
print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subject\n\n";
print MAIL "Hello Friend\n";
print MAIL "It has been brought to our attention that you are interested in using the sendmail program.\n";
print MAIL "If you use the template script given here you should be sending email from your website today.\n";
print MAIL "Thank you.\n";
print MAIL "Create a Site\n";
}

close(MAIL);


print <<End_of_Doc;
<html>
<head><title></title></head>
<body>
<p>Thanks for using our stuff.</p>
</body>
</html>
End_of_Doc

To run this script:

  1. Upload to your web space (cgi-bin) and run the script on your web host's server.
  2. URL: yoursite.com/cgi-bin/test_mail.pl
  3. Be sure file permissions are set to 755.

Sendmail PHP Style

We haven't mentioned PHP to this point for CGI operations because for the most part there isn't much difference in the coding. But for sending mail PHP is so much easier than Perl that we thought we'd throw in a special script.

The next script will show you some of the elements and functions of PHP. We'll show you how to loop through an array of email addresses and associated names and send a form letter to each wihout breaking a sweat. The script is very simple.

NOTE: PHP scipts are never placed in the CGI-BIN

<html>
<head>
<title>PHP Mail Array Loop</title>
</head>
<body>
<?php
$users= array (
"1rstaddress@wol.com"=>"John",
"2ndaddress@wol.com"=>"Paul",
"3rdaddress@wol.com"=>"George",
"4thaddress@wol.com"=>"Ringo");

foreach ($users as $key=>$val)
{
$subject="PHP Mail Test";
$message="Hello ".$val\n
This is just a simple form letter which can be sent to multiple locations.\n
Best Regards\n
Your Name (Webmaster)";
mail($key , $subject , $message , "From: youremailaddress@wol.com") ;
}
?>
</body>
</html>

The first part of the script defines an associative array where the email address is used as the key and the persons name is the value.

Then we loop through the hash. The message is placed within the loop so that the $val data can be placed in the salutation.

You can run this script as is. Just plug in some valid email addresses, finish the form letter, save it in the root directory on your server with a .php extension and run it like you would any html page.

Simple Auto Response

Perhaps you have a PHP form that takes subscriptions for a newsletter. It parses an email address as $email. Just add this little bit of code to the bottom of the page and use it as a simple autoresponder.
<?php
mail($email, "Subscription Confirmation", "Thanks for signing up. You will begin receiving your newsletter shortly.");
?>

We like PHP because your web host is responsible for all coordination of PHP with the mail server. No Muss No Fuss!