473,382 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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

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, 1084 views)
Apr 12 '06 #1
1 10032
eWish
971 Expert 512MB
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

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

Similar topics

1
by: Dustin D. | last post by:
If I attempt to crop a string using regular expressions and the () operator for grouping, Perl always seems to return the last match. For instance, if I have the following: my $test =...
0
by: RN | last post by:
Hi, I don't know if this is the right place to ask this, but - what's the difference between posix and perl regular expressions? A good example is "aa|bb". Will this match "aa" or "ab" in...
1
by: smsabu2002 | last post by:
Hi, I am facing the build problem while installing the DBD-MySql perl module (ver 2.9008) using both GCC and CC compilers in HP-UX machine. For the Build using GCC, the compiler error is...
5
by: Morten | last post by:
How do I detect if a client socket is no longer connected to the listen tcp socket ? I have tried with (just an example): --------------------- Socket tcpSocket; ...
3
by: James D. Marshall | last post by:
The issue at hand, I believe is my comprehension of using regular expression, specially to assist in replacing the expression with other text. using regular expression (\s*) my understanding is...
3
by: John Nagle | last post by:
Here's a large Perl regular expression, from a Perl address parser in CPAN: use re 'eval'; $Addr_Match{street} = qr/ (?: # special case for addresses like 100 South Street...
0
by: kaps | last post by:
Hi, How do I read data from perl socket. I tried these three methods in bold but nothing worked. use strict; use IO::Socket; use HTTP::Request::Common; use LWP::UserAgent;
1
by: Miroslav Endys | last post by:
I have two apps. Client and Server (both are windows console apps). Im using Client Socket of this definition AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp and Im using async...
2
by: somsub | last post by:
Hi all, Here is my samle code use strict ; use warnings ; use IO::Uncompress::Unzip ; When I compiled this three lines of code in win32 I got error like below.
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.