Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Getmail perl script

by xf86 (Initiate)
on Apr 07, 2001 at 00:03 UTC ( [id://70578]=perlquestion: print w/replies, xml ) Need Help??

xf86 has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to write a a perl program to login to a pop3 server and do a RETR for my messages and return each individual message into a file. Example..Message 1 would go in the file message1, message 2 would go in the file message2,etc...What I have working so far is to authenticate, get the number of messages I have and that's it. I have no clue how to return the messages to me properly...here is my code..
#!/usr/bin/perl -w use IO::Socket; use strict; my $EOL = "\015\012"; my $BLANK = $EOL x 1; my $username = "brice"; my $password = "mypassword"; my $count = 0; my @mailstat; my $remote = IO::Socket::INET->new( Proto => "tcp", PeerAddr => "localhost", PeerPort => "pop3(110)", ) or die "cannot connect to daytime port at localhost"; $remote->autoflush(1); print $remote "USER $username" . $BLANK; print $remote "PASS $password" . $BLANK; print $remote "QUIT" . $BLANK; while ( <$remote> ) { if (/lock busy/) { die "Session already in use!\n"; } if (/OK $username has/) { @mailstat = split(/ /); } } if ($mailstat[3] eq "0") { print "You have no messages.\n"; }
The thing that I have figured out as a start to getting the messages would be...
for ($count = $mailstat[3]; $count > 0; $count--) { print $remote "RETR $count" . $BLANK; #then do something with the data here. ???? }
I would appriciate any help. Thanks to all the monks. Ben Rice

Replies are listed 'Best First'.
Re: Getmail perl script
by TheoPetersen (Priest) on Apr 07, 2001 at 00:21 UTC
    I replaced your code with this loop cobbled from the example in Mail::POP3Client:
    use Mail::POP3Client; $pop = new Mail::POP3Client( USER => $username, PASSWORD => $password, HOST => "localhost" ); for( $i = 1; $i <= $pop->Count(); $i++ ) { open(MESSAGE, ">message$i") or die "Can't open message file"; print MESSAGE, $pop->Body; close MESSAGE; } $pop->Close();
    I haven't tested it, but I bet you can get this working sooner than your version.

    ..Theo

Re: Getmail perl script
by suaveant (Parson) on Apr 07, 2001 at 00:09 UTC
    Out of curiosity, why aren't you just using an existing module?

    Update: Out of further curiosity, why did I get voted down for nicely asking a simple question?
                    - Ant

      Probably because you didn't suggest a module. If you asked me 'how do I buy a car?' would 'shouldn't you be looking for a car dealer?' be an appropriate answer? Don't answer a question with a question.

      Celebrate Intellectual Diversity

        Search.cpan.org is a great place to look for modules... I thought that since you had done so much you had probably already looked into modules for pop3 and found them lacking.
                        - Ant
Re: Getmail perl script
by extremely (Priest) on Apr 07, 2001 at 00:45 UTC
    I'm with sauveant on this one. Why not use Mail::POPClient or Net::POP3? And you surely should look at one of them for clues on handling POP3 protocol stuff.

    A POP3 box is supposed to return a "UIDL" Unique IDentifier for each message. You could use that as part of the filename. You really want Net::POP3.

    --
    $you = new YOU;
    honk() if $you->love(perl)

      I agree with both extremely and sauveant. Use a module. It'll save you alot of time and has the benefit of (probably) a good bit of testing.

      What I want to know is thats the difference between them. I looked over the docs and they both look roughly the same (more stuff in Mail::POPClient). I'm using Mail::POPClient in my "yet another mail client" (trust me it's different :)), but am interested to hear of any pros/cons of each.

      xf86, To handle attachements and the like, look into MIME-tools

        I'm not sure there are any hard and fast rules, but for both the POP and IMAP sets (which have Net:: and Mail:: modules) I'd use the Mail:: versions for a client and the Net:: versions for a biffer or other status check. The Net:: versions are light(er) weight and have the methods for talking to the server; good candidates for subclassing if you wanted to do more yourself.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://70578]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-23 22:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found