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.
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.
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, :)
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.