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 junanOk, 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 headersWe 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 headersNow, 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 sessionHere 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 The next Step 2In 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! |
this page is maintained by:
jchakma@yahoo.com