in reply to Automatically getting and reading e-mails

This one is old, it may be of interest. It reads my mail and checks what I want to discard. I don't suggest it for use, but again, may be of interest.
#!/usr/bin/perl -w use strict; use warnings; use Mail::SpamAssassin; use Net::IMAP::Simple; use Email::Simple; use constant DEBUG => 1; #BEGIN { open(STDERR,">>/home/myself/stderr.log"); } my $verbose=1; # quiet my @whitelist = qw( monty beth leocharre bankofamerica dyer pappajohns gmail yahoo craigslist bikini schizowave Amanda Ali geiman.com chroma hickey elizabeth dtyh owen dietofcookies pierre cilli westhost fenton jesse thornton charre puritans cherrys cecilia perl bronwyn cpan ); my @blacklisted = qw( lotto yahoo.co.jp lottery yahoo.co.cn ); my $host = 'mail.host.com'; my $user = 'myself@host.com'; my $pass = 'mypasswerd'; my $imap; my $sa = new Mail::SpamAssassin; my $box = 'INBOX'; my $cycle = 30; # how many before commit and restart my $x=1; my $cycle_limit = 10; # how many cycles before end instance of script my $cycle_count = 0; while (cycle()){ print STDERR "imapcleaner, cycle $cycle_count done\n" if DEBUG; $cycle_count++; last if $cycle_count == $cycle_limit; } sub cycle { $imap = Net::IMAP::Simple->new($host) || die("imapcleaner, unable to connect to IMAP: $Net::IMAP::Simple +::errstr"); # Log on if(!$imap->login($user,$pass)){ die("imapcleaner, login failed: " . $imap->errstr); } print STDERR "imapcleaner: connected\n" if DEBUG; my $count = $imap->select($box); unless( $count){ closeup(); return 0; } print STDERR "per cycle limit is $cycle\n" if DEBUG; my $m = 0; while ( $m < $count ){ ### cleaning ... $m++; last if $m == $cycle; my $header = join '', @{$imap->top($m)}; my $e = Email::Simple->new( $header ); my $from = $e->header('From'); print STDERR "sender: $from" if DEBUG; if ($from and is_blacklist($from)){ print STDERR ", blacklisted\n" if DEBUG; $imap->copy($m, 'INBOX.spam'); $imap->delete($m); next; } my $all = $imap->get($m); my $mail = $sa->parse($all); my $status = $sa->check($mail); if ( $status->is_spam ){ print STDERR ", spam" if DEBUG; #$imap->copy($m, 'INBOX.spam'); $imap->delete($m); print STDERR ", deleted.\n" if DEBUG; next; } print STDERR ", not spam" if DEBUG; my $dont_erase =0; if ($from and is_whitelist($from)){ print STDERR ", whitelisted\n" if DEBUG; $imap->copy($m, 'INBOX.whitelisted') or die('cant copy to +whitelisted'); $imap->delete($m); ## on whitelist: ## $from $dont_erase =1; next; } print STDERR "\n" if DEBUG; # everything else $imap->copy($m, 'INBOX.spam'); $imap->delete($m); } closeup(); return 1; } sub closeup { $imap->quit; # this commits changes $imap = undef; print STDERR "imapcleaner: done\n" if DEBUG; } sub is_whitelist { my $text = shift; $text or die; for (@whitelist){ my $term= $_; if ( $text=~/$term/i ){ return $term; } } return; } sub is_blacklist { my $text = shift; $text or die; for (@blacklist){ my $term= $_; if ( $text=~/$term/i ){ return $term; } } return; }

Replies are listed 'Best First'.
Re^2: Automatically getting and reading e-mails
by merrymonk (Hermit) on Aug 27, 2008 at 15:50 UTC
    Thanks for that.
    It may be old but then many good things are.
    ...and as it works it may well help me to start!