473,725 Members | 2,118 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

1 New Member
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, 1088 views)
Apr 12 '06 #1
1 10063
eWish
971 Recognized Expert Contributor
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
2894
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 = "a0\nb0\nc0\na1\nb1\nc1\n"; $test =~ s/.*(\w\d\n).*/$1/s; print "$test"; The output would be:
0
2208
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 perl and posix? And for those POSIX experts, what do the following do? is for non-english languages and such?
1
3637
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 produced due to the unknown GCC compiler option "+DAportable". For the Build using CC, the preprocessor error is produced due to the recursive declration of macro "PerlIO" in perlio.h file.
5
45039
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; while(tcpSocket.Connected)
3
3216
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 that this will one or more occurrences to replace all the white space between with a comma. This search ElseIf InStr(1, indivline, "$") Then insert a replace statement that uses the regular expression to find and replace all the white space...
3
1540
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 (?:($Addr_Match{direct})\W+ (?{ $_{street} = $^N }) ($Addr_Match{type})\b (?{ $_{type} = $^N })) | (?:($Addr_Match{direct})\W+ (?{ $_{prefix} = $^N }))?
0
2583
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
1889
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 communication with the Server Socket. I have no opened NetworkStream. In case, I kill my client application (via red cross) the server application can see, that client has been disconnected. In case, I shutdown the client socket and close it on...
2
6388
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
8888
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9401
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9176
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8097
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6011
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3221
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2635
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.