Sending Web Form Data Set as Email with ActivePerl
Sending Email with ActivePerl – Part 2
Forward: In this part of the series we see how to send web form data set as email, using ActivePerl.
By: Chrysanthus Date Published: 28 Jul 2012
Introduction
Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.
Everything said in this article is applicable to traditional Perl. However, with traditional Perl you have to precede your code with something like, #!/usr/bin/perl .
You need basic knowledge in HTML (or XHTML) and ActivePerl (Perl) in order to understand this article. If you do not have basic knowledge in HTML then read the series I wrote titled, XHTML Basics. To arrive at the series, type the title and my name Chrys in the Search Box of this page and click Search.
Example Email
Consider the following Perl email ready to send from a web site server:
open(SENDMAIL, "|/usr/lib/sendmail -oi -t")
or die "Can't fork for sendmail: $!\n";
print SENDMAIL <<"EOF";
From: <jsmith@myserver.com>
To: <maryt@herserver.net>
Subject: Illustration
Date: Fri, 21 Nov 2010 09:55:00 -0000
First Name: John
Second Name: Smith
Message:
Dear Madame,
The above salutation line is one paragraph from the point of view of the layman, because a blank line follows it. This is the third paragraph from the layman’s point of view and no line here should be longer than 78 characters including the spaces. The first three lines of the body of this message also form a paragraph from the point of view of the layman.
No line in the email header section should also be longer than 78 characters. In the body of an email the paragraphs of the layman are separated by blank lines. To achieve a paragraph (for layman), type the enter key twice immediately you complete the last line of the previous paragraph. The following two readable lines of this email form a paragraph.
Sincerely,
John Smith
EOF
close(SENDMAIL) or warn "sendmail didn't close properly";
The information in the body of the email message above can be filled in a web (HTML) form to be sent by email. In order for the email receiver (human) to reply your web form information, the web form should have a field for you to fill in your email address. The reply will go to your email box and not to the web server that hosts the web form and its web site. The reply will not go to your web server even though your email Perl script is in your web server.
In order for the information to go to the receiver’s email box, his email address also has to be in the web form. This is usually hard coded in hidden input type control in the web form. The form for the above email is as follows:
<form action="aperlscript.pl" method="post">
<h3>Form Heading here</h3>
<input type="hidden" name="Recipient" value="maryt@herserver.net"><br /><br />
*First Name: <input type="text" name="First Name"><br /><br />
Last Name: <input type="text" name="Last Name"><br /><br />
*Email: <input type="text" name="Email"><br /><br />
Subject: <input type="text" name="Subject"><br /><br />
*Message: <textarea cols="50" rows="3" name="Message"></textarea><br /><br />
<button type="submit">Send</button>
</form>
The value of the action attribute is aperlscript.pl. This is the name of the Perl file script that will receive your data set from the user’s browser, convert it to an email and send the email to the email server. You can give whatever name you want for the file, but end it with the extension, .pl. For a data set, the name of a form control and its value are sent when the submit button is clicked. So, make sure each of the important controls of your form has a name. A control name, such as “First Name” can be important text in the body of the email message. Note that the user types only his first name and he does not type the string, “First Name”. This string is the name of a control and should be important text in the body of the message. Precisely, the body of the message will have,
First Name: John
Here, the string “First Name” is coming from the name of a form control, while “John” is the value typed in the control’s field.
In the form code, the hidden input control has the email address of the email box of the person who will receive the email. Hidden input controls are not displayed when the web form is displayed. You have three input text controls in the form code. Unlike with the hidden control, these text controls will be filled by the Internet user at his client browser. He has to fill his first name, he has to fill his last name and he has to fill his email address in order to have a reply. The name of the first input text control is, “First Name”; the name of the second input text control is “Last Name”; and the name of the last input text control is “Email”.
The user will type his email address in the Email input text control, without the angle brackets and without any back slash in front of @. In the Text Area control the user will type his message (personal message). What he types here will not include his first name, last name or email address. When converting the data set into an email, our Perl script in our web server will put the user’s email address in the header section of the email message. The email message consists of the email header section and the email body. The form message is only what you type in the Text Area control.
When the form is displayed, it looks as if the first name, last name and email will go into the email header section. The email will go into the email header section but the first and last names will go to the body section of the email. Names (user names) are not part of the email header section.
Do not confuse between the form (user’s) message, the email message and the email message body. The form message is what the user types in the Text Area control field of the form. The email message consists of the header section and body section of the email. The email body has the sender’s credentials and the sender’s personal (Text Area) message.
There are three main code segments of the Perl script. The first obtains the values of the controls in the data set sent when the user clicked the submit button. Remember, the Perl script is in the server that receives the form information. The second main code segment validates the values received to see if they were well type; if not, error message is sent back to the user for retyping and the email is not sent to the email server. If there is no typing error, the third main segment prepares and sends the email; otherwise the third main segment does not send the email.
The First Main Code Segment
This is the first main code segment for the Perl script for the above email:
use strict;
print "Content-Type: text/html\n\n";
#obtain values from web form
use CGI;
my $query = new CGI;
my $recipientVal = $query->param('Recipient');
my $fnameVal = $query->param('First Name');
my $lnameVal = $query->param('Last Name');
my $emailVal = $query->param('Email');
my $sbjVal = $query->param('Subject');
my $msgVal = $query->param('Message');
The first two lines are needed for strict coding and if you want to send feedback to the browser. There is a Perl module called the CGI module. This module has what is called the CGI class. To use this module, you import it with the statement, “use CGI;” After this statement above, the CGI object is created with the name, $query. The next six statements read the values of the form controls that are now in the $query object. When the Submit (Send) button of the HTML form is clicked, the values of the controls go to the CGI object ($query) that you have to create in your Perl script. As soon as the object is created in the Perl script, you can obtain the values of the controls as the next six lines above do. To get the value of each control, you need the name of the control. You assign the return value to a variable with any name you want.
These assigned variable names will be used when forming the Perl email code. For example, $recipientVal above will be used in the email code in place of the To email address. The control names, First name, Last Name and Message of the HTML form will also be used in the email body. That is how you do it. You have to use the assigned variable names and some of the control names in the email code as will be illustrated below.
The second main code segment can be very large. It uses the assigned variables to determine if the values of the HTML form controls were properly typed. It they were not properly type, it sends error messages back to the browser and the third main code segment that forms and sends the email is not executed. You should consult other documents in this blog to see how to write the second main code segment effectively; I will show you a simple form of that in the fourth part of this series.
The Third Main Code Segment
The third main code segment is the formation of the Perl email code. It needs the assigned variables and some of the control names. This is part of the third main code segment for the above form and supposed email (see details in part 4):
open(SENDMAIL, "|/usr/lib/sendmail -oi -t")
or die "Can't fork for sendmail: $!\n";
print SENDMAIL $emailMessage;
close(SENDMAIL) or warn "sendmail didn't close properly";
The two statements for the end of file marker have not been used this time. The header section and the email message body are all now in one string, whose variable is $emailMessage. After opening the filehandle, you send the complete message string with the print function using the filehandle. Then you close the filehandle; that is the process here. We shall see how this complete message string is formed in the next part of the series.
So if the third main code segment above is executed, the email will be sent to the email box of the recipient. Let us end here and continue in the next part of the series.
Chrys
Related Links
Major in Website DesignWeb Development Course
HTML Course
CSS Course
ECMAScript Course
NEXT