POP3 client, Step 1

myline

As I told you in the previous step, step 0, I'm going to show you how to get the headers from the remote server and summarize them to show the: From and Subject header. A sample output will be something like:

   You got 4 mail(s) in 192.168.1.1
   1  chakma junan  testing 1
   2  chakma junan  test 2
   3  chakma junan  testing 3
   4  chakma junan  testing 4
Ok, let's start from the place where we stopped in our previous step. (point to remember: add the lines that'll be described here after the below line --- you might as well take a look at the whole script)
   print "You got $total_message mail(s) in $popserver\n";

Ok, let's grep those headers

We can get the headers from the remote server with the following line.

   $heads_size = $remoteserver->list();
It will return a reference to an associative array, hash. For clarity we are going to change it back to a normal hash.
   %heads_size = %$heads_size;
Now the hash 'heads_size' has 'message number' as keys and 'message size' as values(in octets). Here, we use 'message number' only to retrieve further details of heads. So, hear goes the code to grep those heads:
   foreach $msg_no (keys %heads_size)
   {
      # $msg_no = message number
      print " $msg_no ";
      $head_array = $remoteserver->top($msg_no);
   }

Ok, let's parse those headers

Now, we get each mails headers in an array. However, $head_array is a reference to an array, so for clarity we return it back again to normal array.

   @head_array = @$head_array;
Now, we parse all the lines in @head_array. At first we read each line in $line and print only the necessary fields, here, 'From:' and 'Subject:'. Here's the code:
   foreach $line (@head_array)
   {
      chop $line;

      # Let's get the From header
      if ($line =~ /^From: /)
      {
         $line =~ s/^From: //;
         print " $line";
      }

      # Let's get the Subject header
      if ($line =~ /^Subject: /)
      {
         $line =~ s/^Subject: //;
         print " $line";
      }
   }

A sample session

Here goes a sample session.

achakma@jchakma:/home/jchakma/MYPERL{117}% ./pop3client2.pl 
POP server[192.168.1.1]: 
username at 192.168.1.1 [jchakma]: 
Enter password for jchakma@192.168.1.1: 
Ok, now trying to get connected to 192.168.1.1 ....
Connected successfully to 192.168.1.1
Now trying to login...
You got 4 mail(s) in 192.168.1.1
 1  chakma junan  testing 1
 2  chakma junan  test 2
 3  chakma junan  testing 3
 4  chakma junan  testing 4
Closing the connection from 192.168.1.1 now...

achakma@jchakma:/home/jchakma/MYPERL{118}% ./pop3client2.pl
POP server[192.168.1.1]: 
username at 192.168.1.1 [jchakma]: 
Enter password for jchakma@192.168.1.1: 
Ok, now trying to get connected to 192.168.1.1 ....
Connected successfully to 192.168.1.1
Now trying to login...
Sorry dude, you don't have any message in 192.168.1.1!
Closing the connection from 192.168.1.1 now...

The next Step 2

In the next step I'm going to show you how to view the body of e-mail messages + how to delete unnecessary mails from the remote mailbox. Have fun! Bye.

So, here goes the new code. Check it out!

myline

this page is maintained by:
jchakma@yahoo.com

Last modified: Thu 26 Oct 2000 02:24:28 PM JST

1