#!/opt/perl/bin/perl -w use strict; use Mail::IMAPClient; use Mail::SpamAssassin; use Mail::Audit; use Carp; use Proc::Daemon; #Set initial Values. my $inbox = "INBOX"; #Name of Mailbox to check for Spam in my $junk = "Junk Mail"; #Name of Mailbox to Move Spam To my $user = ""; #IMAP user name my $host = ""; #IMAP host name or IP address my $interval = 300; #Duration in seconds to wait between cycles #Password is not hardcoded and not entered on commandline for security purposes print "Password: "; my $password = <>; chomp $password; #Get rid of Zombie problems $SIG{CHLD} = 'IGNORE'; #Run as Daemon Proc::Daemon::Init; #Initialize SpamAssassin my $spamtest = Mail::SpamAssassin->new(); #loop while (1) { #connect to imap server my $imap = Mail::IMAPClient->new(User=>$user,Password=>$password,Server=>$host,UID=>1, Peek=>1) or die "Incorrect Password"; #Operate on $inbox $imap->select($inbox); #Retrieve list of messages my @messages = $imap->messages; #loop through messages foreach my $msg (@messages) { #get whole message and save in array. my $msgtxt = $imap->message_string($msg); my $hashref = $imap->parse_headers($msg,"Subject"); #check $msgtxt for spam my $status = $spamtest->check_message_text($msgtxt); #print $hashref->{Subject}->[0] . " " . $status->get_hits . "\n"; if ($status->is_spam()) { #move suspected spam to junk box my $yes = $imap->move($junk,$msg); if ($yes) { #If we successfully moved it, then delete it from inbox $imap->delete_message($msg); } } } #Close IMAP session $imap->close; #sleep #exit; sleep $interval; }