473,396 Members | 1,982 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,396 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 10036
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: 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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.