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; }

In reply to Re: Automatically getting and reading e-mails by leocharre
in thread Automatically getting and reading e-mails by merrymonk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.