By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,743 Members | 1,018 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,743 IT Pros & Developers. It's quick & easy.

POP3 Mail Client in PERL using IO::Socket module only and regular expressions

P: 1
Hi everyone,

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
  1. #!/usr/bin/perl -w
  2. use strict;
  3. use IO::Socket qw(EFAULT :crlf);
  4. my $host = shift || 'pop3.sympatico.ca';
  5. my $port = shift || '110';
  6. my $socket = IO::Socket::INET->new(PeerAddr => $host, PeerPort => $port) or die "no sock $!";
  7. my $choice; # line 10
  8. my $answer;
  9. my $username;
  10. my $mypass;
  11. my $msgnum;
  12. my $msgcount = 0;
  13. $username = "x9xxxx99"; # format masks for user name and password for 
  14. $mypass = "99xxx9x9"; # ISP account
  15. print $mypass;
  16. $answer = <$socket>;
  17. print "$answer\n";
  18. # send username, pass
  19. print $socket "user " . $username,CRLF;
  20. $answer = <$socket>;
  21. print $socket "pass " . $mypass,CRLF;
  22. $answer .= <$socket>;
  23. print "$answer\n";
  24. #line 30
  25. system("cls");
  26. print "================================================= ======\n";
  27.  
  28. print "POP3 Mail Client you have " . $msgcount. "messages waiting.\n";
  29.  
  30. print "================================================= ======\n";
  31.  
  32. while (1) {
  33. # menu
  34. print " 1 List Messages\n";
  35. print " 2 Display body\n";
  36. print " 3 Display header and body\n";
  37. print " 4 Write message to a file\n";
  38. print " 5 Delete message on the server\n";
  39. print " 6 Quit\n\n";
  40. print "Enter choice: ";
  41. chomp ($choice = <STDIN>);
  42. if ($choice =~ /^1$/) {
  43. print $socket "STAT",CRLF;
  44. $answer = <$socket>;
  45. print "$answer\n\n";
  46. if ($answer =~ /^\+OK\s(\d{1,}).*/) {
  47. $msgcount = $1;
  48. print "You have " . $msgcount . " messages";
  49. if ($msgcount > 0) {
  50. for (my $i = 0; $i < $msgcount; $i++) {
  51. print $socket "STAT",CRLF;
  52. $answer = <$socket>;
  53. print "$answer\n\n";
  54. $msgnum = $i;
  55. print "Message Number: " . $msgnum;
  56. print $socket "RETR " . $msgnum,CRLF;
  57. $answer = <$socket>;
  58. if ($answer =~ /^\-ERR/) {
  59. print "ERROR";
  60. }
  61. else {
  62. if ( $answer =~ /^From.*)/ ) { 
  63. print "From: $1\n";
  64. }
  65. if ( $answer =~ /^Subject.*)/ ) {
  66. print "Subject: $2\n";
  67. }
  68. }
  69. print "\n";
  70. }
  71. else {
  72. print "\n\nYou currently have no messages.";
  73. <STDIN>;
  74. next;
  75. next;
  76. }
  77. if ($choice =~ /^2$/) { 
  78. print "\nPlease enter message number: ";
  79. chomp($msgnum = <STDIN>); 
  80. print $socket "RETR ".$msgnum,CRLF;
  81. $answer = <$socket>;
  82. if ($answer =~ /^\-ERR/) {
  83. print "Error: invalid message number";
  84. <STDIN>;
  85. next;
  86. }
  87. else {
  88. next;
  89. if ($choice =~ /^3$/) {
  90. print "\nPlease enter message number: ";
  91. chomp($msgnum = <STDIN>); 
  92. print $socket "RETR ".$msgnum,CRLF;
  93. $answer = <$socket>;
  94. if ($answer =~ /^\-ERR/) {
  95. print "Error: invalid message number";
  96. <STDIN>;
  97. next;
  98. }
  99. else {
  100. next;
  101. if ($choice =~ /^4$/) {
  102. print "\nPlease enter message number: ";
  103. chomp($msgnum = <STDIN>); 
  104. print $socket "RETR ".$msgnum,CRLF;
  105. $answer = <$socket>;
  106. if ($answer =~ /^\-ERR/) {
  107. print "Error: invalid message number";
  108. <STDIN>;
  109. next;
  110. }
  111. else {
  112. next;
  113. if ($choice =~ /^5$/) {
  114. print "\nPlease enter message number: ";
  115. chomp($msgnum = <STDIN>); 
  116. print $socket "RETR ".$msgnum,CRLF;
  117. $answer = <$socket>;
  118. if ($answer =~ /^\-ERR/) {
  119. print "Error: invalid message number";
  120. <STDIN>;
  121. next;
  122. }
  123. else {
  124. next;
  125. if ($choice =~ /^6$/) {
  126. print $socket "QUIT",CRLF;
  127. print "exiting\n" and last;
  128. }
  129. }
If this is too confusing to read (my first time posting on here which may be causing the code to look funny), I have also attached the code in a pop3.txt document that will need to be renamed to a .pl extension in order to execute it as a PERL program.

If anyone can give me any assistance on this problem, it would be greatly appreciated. Thanks.
Attached Files
File Type: txt pop3.txt (3.9 KB, 893 views)
Apr 12 '06 #1
Share this Question
Share on Google+
1 Reply


eWish
Expert 100+
P: 971
For the less advanced user there would be a module to assist in doing this rather than writting your own.

--Kevin
Nov 18 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.