1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
# Always be safe use strict; use warnings; # Use the module use Mail::IMAPClient; $imap = Mail::IMAPClient->new( Server => 'mail.server.com:143', User => 'me', Password => 'mypass') # module uses eval, so we use $@ instead of $! or die "IMAP Failure: $@"; foreach my $box qw( HAM SPAM ) { # Which file are the messages going into my $file = "mail/$box"; # Select the mailbox to get messages from $imap->select($box) or die "IMAP Select Error: $!"; # Store each message as an array element my @msgs = $imap->search('ALL') or die "Couldn't get all messages\n"; # Loop over the messages and store in file foreach my $msg (@msgs) { # Pipe msgs through 'formail' so they are stored properly open my $pipe, "| formail >> $file" or die("Formail Open Pipe Error: $!"); # Send msg through file pipe $imap->message_to_file($pipe, $msg); # Close the messgae pipe close $pipe or die("Formail Close Pipe Error: $!"); } # Close the folder $imap->close($box); } # We're all done with IMAP here $imap->logout();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
my $imap = Net::SIMPLE::IMAP->new('imap.myserver.de');
$imap->login(myusername,mypassword);
$imap->select('Inbox');
my @index = $imap->list();
for my $m ( @index ) {
write_to_file ( $imap->get( $m ); ) ;
}
$imap->logout;
1;
__END__