Приглашаем посетить
Дружинин (druzhinin.lit-info.ru)

13.5 Emailing and Perl

Table of Contents

Previous Next

13.5 Emailing and Perl

13.5.1 My first Perl script

Similar to ASP and PHP, Perl is a server language for writing script programs for the World Wide Web. It was developed in the late 1980s by Larry Wall to run initially in a UNIX environment and later adopted in many other operating systems. Perl scripts in this book will have the file extension .pl. This section is not an introduction to the language, but rather a collection of simple Perl program fragments for email applications.

Although Perl is available on PC and Microsoft systems, we will follow the original UNIX design. It's assumed that you have a general UNIX (or LINUX) environment. With the basic UNIX structure and its naming convention, you should have a directory in which XHTML documents are kept. This directory is usually called public_html under your login directory. Your ISP may have different arrangements and should be able to provide you with the information. For PC and Microsoft system users, a suitable substitution of the directory may be needed. The Perl scripts should be stored in a directory executable by the Perl interpreter. This directory may be the same as public_html (or a subdirectory called cgi-bin) and is accessible by the usual http:URL format. We also assume that the Perl interpreter is located in the directory "/usr/bin/perl." You may, of course, have a different location for the interpreter.

Perl is different from ASP and PHP for which script statements can be mixed or embedded into an XHTML page. The Web engines are happy to ignore any invalid statements. In other words, both ASP and PHP will pass everything they don't understand back to the caller. Perl, on the other hand, is a strict language in that every line of a Perl program should be a valid Perl statement.

Similar to ASP, PHP, and other server scripts, Perl can also be used to return a page of information (usually an XHTML document) to the Web browser after the browser submits a request. A typical XHTML page to call a Perl program is



Example: ex13-18.htm - My First Page With Perl

 1: <?xml version="1.0" encoding="iso-88591"?>
 2: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 3:     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 4: <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 5: <head><title>My First Perl</title></head>
 6: <body>
 7: <form method="post" action="ex13-18.pl">
 8: Please Press Button To Activate A Perl Program<br /><br />
 9: <input type="submit" value="Press Me">
10: </form>
11: </body>
12: </html>

This is a form application. It acts as an interface for a Perl program. When a user clicks the Press Me button as indicated in line 9, the form action activates a Perl program called ex13-18.pl located in the URL address www.pwt-ex.com. This Perl program returns the following simple XHTML page:



Example: ex13-18.pl - The Perl Script For ex13-18.htm

 1: #!/usr/local/bin/perl
 2: print ("Content-type:text/html\n\n");
 3: print << "mywebpage";
 4:  <?xml version="1.0" encoding="iso-88591"?>
 5:  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 6:     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 7:  <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 8:  <head><title>My First Perl</title></head>
 9:  <body>
10:   My First Perl Program <br >
11:   Thank you For Calling Me.
12:  </body>
13:  </html>
14: mywebpage
15:

The first line of the script informs the system where to find the Perl interpreter, and the second line alerts the calling browser that it is about to receive an XHTML document. The remaining part of the script makes use of a Perl facility called a here document. The here document will print everything between the statements print<<"mywebpage" (line 3) and mywebpage (line 14). This here document feature is useful for laying out XHTML documents to be returned by Perl scripts. If you prefer, you can replace the here document feature by a series of print statements. In practice, simple cut and paste of a proven and tested XHTML document can eliminate most human errors. A Web page returned in this way may be as simple or as complex as you like. Some screen shots of this example are given in Figs 13.29 and 13.30.

Figure 13.29. ex13-18.htm

graphics/13fig29.gif


Figure 13.30. ex13-18.p1

graphics/13fig30.gif


Perl is a popular language on the Web. One reason is that it provides a rich set of built-in instructions for Web page editing. For example, the previous example can be rewritten as



Example: ex13-19.pl - My First Perl Script II

 1: #! /usr/bin/perl
 2:
 3: use warnings;
 4: use strict;
 5: use CGI qw(:standard);
 6:
 7: print (header());
 8: print (start_html("My First Perl"));
 9: print ("My First Perl Program", br(),"Thank you For Calling Me.");
