Products
Services
Contact Us

Scripts: Perl :: Mail functions :: Library Article #3

Developer's Section

Process form data and send mail using Perl
By: Erobo Team Member

Hire a Developer for Related Work / Installation | $55 hr
Rating:  | Rate It:   
Average Votes: (1906)
Favorites:

In this example we will look at processing form data and sending email using the Perl programming language.

In the following script we will be showing how to process form data when a new request have been received. i.e. when someone has posted data to the script. Then the script will use each of the fields to construct and email message, and then redirect to a response page.

Note: Error handling techniques are also shown.

 Code Snippet 1

#!/usr/bin/perl -w

#Using Perl Error Handling Exceptions 
#(Output errors before executing the script)

qw(:standard);

use CGI::Carp qw(fatalsToBrowser);

#end error handling

#print "Content-type: text/html\n\n";

# process POST information
read(stdin, $input_line, $ENV{CONTENT_LENGTH});
$input_line =~ tr/+/ /;
@fields = split(/\&/,$input_line);
foreach $i (0 .. $#fields) {
    ($name,$value) = split(/=/,$fields[$i]);
    $name =~ s/%(..)/pack("c",hex($1))/ge;
    $value =~ s/%(..)/pack("c",hex($1))/ge;
    $input{$name} = $value;
}

$mailprog = "/usr/sbin/sendmail";

open (MAIL, "|$mailprog -t") || exit "Can't open $mailprog!\n";
print MAIL "To: Erobo Administrator <services\@erobo.net>\n";
#print MAIL "To: Erobo Administrator <services\@erobo.net>\n";
print MAIL "From: myscript\@erobo.net 
                               <services\@erobo.net.com>\n";
print MAIL "Subject: Form Data processing from erobo \n\n";


print MAIL "First Name:  $input{'fname'}\n\n";
print MAIL "Last Name:  $input{'lname'}\n\n";
print MAIL "Address:  $input{'address'}\n\n";
print MAIL "City:  $input{'city'}\n\n";


print MAIL "State:  $input{'state'}\n\n";
print MAIL "Zip:  $input{'zip'}\n\n";
print MAIL "Email:  $input{'email'}\n\n";
print MAIL "Comments:  $input{'comments'}\n\n";


print MAIL "Type of User:  \n";

if ($input{'programmer'} ne "") {
print MAIL "\tI'm a  professional programmer\n";
}
else {
        print MAIL "\tI'm a business customer\n";
}

close (MAIL);

# Redirect the user
print("location:http://www.yoursite.com/success.html\n\n");

# eroboteammember@erobo.net


See other Scripts in Mail functions

Submit Your Scripts:


System Message:
  • Your Perl script has been sent. Please allow 48 hours for processing.


If you would like to have your Perl scripts published in this section please fill out
the form below:
*Your Name or Username:
Home Town:
*Email:
*Description and Code:
*Enter Code shown
to the right:

[ Refresh Image ]