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

Discussion in 'Perl Misc' started by bobano, Apr 11, 2006.

  1. bobano

    bobano Guest

    Hi everyone

    I am writing a POP3 Client program in Perl. You connect to a POP
    Server and have a running conversation with the mail server usin
    commands from the RFC 1939 Post Office Protocol. This program ca
    perform 5 options from a menu on your POP3 mail by logging in with th
    correct POP3 server along with a user name and password that you use t
    log in to your ISP. The user name and password as well as the serve
    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 nam
    ($username variable) and password ($mypass variable) into the program

    I can't figure out how to get the e-mail messages required in "Lis
    Messages" in Menu Option 1 of my program to display a list of th
    messages displaying ONLY the message number, who the message is fro
    and the subject. I am not displaying the entire text of the message i
    Option 1.

    I am close to getting all of the messages in the mailbox listed wit
    their message number, who the message is from and the subject but it i
    messing up and giving me ALL the header information of the message eve
    though I only want the message number defined in the program as well a
    the \"From\" and \"Subject\" fields. It also seems to be only displayin
    the header info for one of the messages which means something is fault
    with the \"for\" loop that I created and it is not looping through th
    mail message by message properly.

    Can someone also tell me for Option 3 where I need to display th
    header and body of the e-mail based on the message number typed in b
    the user how I can differentiate between the header and body of eac
    message using regular expressions? I should also point out that I a
    prohibted from using any of the Net::pOP3 Perl modules or POP3Mail Per
    modules to complete this so I need to use regular expressions and th
    IO::Socket Perl module only.

    Here is the code that I have for the program up to this point

    #!/usr/bin/perl -
    use strict
    use IO::Socket qw:)DEFAULT :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 1
    my $answer
    my $username
    my $mypass
    my $msgnum
    my $msgcount = 0
    $username = "x9xxxx99"; # format masks for user name and password for
    $mypass = "99xxx9x9"; # ISP accoun
    print $mypass
    $answer = <$socket>
    print "$answer\n"
    # send username, pas
    print $socket "user " . $username,CRLF
    $answer = <$socket>
    print $socket "pass " . $mypass,CRLF
    $answer .= <$socket>
    print "$answer\n"
    #line 3
    system("cls")
    print "=======================================================\n"

    print "POP3 Mail Client you have " . $msgcount. "messages waiting.\n"

    print "=======================================================\n"

    while (1)
    # men
    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 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 .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.


    +-------------------------------------------------------------------+
    |Filename: pop3.txt |
    |Download: http://helpfeeds.com/attachment.php?attachmentid=480 |
    +-------------------------------------------------------------------+

    --
    bobano
    ------------------------------------------------------------------------
    bobano's Profile: http://helpfeeds.com/member.php?userid=1294
    View this thread: http://helpfeeds.com/showthread.php?t=290445
    bobano, Apr 11, 2006
    #1
    1. Advertising

  2. bobano <> wrote in news:bobano.265149
    @helpfeeds.com:

    > I am writing a POP3 Client program in Perl.


    ....

    > 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


    I would have used

    http://search.cpan.org/~sdowd/Mail-POP3Client-2.17/POP3Client.pm

    --
    A. Sinan Unur <>
    (remove .invalid and reverse each component for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Apr 12, 2006
    #2
    1. Advertising

  3. bobano

    Dr.Ruud Guest

    Dr.Ruud, Apr 12, 2006
    #3
  4. bobano

    bobano Guest

    I a, prohibited from using Mail::pOP3 Client module on this particula
    project but I did try it and the program won't compile. It says "Can'
    locate Mail/POP3Client.pm in @INC (@INC contains C:/Perl/li
    C:/Perl/site/lib.) at POP3Mail.pl line 4.

    Is there some extra header file I need to use the POP3Client module?
    keep getting that error when trying to run the example from the sit
    that some of you posted for the "Mail::pOP3Client" module

    --
    boban
    -----------------------------------------------------------------------
    bobano's Profile: http://helpfeeds.com/member.php?userid=129
    View this thread: http://helpfeeds.com/showthread.php?t=29044
    bobano, Apr 12, 2006
    #4
  5. bobano

    Bart Lateur Guest

    bobano wrote:

    >I a, prohibited from using Mail::pOP3 Client module on this particular
    >project but I did try it and the program won't compile. It says "Can't
    >locate Mail/POP3Client.pm in @INC (@INC contains C:/Perl/lib
    >C:/Perl/site/lib.) at POP3Mail.pl line 4."


    It would certainly help if you first installed the Mail::pOP3Client
    library. You can get it from CPAN.

    <http://search.cpan.org/search?module=Mail::pOP3Client>

    --
    Bart.
    Bart Lateur, Apr 13, 2006
    #5
  6. bobano <> wrote:
    >
    > I a, prohibited from using Mail::pOP3 Client module on this particular
    > project but I did try it and the program won't compile. It says "Can't
    > locate Mail/POP3Client.pm in @INC (@INC contains C:/Perl/lib
    > C:/Perl/site/lib.) at POP3Mail.pl line 4."



    It sounds like the module is not installed on your system.

    Is the module installed on your system?

    If not, then see:

    perldoc -q module

    How do I install a module from CPAN?

    If so, then see:

    perldoc -q module

    How do I keep my own module/library directory?

    for help on how to let perl find the module.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Apr 13, 2006
    #6
  7. bobano

    robic0 Guest

    On Thu, 13 Apr 2006 16:44:12 -0500, Tad McClellan <> wrote:

    >bobano <> wrote:
    >>
    >> I a, prohibited from using Mail::pOP3 Client module on this particular
    >> project but I did try it and the program won't compile. It says "Can't
    >> locate Mail/POP3Client.pm in @INC (@INC contains C:/Perl/lib
    >> C:/Perl/site/lib.) at POP3Mail.pl line 4."

    >
    >

    I'm new to this thread and just reading the outskirts.
    Is the socket input subject to regexp?
    [snip]
    robic0, Apr 14, 2006
    #7
  8. bobano

    TTK Ciar Guest

    Hello!

    One of my first perl projects happens to have been a POP3 client.
    So the code is kind of messy, but it works. The source is here:

    http://aux.ciar.org/ttk/public/popper

    It uses the Socket module rather than IO::Socket, but reading the
    context of your situation I'm not sure if that would matter. Even
    if it does, it should be readily convertible. Just hoping it helps.

    Good luck!
    -- TTK
    TTK Ciar, Apr 14, 2006
    #8
  9. bobano

    bobano Guest

    Hi everyone

    I am using the Net::pOP3 module and I have included a statement in thi
    program "$pop3->top($msgnum)". According to the module documentation
    the top($msgnum) function has a return value that is supposed t
    display the header of all the e-mail messages in the POP3 account.
    tested it and it does work but it displays the ENTIRE heade
    information and I do not wish to have that. How can I parse that retur
    value out so that it only displays the "From" and "Subject" fields fro
    the e-mail headers instead of the entire header which includes lots o
    useless information that I don't want to display

    --
    boban
    -----------------------------------------------------------------------
    bobano's Profile: http://helpfeeds.com/member.php?userid=129
    View this thread: http://helpfeeds.com/showthread.php?t=29044
    bobano, Apr 16, 2006
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jay Douglas
    Replies:
    0
    Views:
    600
    Jay Douglas
    Aug 15, 2003
  2. Kevin F
    Replies:
    1
    Views:
    536
    Alex Martelli
    Mar 19, 2006
  3. SPJ
    Replies:
    0
    Views:
    336
  4. SteveC
    Replies:
    2
    Views:
    445
    SteveC
    Jul 30, 2008
  5. Noman Shapiro
    Replies:
    0
    Views:
    232
    Noman Shapiro
    Jul 17, 2013
Loading...

Share This Page