Perl POP3 collector

This collects multiple pop3 email accounts and sends them to an smtp server. I wrote this as an exchange pop3 collector.

Setup:
1. Set $inserver to your incoming mail server to collect the pop3 email from. If you need to collect from multiple pop3 servers just copy the script and run them separately.
2. Set $outserver to the server that you would like to move the email to. It does not have to be a local server.
3. Set the %accounts array with the pop3 account you would like to collect.
If you are collect multiple pop3 accounts %accounts might look like this.

%accounts = (
            '[email protected]' => {'password' =>  'pop3_pass','sendto' =>  'demo@demo_exchange.local',},
             '[email protected]' => {'password' =>  'demo2_pass','sendto' =>  'demo2@demo_exchange.local',},
            ); 
#Usage: Run this as a scheduled task to collect external pop3 email and move them into exchange.
use Net::POP3;
use Net::SMTP;

#1 = turn debug on || 0 = turn debug off
#to check for errors run the this from the command line. Example:
# perl exchangepop.pl 1>>expop.log 2>>expop.log
$debug=0;

$inserver="mail.gmail.com"; #email server to get email from

$outserver="localhost"; #Exchange Server, this is usually localhost.

#Accounts to Collect e-mail from, Example:
# 'Pop3 user name' => {'password' =>  'Pop3 Password','sendto' =>  'Exchange E-mail Address',},
%accounts = (
           '[email protected]' => {'password' =>  'pop3_pass','sendto' =>  'demo@demo_exchange.local',},
           );

foreach $accnt (keys %accounts)
{
#print "User: $accnt password:", $accounts{$accnt} -> {"password"}, " sendto: ",$accounts{$accnt} -> {"sendto"}," \n";

  $pop = Net::POP3->new($inserver,Timeout => 60, Debug => $debug);
   if ($pop->login($accnt, $accounts{$accnt} -> {"password"}) > 0)
   {
       my $msgnums = $pop->list;
       foreach my $msgnum (keys %$msgnums)
       {
           my $msg = $pop->get($msgnum);
           $smtp = Net::SMTP->new($outserver, Timeout => 60, Debug =>$debug);
           $smtp->mail($accnt);
           $smtp->to($accounts{$accnt} -> {"sendto"});
           $smtp->data();
           $smtp->datasend(@$msg);
           $smtp->dataend();
           $smtp->quit;
           $pop->delete($msgnum);
       }
   }
   $pop->quit;
}
print "done!";

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.