I am writing a POP3 Client program in Perl. You connect to a POP3 Server and have a running conversation with the mail server using commands from the RFC 1939 Post Office Protocol. This program can perform 5 options from a menu on your POP3 mail by logging in with the correct POP3 server along with a user name and password that you use to log in to your ISP. The user name and password as well as the server name are all hard-coded into the program and no user input is required. You just have to hard-code your ISP server ($host variable), user name ($username variable) and password ($mypass variable) into the program.
I can't figure out how to get the e-mail messages required in "List Messages" in Menu Option 1 of my program to display a list of the messages displaying ONLY the message number, who the message is from and the subject. I am not displaying the entire text of the message in Option 1.
I am close to getting all of the messages in the mailbox listed with their message number, who the message is from and the subject but it is messing up and giving me ALL the header information of the message even though I only want the message number defined in the program as well as the "From" and "Subject" fields. It also seems to be only displaying the header info for one of the messages which means something is faulty with the "for" loop that I created and it is not looping through the mail message by message properly.
Can someone also tell me for Option 3 where I need to display the header and body of the e-mail based on the message number typed in by the user how I can differentiate between the header and body of each message using regular expressions? I should also point out that I am prohibted from using any of the Net::POP3 Perl modules or POP3Mail Perl modules to complete this so I need to use regular expressions and the IO::Socket Perl module only.
Here is the code that I have for the program up to this point:
Expand|Select|Wrap|Line Numbers
- #!/usr/bin/perl -w
- use strict;
- use IO::Socket qw(EFAULT :crlf);
- my $host = shift || 'pop3.sympatico.ca';
- my $port = shift || '110';
- my $socket = IO::Socket::INET->new(PeerAddr => $host, PeerPort => $port) or die "no sock $!";
- my $choice; # line 10
- my $answer;
- my $username;
- my $mypass;
- my $msgnum;
- my $msgcount = 0;
- $username = "x9xxxx99"; # format masks for user name and password for
- $mypass = "99xxx9x9"; # ISP account
- print $mypass;
- $answer = <$socket>;
- print "$answer\n";
- # send username, pass
- print $socket "user " . $username,CRLF;
- $answer = <$socket>;
- print $socket "pass " . $mypass,CRLF;
- $answer .= <$socket>;
- print "$answer\n";
- #line 30
- system("cls");
- print "================================================= ======\n";
- print "POP3 Mail Client you have " . $msgcount. "messages waiting.\n";
- print "================================================= ======\n";
- while (1) {
- # menu
- print " 1 List Messages\n";
- print " 2 Display body\n";
- print " 3 Display header and body\n";
- print " 4 Write message to a file\n";
- print " 5 Delete message on the server\n";
- print " 6 Quit\n\n";
- print "Enter choice: ";
- chomp ($choice = <STDIN>);
- if ($choice =~ /^1$/) {
- print $socket "STAT",CRLF;
- $answer = <$socket>;
- print "$answer\n\n";
- if ($answer =~ /^\+OK\s(\d{1,}).*/) {
- $msgcount = $1;
- print "You have " . $msgcount . " messages";
- }
- if ($msgcount > 0) {
- for (my $i = 0; $i < $msgcount; $i++) {
- print $socket "STAT",CRLF;
- $answer = <$socket>;
- print "$answer\n\n";
- $msgnum = $i;
- print "Message Number: " . $msgnum;
- print $socket "RETR " . $msgnum,CRLF;
- $answer = <$socket>;
- if ($answer =~ /^\-ERR/) {
- print "ERROR";
- }
- else {
- if ( $answer =~ /^From.*)/ ) {
- print "From: $1\n";
- }
- if ( $answer =~ /^Subject.*)/ ) {
- print "Subject: $2\n";
- }
- }
- print "\n";
- }
- }
- else {
- print "\n\nYou currently have no messages.";
- <STDIN>;
- next;
- }
- next;
- }
- if ($choice =~ /^2$/) {
- print "\nPlease enter message number: ";
- chomp($msgnum = <STDIN>);
- print $socket "RETR ".$msgnum,CRLF;
- $answer = <$socket>;
- if ($answer =~ /^\-ERR/) {
- print "Error: invalid message number";
- <STDIN>;
- next;
- }
- else {
- }
- next;
- }
- if ($choice =~ /^3$/) {
- print "\nPlease enter message number: ";
- chomp($msgnum = <STDIN>);
- print $socket "RETR ".$msgnum,CRLF;
- $answer = <$socket>;
- if ($answer =~ /^\-ERR/) {
- print "Error: invalid message number";
- <STDIN>;
- next;
- }
- else {
- }
- next;
- }
- if ($choice =~ /^4$/) {
- print "\nPlease enter message number: ";
- chomp($msgnum = <STDIN>);
- print $socket "RETR ".$msgnum,CRLF;
- $answer = <$socket>;
- if ($answer =~ /^\-ERR/) {
- print "Error: invalid message number";
- <STDIN>;
- next;
- }
- else {
- }
- next;
- }
- if ($choice =~ /^5$/) {
- print "\nPlease enter message number: ";
- chomp($msgnum = <STDIN>);
- print $socket "RETR ".$msgnum,CRLF;
- $answer = <$socket>;
- if ($answer =~ /^\-ERR/) {
- print "Error: invalid message number";
- <STDIN>;
- next;
- }
- else {
- }
- next;
- }
- if ($choice =~ /^6$/) {
- print $socket "QUIT",CRLF;
- print "exiting\n" and last;
- }
- }
If anyone can give me any assistance on this problem, it would be greatly appreciated. Thanks.