First time here? First time here? Newsletter Signup Newsletter
Login | Register | 651326 Members and growing!
Search tutorials
Author: Jeffrey F. Hill | Website:

Option 2 - Setting up the Perl Script to send Mail with Perl 

The hardest part of using a cgi script is the setup and getting all your paths correctly.  I'll attempt to explain all this in as much detail as I can.  Along with some common errors that you can make, and how to avoid them.  First you'll have to change a couple things in the actual Perl script. You should only have to change Line 14 and possibly Line 1.  For the purpose of this example,  line numbers were included below.  In the actual script these should not be included.  In my opinion Perl is the hardest to work with out of all three options - but is the most universal in terms of allowed usage on servers.  There are some extra steps that you have to take in order to use a Perl script - If you have the choice I would recommend using either the PHP method or the ASP method.  (Notice the similarities between PHP and Perl - by learning one or the other you'll have a good knowledge of the one you don't know).

Note:  The User Entered variables in this case / example are - FirstName, Email, Company, ToComments, HearAbout - These values have to use the $formdata function from the subparseform.lib.  Don't worry about this just make sure that any data that is entered by the user and posted to the script appears like this for example: $Email = $formdata{'Email'};

Open up the attached Perl Script called "snowMailPerl.cgi" with notepad or any other text editor to follow along and make changes.  

1)  #!/usr/bin/perl

2)  require "subparseform.lib";
3)  &Parse_Form;

4)  $Email = $formdata{'Email'};
5)  $FirstName = $formdata{'FirstName'};
6)  $Company = $formdata{'Company'};
7)  $ToComments = $formdata{'ToComments'};
8) $HearAbout = $formdata{'HearAbout'};

9) $ToEmail = "your\";
10) $ToSubject = "Testing out Tutorial";

11) $EmailBody = "Sent By: $FirstName\nSenders Email: $Email\nSenders Company: $Company\n\n
	 Message Sent:\n$ToComments\n\nSender Heard About Site From: $HearAbout\n";

12)  $EmailFooter="\nThis message was sent by: $FirstName from $REMOTE_ADDR If you feel that you 
	 recieved this e-mail by accident please contact us at";

13) $Message = $EmailBody.$EmailFooter;

14) open (MAIL, "|/usr/sbin/sendmail -t") || &ErrorMessage
15) print MAIL "To: $ToEmail \nFrom: $Email\n";
16) print MAIL "Subject: $ToSubject\n";
17) print MAIL "$Message\n";
18) close (MAIL);

19) print "Content-type: text/html\n\n";
20) Print "_root.Mail.EmailStatus=Complete - Your mail has been sent";

21) sub ErrorMessage {
22) print "Content-type: text/html\n\n";
23) print "_root.Mail.EmailStatus=Connection Failed Please Check the path to the text File";
24) exit; }

Line 1 - The first line contained in the script is called the shebang line.  You will notice that it has a # symbol followed by a ! symbol.  All other occurrences of the # symbol indicates a comment in a perl script.  The first line is a special case though.  It indicates the Path to a program on the server which can execute the script - Do not indent this or put it anywhere else other then the first line.  The program in most cases is something like Perl.exe.  Don't worry about that part though. Just make sure that the path is correct.  The most common path to perl is #!/usr/bin/perl On different types and different setups of servers it may be different however.  If your using an online host they should have the path to perl listed in their online documentation.  With Telnet you can use the command - 'Which Perl' from the prompt and it will display the path.  Change this line to reflect that path.

Line 2 - of the code includes the library file "subparseform.lib" into the context of the script.  This file contains one subroutine that parses the incoming data.  You will never have to make any changes to the subparseform.lib file; it will work for any variable you can think of.  

Line 3 - Just includes a subroutine from the "subparseform.lib" file.  There is only one subroutine in this file. You can include as many others though, and use them in the script in the same fashion.  As your scripts become more complex this is an easy way to keep track of everything.  

Lines 4 to 8 - This just puts the incoming variables from the Flash movie into a form that you can work with.  It uses a function from the included "Parse_Form" subroutine.  Don't worry about this part, if you want more or less variables just use the same syntax and it'll work.

