next up previous contents
Next: Checking for Upness (Echo) Up: Networking with Perl Previous: Using Perl to Send

Receiving Mail (POP)

The flip side to sending mail is, of course, receiving it. This is done using the POP or Post Office Protocol. Since you've already read about the SMTP protocol in detail, We'll skip describing the details of the POP.

The program pop.pl contains a program that will filter your mail. It will display a report of the authors and subject line for any mail that relates to One Word, a mailing list devoted to the great jazz guitarist John Mclaughlin (one-word@ml.ee) . This program will not delete any mail from the server, so you can experiment with confidence.

Note Before trying to run this program, make sure that the POP3Client module (POP3Client.pm) is in the Mail subdirectory of the library directory. You may need to create the Mail subdirectory. See your system administratior if you need help placing the file into the correct directory.

You might need to modify the program for other systems -- other than windows -- On SunOS 5.4+ (Solaris 2), you'll need to change the POP3Client module to use a packing format of 'S n c4 x8' instead of 'S n a4 x8'. Other changes might also be needed.

The program pop.pl operates as follows:

The code for pop.pl is as follows:

#!/usr/bin/perl -w


use Mail::POP3Client;
use strict;


my($i, $from, $subject);




format main::STDOUT_TOP =
    @|||||||||||||||||||||||||||||||||||||||||||||||||  Pg @<
    "Waiting Mail Regarding One-Word",                    $%


    Sender                  Subject
    ----------------------  --------------------------------
.


format main::STDOUT =
    @<<<<<<<<<<<<<<<<<<<<<  @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
           $from,                    $subject
.


my($username)   = 'dave';
my($password)   = 'XXXXXXXX';
my($mailServer) = 'mailhost2.cs.cf.ac.uk';


my($pop) = Mail::POP3Client->new($username, $password, $mailServer);


for ($i = 1; $i <= $pop->Count; $i++) {
    my($one_word) = 0;


    foreach ($pop->Head($i)) {
        $from = $1 if /From:\s(.+)/;


$subject = $1 if /Subject:\s(.+)/;


        if (/Subject: .*One-Word/) {
#           @body = $pop->Body($i);
#           $pop->Delete($i);
            $one_word = 1;
        }
    }


    if ($one_word) {
        write();
    }
}

This program displays:

Waiting Mail Regarding One-Word           Pg 1


    Sender                  Subject
    ----------------------  ---------------------------------
    Massimo                 [One-Word] One Word Tribute CD
    Marco                   [One-Word] Gigs in London
    Andres                  [One-Word] Pages of Fires

When you run this script, you should change $username, $password, and $mailServer and the filter test to whatever is appropriate for your system.

You could combine the filter program with the send mail program (from sendmail.pl) to create an automatic mail-response program. For example, if the subject of a message is "Info," you can automaticallly send a pmilesefined message with information about a given topic. You could also create a program to automatically forward the messages to a covering person while you are on vacation. I'm sure that with a little thought you can come up with a half-dozen ways to make your life easier by automatically handling some of your incoming mail.



 
next up previous contents
Next: Checking for Upness (Echo) Up: Networking with Perl Previous: Using Perl to Send
dave@cs.cf.ac.uk