Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

SpamAssassin IMAP client for exchange

by anithri (Beadle)
on Sep 04, 2002 at 21:16 UTC ( #195210=CUFP: print w/ replies, xml ) Need Help??

for reasons I don't care to discuss I use Outlook/Exchange as my primary mailer. I have been searcing for ways to use SpamAssassin with Outlook that doesn't screw up the integrity of the email in outlook (stupid MS MIME problems and such). After reading about the POP3 proxy, i twigged on the idea of using IMAP to shuffle message between mailboxes. There is no rewriting of messages with the SpamAssassin messages. This is a working script using perl 5.6.1 on RedHat. YMMV
#!/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 cy +cles #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,S +erver=>$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 inbo +x $imap->delete_message($msg); } } } #Close IMAP session $imap->close; #sleep #exit; sleep $interval; }

Comment on SpamAssassin IMAP client for exchange
Download Code
Re: SpamAssassin IMAP client for exchange
by Anonymous Monk on Oct 28, 2003 at 11:36 UTC
    Hi, anithri. Thanks for this script. It proved to be VERY useful to me and some co-workers. I took the freedom to adapt your script a little bit... Tested under Debian 'Woody' with Perl v5.6.1, IMAPClient v2.1.4, SpamAssassin v2.60 and Exchange 2000. Primary changes include message rewriting, commandline switches and a check whether the mail was checked earlier...
    #!/usr/bin/perl -w ### Modules use strict; use Mail::IMAPClient; use Mail::SpamAssassin; use Proc::Daemon; use Term::ReadKey; use Getopt::Long; ### Variables my ($spamtest, $imap, $status); my ($flag, $password, $msg, $msgtxt); my ($opt_user, $opt_host, $opt_help, $opt_interval); my @messages; ### Initial values my $inbox = "INBOX"; # Name of mailbox to check for SPAM in my $junk = "INBOX/SPAM"; # Name of mailbox to move SPAM to ### Main program print <<_EoT_; IMAP interface to SpamAssassin V1.0 (c)2003 Alexander J. Trentini (thanks to anithri of perlmonks.org) _EoT_ # Check commandline parameters GetOptions("user=s" => \$opt_user, # IMAP user name "host=s" => \$opt_host, # IMAP host name or IP add +res "interval=s" => \$opt_interval, # Check interval "help" => \$opt_help); # Usage text # Help text? if (defined($opt_help)) { print <<_EoT_; Usage: $0 [--user --host --interval] [--help] If no parameters are given on the commandline, the script enter +s interactive mode. Parameters: --help Prints this usage text --user IMAP user name to use --host IMAP host name or IP address to use --interval Check interval Note: The password will ALWAYS be read via interactive mode, du +e to security reasons! _EoT_ exit(1); } # Interactive mode? unless ($opt_host and $opt_user and $opt_interval) { print("Interactive mode.\n\n"); print("Check interval (seconds): "); $opt_interval = <>; chomp($opt_interval); print("IMAP host name or IP address: "); $opt_host = <>; chomp($opt_host); print("IMAP user name: "); $opt_user = <>; chomp($opt_user); } # Get password (always interactive) print "Password for $opt_user: "; ReadMode(2); $password = <>; chomp($password); ReadMode(0); print("\n"); # Interval ok? #TODO if ($opt_interval < 60) { die("Are you crazy? I'm just a little CELERON (60s interval min.)! +\n"); } # Get rid of zombie problems $SIG{CHLD} = 'IGNORE'; # Run as daemon Proc::Daemon::Init; # Initialize SpamAssassin $spamtest = Mail::SpamAssassin->new(); # Loop endlessly while (1) { # Connect to IMAP server $imap = Mail::IMAPClient->new(User => $opt_user, Password => $password, Server => $opt_host, UID => 1, Debug => 0, Peek => 1) or die("Couldn't conn +ect" . " to $opt_host using $opt_user (Incorrect password?).\n"); $imap->select($inbox); # Create $junk folder if not existant unless ($imap->exists($junk)) { $imap->create($junk); } # Omit already checked messages and messages within # the IXOS mail archive (compressed, would result in # garbage output) #TODO @messages = $imap->search('NOT HEADER X-Spam-Checker-Version "Spam +Assassin" NOT SUBJECT "IXOS"'); # Traverse messages foreach $msg (@messages) { # Only check if mail is smaller than 150KB (performance) next if ($imap->size($msg) > 153600); # Get message and pass to SpamAssassin $msgtxt = $imap->message_string($msg); $status = $spamtest->check_message_text($msgtxt); $status->rewrite_mail(); $msgtxt = $status->get_full_message_as_text(); if ($status->is_spam()) { $flag = $imap->append_string($junk, $msgtxt); } else { $flag = $imap->append_string($inbox, $msgtxt); } # If successfully created, delete original from inbox $imap->delete_message($msg) if ($flag); } # Close IMAP session $imap->close(); sleep $opt_interval; }
      Hey -- thanks for this! I hacked up a copy for a co-worker to work with modern Mail::SpamAssassin, require less non-core CPAN modules, track its state by adding a custom header to the processed messages, and generally do less stuff I wouldn't need: http://taint.org/x/2009/reassassinate

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://195210]
Approved by particle
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (11)
As of 2014-07-22 22:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (130 votes), past polls