Lines 9 - 10 - These lines contain the Email address that you are sending this e-mail message to - (Note this should always be hard coded in - it's really bad to allow anonymous e-mails from your site).  And the Subject line of the E-mail your sending.  This can also be included in the Flash Movie as a hidden field.  

Line 11 - Places the incoming data into a nice readable format that is sent as the E-mail message body text. 

Line 12 - This adds some additional information to the bottom of the e-mail that you are sending.   You can add any extra info you want here.  Make sure to remember that everything has to be enclosed with " symbols and a ; symbol has to end the line.  Also not that the \n combo adds a return / new Line to your e-mail.  

Line 13 -  This just combines the variables $EmailBody and $EmailFooter into one variable $Message.  They period '.' combines the two. 

Line 14 - This line opens a connection to SendMail (Sendmail is a e-mail program that is commonly used on most Unix systems) if your using a windows server this will be different - and you should ask your host for more information on how to send e-mails with this method.  The MAIL part is simply just a reference that you will be using.  The | symbol opens a connection to the SendMail program and allows you to send data to it.  The /usr/sbin/sendmail is the path to sendmail on your server.  This is the most common path.  With telnet you can check this path by typing "Which Sendmail" or "whereis SendMail" at the prompt, the path will be displayed directly after.  The -t flag follows directly after this (make sure there is a space in between the two).  The || &ErrorMessage indicates an error message if the path to the SendMail program is incorrect. 

Lines 15-17 - This prints your data to the e-mail that you are sending.  The basic requirement for this is that you have a carriage return / new line after each - you do this by adding a \n after each part of the e-mail. 

Line 18 - This closes the Connection to the Mail program.  

Line 19-20 -  Prints out a success message which is read by the Flash movie when the script is done executing.

Lines 21 to 24 - This is just an error message that you will get if the script was unable to open the Mail Program.  Just an easy way to check to see if the scripts working.

Note on CGI vs Perl (I've found a lot of people get confused on this issue)

CGI in itself is not a programming language, it is a way of doing things (Protocol). CGI stands for Common Gateway Interface. CGI is the normal way that browsers communicate with a web server, such as when Internet Explorer talks to a Web server.  Any script that sends or receives information from a server has to follow a set of standards set by the CGI protocol.  When people are talking about CGI scripts they are talking about a script that communicates with a web server.  They can be written in Perl or other types of programming languages.  Also all perl scripts do not have to be CGI scripts - Perl is actually a real programming language and is used for many purposes other then just the Internet. 

These are the extra steps required when using the perl script.


1) Upload the the files "snowMailPerl.cgi" and "subparseform.lib" to your cgi-bin on your server.  IMPORTANT: these must be uploaded in ASCII mode.  They will Not work if you upload them in binary mode.

2) Upload your Flash swf  in Binary mode to any directory you want.

 Note: In the attached Flash .fla you will have to change the actionscript on the button that sends the mail from flash - to be able to use the Perl Script.  Change the loadvariables line to - loadVariablesNum ("", "0", "Post");

Changing the Permissions of the uploaded files on your server

You need to change the permissions of the files "snowMailPerl.cgi".  The easiest way to do this is by using telnet to log-on to your account.  Their are other ways but this is the most basic - others may be easier but it would be impossible to explain them all (ask your host how to change permissions if you are having trouble with this).  One other option for changing the permissions of the script is by using an FTP program some allow you to change the permissions directly from there.  Once you have successfully entered your login name and password you will see the shell prompt.  You must then use the CD (Change directory) command to get to your cgi-bin.  You can then list all the files in that directory by typing in ls - this will list the files located in that directory.  You can type in ls -l to list all the files plus their permissions in that directory.   In most cases it will be something like this:

%cd public_html
%cd cgi-bin
%ls -l
total 2
-rwxr-xr-x  1   July 31 15:30 snowMailPerl.cgi
-rw-rw-r--  1   July 31 15:00 subparseform.lib

I don't want to go into all the details on setting the file permissions so I'll just tell you what you need to do for this script. You need to change the permissions to 755.  You can do this with the following command.  

%chmod 755 snowMailPerl.cgi
 Note: Permissions on a Unix server are based on 3 different category's - Owner, Group, and Other/everyone.  These are further broken down to 3 levels - Read, Write, and Execute. When you first upload a script it is usually set so that the Owner has read and write permission's and everyone else can just read.  Because you want others to be able to execute the script you have to change the permissions of the files.  Therefore you have to change the permission's of the script to 755 - so that everyone can execute it.  This mainly applies to Perl scripts and files that you need to write to on the server.  
» Level Advanced

Added: 2001-08-27
Rating: 9 Votes: 328
(10 being the highest)
» Author
No details available.
» Download
Download the files used in this tutorial.
» Forums
More help? Search our boards for quick answers!


  • There are no comments yet. Be the first to comment!

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.
Featured Flash FLA
» Author: Inocreato
» Title: RaiseTheBlocks
» Description: Raise all the blocks to win the game
Featured Sound Loops
Image for equalize

Audio Player

» Author: TomCat Carty
» Title: The Wood
» Description: Just a little game ending or it can maybe be looped. Recorders with music box and percussion to give the feel of well, I don't know, the woods? Free to use, just credit me. thank you
Latest Font
» Author: Fábio FAFERS
» Description: I created this font for free use. Everyone can apply it in personal or business texts. Its free, but I want to be communicated in case of business use. Donations are accepted to keep the project of free fonts alive! Thank you all
Featured Sound Fx
Image for equalize

Audio Player

» Author: Davisigner
» Description: Hmm... what to say about this one? It's reminiscent of the closing notes of the opening music from the Three Stooges done in a church organ style with a closing cymbal crash. I'll give this one away gratis, but feel free to check out my free loops and potential upcoming license-mandated ones over in the respective part of Flashkit.