Broad Network


Perl CGI and Email for Web Development

Web Development Basics with Perl and MySQL – Part 3

Using Apache Web Server

Foreword: In this part of the series, I talk about Perl CGI and Email for Web Development.

By: Chrysanthus Date Published: 27 Apr 2016

Introduction

This is part 3 of my series, Web Development Basics with Perl and MySQL. In this part of the series, I talk about Perl CGI and Email for Web Development. The Perl installation comes with a module called the CGI module. CGI stands for Common Gateway Interface. You do not need to know the details of that module before you use it for web development. You just need to know what you need and then use it; and that is what I explain to you in this part of the series. You should have read the previous part of the series before reaching here, as this is a continuation.

Data from the browser at the client’s computer in the Internet, arrives at the server. At the server, you can use the CGI module to extract the data from the form it arrived at. After extracting, you can now send the data to an email box or to a database server. You do not need the PurePerl MySQL API to send the data to an email box. However, you need the PurePerl MySQL API to send the data to a MySQL server.

I spend the first half of this tutorial explaining how to use the CGI; and then the second half explaining how to send an email.

The CGI Class and Objects
To use the CGI module, you need to have,

    use CGI;

at the top of the block of your script that needs the CGI features. Your script is in a sub directory of the web server (any web server comes with a number of directories). The CGI is a class, from which objects can be created. Below the above statement, you can go ahead to instantiate an object, as in:

    my $obj = CGI->new();

where $obj is a name of your choice for the object, and the rest of the words in the statement are reserved words.

The web Form at the client sends data, typically using one of two methods. You have the GET or the POST methods. Whichever method has been used, the CGI collects the data for you. You just need to know how to get the data from the CGI.

Nature of Source Data
At the web page Form, the data is coded in name/value pairs, as follows

    email/chrys@broad-network.com
    firstname/Chrysanthus
    title/M.
    job/system analyst

There are 4 name/value pairs here: email is a name, firstname is a name, title is a name, and job is a name. And there are 4 values: chrys@broad-network.com is a value; Chrysanthus is a value; “Mr.” is a value; and “system analyst” is a value. So there are 4 name/value pairs with each name corresponding to a value. There can be more or less than 4 name/value pairs.

In normal programming, you should know the names for the name/value pairs. You will then use the names to obtain the values through CGI. However, if you do not know the names, CGI offers you a way of knowing all the names – see below.

Obtaining the Names
For the name/value pair, “email/chrys@broad-network.com” if you know the name, email, you can obtain the value by coding the following after the instantiation of the object:

    my $email = $obj->param('email');

Here, $email is the script variable of your choice. $obj is the object you created. -> is an operator that relates the object to its method, param(). So param is a kind of reserved word, here. 'email' in quotes is the name of the name/value pair. You obtain the other values in the same way. To obtain the value for the name, job, you will type something like,

    my $profession = $obj->param('job');

where $profession is the variable of your choice and the rest of the words at this point, are not really of your choice.

Multivalued Parameters
A few parameters that arrive are multivalued, e.g. from multiple selections in a scrolling list of the web page Form. In this case, the value of the name/value pair, is a list. No problem; CGI provided the solution. You receive the list as in the following statement:

    my @values = $obj->param('foo');

where @values is a variable of your choice. Note that this variable, unlike the previous ones, is an array, because of the preceding, @ to hold the list. Here, the name/value pair has the name “foo”  and the value is a list. You should know the name, foo in advance. You can get this from the web page that sent the data.

List of Names
If for some reason or the other, you do not know the names for the name/value pairs, there is no problem. The CGI module already provided a solution. It is to code:

    @names = $obj->param;

where @names is a variable of your choice. It is an array that holds the list of all the names of the name/value pairs sent from the client. $obj is the object you created above. The rest are reserved words in this situation.

So, after obtaining the data sent by the client, you can then send the data to an email box or to a database. I hinted you how to send data to the database in the previous part of the series. However, for the rest of this tutorial, I will briefly explain how to send data to an email Box. Sending data to the database will be treated in detail in later parts of this series.

Email
An email box is a set of directories or similar structure that holds the emails sent to you. These directories belong to a software called the email server. The email server can be in the same computer as your database server or in some other computer; that does not matter here. What you need in the same computer as your script, is a program called the sendmail program. This program may have some other name, but its purpose is to send the data to the destination email box. You do not have to write this sendmail program; it is already written for you and available in the server; you just use it.

Effective Email Example
Now, if you type the following in your script as presented, the message body beginning from “Dear Madam” right up to “John Smith” (inclusive) will be sent to the destination email box:

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: Reminder
Date: Fri, 21 Nov 2010 09:55:00 –0000
Cc: <webmaster@server.com>

Dear Madam,

I am by this email reminding you to pay your dues. We also want to let you know that an email message is made up of lines, and after each line you must press the enter key of your keyboard if you are typing the email in a text editor and if the email will be sent as typed, through the Internet cable without modification.

Sincerely,
John Smith
EOF
close(SENDMAIL)  or warn "sendmail didn't close properly";

This structure begins with an open statement, which opens a channel for the email to be sent by the email program. Next you have the print command (function), which prints everything from “"EOF";” to EOF (exclusive), to the channel. Now, within “"EOF";” and EOF, the Enter key has been pressed everywhere, just before you type the next line. No line in the email message should be longer than 78 characters; this rule also applies to the body of the message.

Within “"EOF";” and EOF, “From:” is a field name, whose value is, <jsmith@myserver.com>; it can be “John Smith<jsmith@myserver.com>". “To:” is a field name whose value is <maryt@herserver.net> . “Subject:” is a field name, whose value is Reminder. “Date:” is a field name whose value is Fri, 21 Nov 2015 09:55:00 –0000 . The date value has to be typed in that format. “Cc:” is a field name whose value is <webmaster@server.com> . The rest of the text from “Dear Madam” to “John Smith” is the email body. The blank line between “Cc: <webmaster@server.com>” and “Dear madam” has to be there.

Now, all the above values and the email body, come from the client. That is where CGI module comes in. The rest of your coding has to produce the email structure and send (using the mail program) to the destination email box. The name of the mail program in the above structure is sendmail.

In a later part of this series, I will present a Perl script that produces the email automatically. I will also explain the feedback coding to confirm that the message has been sent or it has not been sent.

Conclusion
The CGI module extracts the data from what is received at the server. From there the data can be constructed into an email and sent with the help of a send mail program to an email box, or the data can be sent to a database with the help of an API.

We stop here and continue in the next part of the series.

Chrys

Related Links

Web Development Basics with Perl and MySQL
Perl Validation of HTML Form Data
Page Views with Ajax and Perl and MySQL
Web Live Text Chart Application using Perl and MySQL
More Related Links
Perl Mailsend
PurePerl MySQL API
Perl Course - Professional and Advanced
Major in Website Design
Web Development Course
Producing a Pure Perl Library
MySQL Course

BACK NEXT

Comments

Become the Writer's Follower
Send the Writer a Message