# 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();