tags:

views:

78

answers:

3

Hello,

I learning Perl and I want to create a simple application that gets all my emails and save they to a file, but how I can do this? Thanks.

+4  A: 

POP3 example in Perl

Josh Stodola
Very nice tutorial!
Nathan Campos
+3  A: 

The answer to almost any such question is "Find the right module on CPAN Search".

Most modules come with examples in the documentation and tests.

Good luck, :)

brian d foy
Thanks very much!
Nathan Campos
While I agree with the 'answer' is it really an answer? He should have searched CPAN sure but shouldn't we, to make SO complete and popper, atleast provide a working set of modules he could employ? I just thought it was a rule of SO that you don't link to search engines? I could be wrong though.
Robert Massaioli
I read it as a more polite "RTFM". Although RTFM is obvious, not everyone knows about CPAN (or just how expansive it is).
Ether
Well, the set of modules he needs largely depends on everything he didn't specify.
brian d foy
The asker picks the answer. Don't argue about it. That's just childish.
Josh Stodola
+7  A: 

I used to use the following script to filter SpamAssassin flagged email before switching ISPs:

#!/usr/bin/perl

use strict;
use warnings;

$| = 1;

use constant SEVERITY => 5;

use Mail::POP3Client;
use Term::ReadKey;

my $user = shift;

my $pop = Mail::POP3Client->new(
    HOST => '127.0.0.1', 
    PORT => 9999
);

my $pass = prompt_password();
print "\n";

$pop->User($user);
$pop->Pass($pass);
$pop->Connect or die $pop->Message;

my $count = $pop->Count;

$count >= 0 or die "Failed to get message count.\n";
$count >  0 or die "No messages in mailbox.\n";

my @to_delete;

print "Scanning messages:  ";

my $to_delete = 0;
for my $msg_num (1 .. $count) {
    my @headers = $pop->Head($msg_num);

    for my $h (@headers) {
        if($h =~ /^X-Spam-Level: (\*+)/) {
            if(SEVERITY <= length $1) {
                $to_delete += 1;
                $pop->Delete($msg_num);
                print "\b*>";
            } else {
                print "\b->";
            }
        }
    }
}

print "\b ... done\n";

use Lingua::EN::Inflect qw( PL );

if( $to_delete ) {
    printf "%d %s will be deleted. Commit: [Y/N]?\n",
        $to_delete, PL('message', $to_delete);
    $pop->Reset unless yes();
}

$pop->Close;

print "OK\n";

sub yes {
    while(my $r = <STDIN>) {
        $r = lc substr $r, 0, 1;
        return 1 if $r eq 'y';
        next unless $r eq 'n';
        last;
    }
    0;
}

sub prompt_password {
    print 'Password: ';
    ReadMode 2;
    my $pass = ReadLine 0;
    ReadMode 0;
    chomp $pass;
    return $pass;
}

It is trivial to change this so it saves messages. See Mail::POP3Client.

Sinan Ünür
Thanks very much, i going to study this code!
Nathan Campos