Here I'm going to demonstrate a simple POP3 client in perl based on Net::POP3 module in step by step. This is step 0. Before you start testing the client please make sure that you have the following items installed (beside perl5.005_03, I'm writing this on this version): I guess you'll get this from http://www.perl.com/CPAN-local/modules/01modules.index.html.
First mandatory partWell, first thing is firs! I know you are not a novice perl programmer, but let me put over here the first few lines and let me remind you that you have to change the first line according to the place where 'perl' interpreter resides! 'use Net::POP3' says that it's going to use the POP3 module from the Net directory! #!/usr/bin/perl use Net::POP3;Before you proceed further make sure that you have the POP3 module installed properly. A single line testing can be something like: % perl -e "use Net::POP3" # -e command switchIf you get no error messages then you can proceed. Otherwise you might get something like: Can't locate Net/POP3.pm in @INC (@INC contains: /usr/lib/perl5/5.00503/i386-linux /usr/lib/perl5/5.00503 /usr/lib/perl5/site_perl/5.005/i386-linux /usr/lib/perl5/site_perl/5.005 .) at -e line 1. BEGIN failed--compilation aborted at -e line 1.It means that you don't have have the POP3 module installed in appropriate places(or you haven't installed them) --- do it before you can proceed. Ok, What's the POP server's IP add?Here we are going to take POP server's IP address. We provide a default IP add when no input is registered from the user. print "POP server[192.168.1.1]: "; chop($popserver = User name at the POP serverHere we give the user opportunity to provide us with a username at the POP server. If the user doesn't provide one, i.e. just presses enter, we try with the current username! $default_username = getpwuid $<; print "username at $popserver [$default_username]: "; chop($username = Let's take the password with the echo offHere we take the password for the username at the POP server with turning the echo off. Turning echo off means that whatever the user types on the console won't be echoed back on the console. Quite familiar with this kind of things, huh! system "stty -echo"; print "Enter password for $username\@$popserver: "; chop($password = Connect to POP server, login and count mailsOk, let's open a connection and log. Upon failure let's show some 'decent' messages. print "Ok, now trying to get connected to "; print "$popserver ....\n"; # Let's call the constructor with one value! $remoteserver = new Net::POP3($popserver); if (! $remoteserver) { # Let's be gentle! Not required! print "Sorry, dude. Couldn't connect "; print "to $popserver...\n"; print "Check whether your popserver($popserver) "; print " is alive or not.\n"; print "\n"; print " ping $popserver \n"; print "\n"; print "will be a nice try.\n"; exit 1; } else { print "Connected successfully to $popserver\n"; } # Now let's see how many mails are there in # $remoteserver print "Now trying to login...\n"; $total_message = $remoteserver->login($username, $password); if ($total_message eq "") { print "Sorry Bro! Login failed! "; print "Try another session\n"; } elsif ($total_message > 0) { print "You got $total_message mail(s) "; print "in $popserver\n"; } else { print "Sorry dude, you don't have any message "; print "in $popserver!\n"; } Ok, let's close connections by the bookOk, let's close the connections by the book. So, where do we count mails? When you login to the POP server with the following code: $total_message = $remoteserver->login($username, $password);The server returns the number of messages in the mailbox. A sample sessionHere goes few sample session. Test it yourself. user@localhost:/home/user/MYPERL{115}% ./pop3client1.pl POP server[192.168.1.1]: 192.168.1.1 username at 192.168.1.1 [user]: chakma Enter password for chakma@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... user@localhost:/home/user/MYPERL{116}% ./pop3client1.pl POP server[192.168.1.1]: username at 192.168.1.1 [user]: chakma Enter password for chakma@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 1 mail(s) in 192.168.1.1 Closing the connection from 192.168.1.1 now... user@localhost:/home/user/MYPERL{117}% The next Step 1In the next step I'm going to show you how to get all the headers from the POP servers mailbox. So, give a visit after a week! Have fun! |
this page is maintained by:
jchakma@yahoo.com