10: print (end_html());

Perl uses the "use" directive to include modules as built-in library functions. Once you have included the module, you can call the associated functions directly. The warnings and strict modules used in lines 3 and 4 will instruct the interpreter to check valid Perl programming styles and produce warning messages.

The CGI module provides a rich set of standard functions to generate XHTML elements and related functionalities. For example, the header function in line 7 generates the HTML header. The details of the header string will depend on the Perl used, but for most Perl interpreters, this will be an XHTML header. The start_html()function returns the statement



<head><title>My First Perl</title></head><body>

The br() function generates a line break <br />. The end_html() function returns the string "</body></html>" and concludes the document. If you run this Perl program from your browser (e.g. http://www.pwt-ex.com/book/chap13a/ex13-19.p1) you will see Fig. 13.30 and the HTML/XHTML code generated by the Perl script via the "source" option of the browser. This generated XHTML page is shown in listing ex13-07.txt.



Listing: ex13-07.txt

 1: <?xml version="1.0" encoding="utf-8"?>
 2: <!DOCTYPE html
 3:   PUBLIC "-//W3C//DTD XHTML Basic 1.0//EN"
 4:   "http://www.w3.org/TR/xhtml-basic/xhtml-basic10.dtd">
 5: <html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
 6: <head><title>My First Perl</title></head>
 7: <body>
 8:   My First Perl Program<br />
 9:   Thank you For Calling Me.
10: </body>
11: </html>

13.5.2 Using Perl to get user name and email address

Before you can send email information, you need to construct a Web page to obtain the name and email address of the user. The following page is used as an interface:



Example: ex13-20.htm - Getting Email Input With Perl

 1: <?xml version="1.0" encoding="iso-88591"?>
 2: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 3:     "http://www.w3.org/TR/xhtml1/DTD/xhtml1transitional.dtd">
 4: <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 5: <head><title> Getting Email Input With Perl  ex1320.htm</title></head>
 6: <style>
 7:   .tx01{font-size:14pt;color:#ffff00;font-weight:bold}
 8:   .tx02{font-family:arial;font-size:14pt;background:#ffffff;color:#000000}
 9:   .butSt{background-color:#aaffaa;font-family:arial;font-weight:bold;
10:      font-size:14pt;color:#008800;width:80px;height:30px}
11: </style>
12: <body style="background:#000088;font-family:arial;color:#ffff00">
13: <div align="center" style="font-size:18pt"> <br />
14:   Please Join Our Mailing List<br />
15:  <form method="post" action="ex13-20.pl">
16:  <table class="tx01">
17:   <tr><td>Name:</td>
18:    <td><input class="tx02" type="text" name="name" id="name"></td></tr>
19:   <tr><td>Email:</td>
20:    <td><input class="tx02" type="text" name="email" id="email"></td></tr>
21:   <tr><td colspan=2><input class="butSt" type="submit" value="Submit">
22:   </td></tr></table>
23:   </form>
24: </div>
25: </body>
26: </html>

This is a page with a simple form. Once the Submit button is clicked, the form action will call the Perl program ex13-20.pl located in the same directory. In order to obtain the data correctly from the Web page, the Perl program needs a mechanism to catch the input from this page. The corresponding Perl program is listed as follows:



Example: ex13-20.pl - The Perl Script For ex13-20.htm

 1: #! /usr/bin/perl
 2:
 3: use warnings;
 4: use strict;
 5: use CGI qw(:standard);
 6:
 7: my $usr_name=param("name");
 8: my $usr_email=param("email");
 9:
10: print ("Content-type/html\n\n");
11: print << "mypage";
12:   <?xml version="1.0" encoding="iso-88591"?>
13:   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
14:     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
15:   <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
16:   <head><title>ex1320.pl </title></head>
17:   <body style="background:#000088">
18:   <div style="font-family:arial;font-size:18pt;color:#ffff00">
19:      Your Name is $usr_name <br />
20:      Your Email Address is <br /> $usr_email <br />
21:   </div>
22:   </body>
23:   </html>
24: mypage

The only new statements in this program are lines 7 and 8. The parameter function



my $usr_name=param("name");

is used to catch the name returned by the interface page ex13-20.htm. Any input field from the form interface can be accessed by the param() function of Perl. The next step is to assign this user name as a variable in Perl. Variable names in Perl have a prefix dollar sign, $. The keyword my is a Perl identifier to indicate a local variable. Once the variables $usr_name and $usr_email are defined, they can be used anywhere inside the Perl program. Some screen shots of ex13-20.htm and ex13-20.pl in action are shown in Figs 13.31 and 13.32.

Figure 13.31. ex13-20.htm

graphics/13fig31.jpg


Figure 13.32. ex13-20.p1

graphics/13fig32.jpg


13.5.3 Mailing information to users with Perl

In this section, we will discuss two popular ways to deliver email using Perl. The first one is to use a mailing agent called sendmail and the second one is to use an SMTP mail server. Application sendmail is a popular mail program that is available in almost all UNIX environments; in fact, some books are dedicated entirely to this program. To call sendmail, the following program framework is frequently used:



Example: ex13-21.pl - An Email Framework With Perl

 1: #! /usr/bin/perl
 2: open (USRMAIL, "| /usr/lib/sendmail -oi -n -t" );
 3:
 4: print USRMAIL << "usr_message";
 5: To:John\@www.pwt-ex.com
 6: From:JohnSmith\@www.pwt-ex.com
 7: Subject:Testing
 8:
 9: Dear John:
10:
11: This is testing Email from JohnSmith@pwt-ex.com to John@pwt-ex.com
12: using Perl.
13:
14: JohnSmith
15: usr_message
16: close USRMAIL

The open statement in line 2 activates the sendmail program located in the /usr/lib directory. The parameters associated with sendmail are used to convert the program to a file handle (or stream) with a name USRMAIL. You can then use this file handle to insert a whole email message by using the here document feature of Perl. At the end, we terminate the stream by issuing a close statement.

The information in the To: and From: fields can be any acceptable email address. The slash before the "@" symbol in line 5 is a requirement in order to generate a proper "@" symbol. After that, you have the Subject: field and the contents of the email body. As a feature of Perl, you can of course use variables inside the email.

As a practical example, the following Perl program is constructed to email information to the requestor. This example has the same interface as ex13-20.htm, but embedded entirely in a Perl program.



Example: ex13-22.pl - The Perl Script for generating XHTML

 1: #! /usr/bin/perl
 2: use warnings;
 3: use strict;
 4:
 5: print "Content-type:text/html\n\n";
 6: print << "mypage";
 7:  <?xml version="1.0" encoding="iso-88591"?>
 8:  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 9:     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
10:  <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
11:  <head><title>Send Email Using Perl (I)</title></head>
12:  <style>
13:   .tx01{font-size:14pt;color:#ffff00;font-weight:bold}
14:   .tx02{font-family:arial;font-size:14pt;background:#ffffff;color:#000000}
15:   .butSt{background-color:#aaffaa;font-family:arial;font-weight:bold;
16:      font-size:14pt;color:#008800;width:80px;height:30px}
17:  </style>
18:  <body style="background:#000088;font-family:arial;color:#ffff00">
19:  <div align="center" style="font-size:18pt"><br />
20:    Please Join Our Mailing List<br />
21:  <form method="post" action="ex1323.pl">
22:  <table class="tx01">
23:   <tr><td>Name:</td>
24:    <td><input class="tx02" type="text" name="name" id="name"></td></tr>
25:   <tr><td>Email:</td>
26:    <td><input class="tx02" type="text" name="email" id="email"></td></tr>
27:    <tr><td colspan=2><input class="butSt" type="submit" value="Submit">
28:    </td></tr></table>
29:   </form>
30:  </div>
31:  </body>
32:  </html>
33: mypage
34:

As you can see from this example, it is possible to develop Web pages entirely by using Perl. All you have to do is to copy the page into a here document. To capture the name and address of this page and to send email to the recipient, another Perl program is developed as follows:



Example: ex13-23.pl - The Perl Script For ex13-23.htm

 1: #! /usr/bin/perl
 2:
 3: use warnings;
 4: use strict;
 5: use CGI qw(:standard);
 6:
 7: my $usr_name=param("name");
 8: my $usr_email=param("email");
 9:
10:
11: print "Content-type:text/html\n\n";
12:
13: print << "mypage";
14:  <?xml version="1.0" encoding="iso-88591"?>
15:  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
16:     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
17:  <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
18:  <head><title>ex1323.pl </title></head>
19:  <body style="background:#000088">
20:  <div style="font-family:arial;font-size:18pt;color:#ffff00">
21:  <h1>Thank you!</h1>
22:      Newsletters and special discount messages will be sent to
23:      <span style="color:#ffffff">$usr_name </span>at
24:      <span style="color:#ffffff">$usr_email</span>.
25:  </div></body></html>
26: mypage
27:
28: open (USRMAIL, "| /usr/lib/sendmail -oi -n -t" );
29:
30: print USRMAIL << "usr_message";
31: To:$usr_email
32: From:JohnSmith\@www.pwt-ex.com
33: Subject:Congratulations
34:
35: Dear $usr_name:
36:
37: Thank you for filling out our mailing list form.
38: As a registered customer, I would like to inform you
39: that you are entitled to a further 10% discount on all
40: brand products for the next three months.
41:
42: Sales Manager
43: John Smith
44: www.pwt-ex.com
45:
46: usr_message
47: close USRMAIL;

This program contains three parts. The first part is to get the name and address from an interface such as ex13-23.htm. This interface is the same as ex13-20.htm but calls ex13-23.pl instead. The second part (lines 1326) is a "Thank you" page to provide an instant acknowledgment. The final part is the emailing with sendmail. The variables $usr_email and $user_name in the mail content (lines 31 and 35) are used to ensure that the mailing message is a personalized one. Some screen shots of this example are given in Figs 13.33 and 13.34.

Figure 13.33. ex13-23.htm

graphics/13fig33.jpg


Figure 13.34. ex13-23.p1

graphics/13fig34.jpg


If for any reason the sendmail program is not available, you can still use Perl to send your email if the name of your SMTP server is known. As mentioned at the beginning of this chapter, your ISP should be able to provide you with the name of the SMTP server. You need this information to set up a mail client such as Outlook Express.

First, you need to develop a Perl program to generate the "To," "From," "Subject," and "Message" information. This program can easily be constructed as follows:



Example: ex13-24.pl - SMTP Server Using Perl

 1: #! /usr/bin/perl
 2: print ("Content-type/html\n\n");
 3: print << "mypage";
 4:  <?xml version="1.0" encoding="iso-88591"?>
 5:  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 6:     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 7:  <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 8:  <head><title>ex1324.pl</title></head>
 9:  <style>
10:   .tx01{font-size:14pt;color:#ffff00;font-weight:bold}
11:   .tx02{font-family:arial;font-size:14pt;background:#ffffff;color:#000000}
12:   .butSt{background-color:#aaffaa;font-family:arial;font-weight:bold;
13:      font-size:14pt;color:#008800;width:80px;height:30px}
14:  </style>
15:  <body style="background:#000088;font-family:arial;color:#ffff00">
16:  <div align="center"$style="font-size:18pt">
17:   Please Enter Your Name and <br />Email Address
18:   <form method="post" action=" ex1325.pl">
19:   <table class="tx01">
20:    <tr><td>From:</td>
21:     <td><input class="tx02" type="text" name="from" id="from"></td></tr>
22:    <tr><td>To:</td>
23:     <td><input class="tx02" type="text" name="to" id="to"></td></tr>
24:    <tr><td>Subject:</td>
25:    <td><input class="tx02" type="text" name="subject" id="subject"></td>
26:    </tr><tr><td valign="top">Message:</td>
27:     <td><textarea name="message" id="message" class="tx02"
28:         rows="6" cols="40" wrap="yes"></textarea></td></tr>
29:    <tr><td><input class="butSt" type="submit" value="Submit"></td>
30:     <td><input class="butSt" type="reset" value="Reset"></td></tr>
31:    </table>
32:    </form>
33:   </div>
34:   </body>
35:   </html>
36: mypage
37:

Again, this is just another copy and paste example for generating XHTML using Perl. You can run this Perl page directly. The "To," "From," "Subject," and "Message" information will be sent to a Perl program called ex13-25.pl once the "Submit" button is clicked. Some screen shots of this page are given in Figs 13.35 and 13.36.

Figure 13.35. ex13-24.p1

graphics/13fig35.jpg


Figure 13.36. ex13-25.p1

graphics/13fig36.jpg


In order to connect an SMTP server, you need an external module called "NET::SMTP" which can be found inside a library called libnet. Most Perl implementations will have this package installed. For some systems, you may need to install it yourself. Suppose the identity of an SMTP server that you can use is smtp.pwt-ex.com. The following Perl program, ex13-25.pl, can be used to get the email data and deliver the message via the SMTP mail server.



Example: ex13-25.pl - Perl Script For ex13-24.pl

 1: #! /usr/bin/perl
 2:
 3: use strict;
 4: use warnings;
 5: use Net::SMTP;
 6: use CGI qw( :standard );
 7:
 8: my $from = param( "from" );
 9: my $to = param( "to" );
10: my $subject = param( "subject" );
11: my $message = param( "message" );
12:
13: print ("Content-type/html\n\n");
14: my $smtp = Net::SMTP->new ("smtp.pwt-ex.com")
15:    or die("Cannot connect to SMTP server: $!");
16:
17: $smtp->mail( "$from" );
18: $smtp->to( "$to" );
19:
20: $smtp->data();
21: $smtp->datasend( "From: $from\n" );
22: $smtp->datasend( "To: $to\n" );
23: $smtp->datasend( "Subject: $subject\n\n" );
24: $smtp->datasend( "$message\n" );
25: $smtp->dataend();
26: $smtp->quit();
27:
28: print << "mypage";
29:  <?xml version="1.0" encoding="iso-88591"?>
30:  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
31:     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
32:  <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
33:  <head><title>Sending Email Using SMTP Server</title></head>
34:  <body style="background:#000088">
35:  <div style="font-family:arial;font-size:18pt;color:#ffff00">
36:       Your Email has been sent to $to
37:  </div></body></html>
38: mypage
39:

After the collection of data from the interface (lines 811), a connection to the server (line 14) is made and the object is assigned a local variable $smtp. Since $smtp is an object, you can call all its member functions directly. The first function associated with $smtp is $smtp->mail ("$from"). This function is to tell the server who is the sender of the email. The function $smtp->to("$to") identifies the recipient. The remaining series of function calls (lines 2026) are used to create a data section and fill in the necessary To:,From:,Subject:, and Message: fields of an email. The dataend() function terminates the data section and quit() disconnects the SMTP server.

The last section of this program (lines 2838) is just a Web page used to confirm delivery.

13.5.4 Checking emails with Perl

If you have a POP3 account somewhere on the Internet, you can use the next example to check how many emails are in your inbox. In order to gain access to your mail account, you need the following information:

  • User name Your mail account name, e.g., JohnSmith@pwt-ex.com.

  • Password The password to gain access to the account.

  • Server The name of the POP3 server or machine that holds your emails.

All this information should be available to you from your system administrator or ISP. To develop an interface to obtain the information, the following Perl program is used:



Example: ex13-26.pl - Accessing POP3 Account Using Perl

 1: #! /usr/bin/perl
 2: print ("Content-type/html\n\n");
 3: print << "mypage";
 4:  <?xml version="1.0" encoding="iso-88591"?>
 5:  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 6:     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 7:  <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 8:  <head><title>Accessing POP3 Using Perl  ex1326.pl</title></head>
 9:  <style>
10:   .tx01{font-size:14pt;color:#ffff00;font-weight:bold}
11:   .tx02{font-family:arial;font-size:14pt;background:#ffffff;color:#000000}
12:   .butSt{background-color:#aaffaa;font-family:arial;font-weight:bold;
13:      font-size:14pt;color:#008800;width:80px;height:30px}
14:  </style>
15:  <body style="background:#000088;font-family:arial;color:#ffff00">
16:  <div align="center" style="font-size:18pt">
17:   Check Email From a<br />POP3 Server<br />
18:   <form method="post" action="ex1327.pl">
19:   <table class="tx01">
20:    <tr><td>Username:</td><td><input class="tx02" type="text"
21:          name="username" id="username"></td></tr>
22:    <tr><td>Password:</td><td><input class="tx02" type="password"
23:          name="password" id="password"></td></tr>
24:    <tr><td>POP Server:</td><td><input class="tx02" type="text"
25:          name="server" id="server"></td></tr>
26:    <tr><td><input class="butSt" type="submit" value="Submit"></td>
27:     <td><input class="butSt" type="reset" value="Reset"></td></tr>
28:    </table>
29:    </form>
30:   </div>
31:   </body>
32:   </html>
33: mypage
34:

This program is easy to understand. We have used the password field in lines 2223. If an input element with type is identified as password, the user input in this field will be masked as asterisks. The screen shot of this Perl program is given in Fig. 13.37.

Figure 13.37. ex13-26.p1

graphics/13fig37.jpg


Once the Submit button is pressed, another Perl program, ex13-27.pl, is activated. This program will perform the actual connection and check the emails from the server. The code of this program is surprisingly simple and is listed as follows:



Example: ex13-27.pl - Perl Script For ex13-26.pl

 1: #!/usr/bin/perl
 2: print ("Content-type/html\n\n");
 3:
 4: use warnings;
 5:
 6: use Mail::POP3Client;
 7: use CGI qw( :standard );
 8:
 9: my $user_id = param( "username" );
10: my $password = param( "password" );
11: my $server_name = param( "server" );
12:
13: my $my_pop = new Mail::POP3Client( USER => $user_id,
14:    PASSWORD => $password, HOST => $server_name ) or
15:    die("Cannot connect: $!");
16:
17: my $email_count = $my_pop->Count();
18:
19: print << "mypage";
20:  <?xml version="1.0" encoding="iso-88591"?>
21:  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
22:     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
23:  <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
24:  <head><title>Accessing POP3 Using Perl - ex1327.pl</title></head>
25:  <body style="background:#000088">
26:  <div style="font-family:arial;font-size:18pt;color:#ffff00">
27:       $username <br />
28:       You have $email_count messages in your inbox.
29:  </div></body></html>
30: mypage
31:
32: $my_pop->Close();
33:

Three variables, $user_id, $password, and $server_name, as defined in lines 911 are used to store the corresponding input data from the interface. All this information is needed to construct the Perl command



my $my_pop = new Mail::POP3Client( USER => $user_id,
    PASSWORD => $password, HOST => $server_name )

This command opens a connection to the POP3 server via the Mail::POP3Client module and creates a new object variable called $my_pop. The expression USER=>$user_id in Perl is to assign the variable value $user_id to the identifier USER. If the creation of the object $my_pop is successful, you will have a proper connection to the mail server. In this case, a simple member function call to Count such as



$email_count = $my_pop->Count()

will return the number of emails in the inbox. This number is assigned to the variable $email_count and is used in line 28 as returned information to the user. The close function $my_pop->Close()terminates the server connection when finished. A screen shot of this program is given in Fig. 13.38.

Figure 13.38. ex13-27.p1

graphics/13fig38.jpg


    Table of Contents

    Previous Next