Processing incoming mail messages 
Author Message
 Processing incoming mail messages

I need to write a Perl script that will be kicked off by cron every
so many hours.  It will need to process any messages in the mail
box for the account its running under.  While there seem to be lots
of modules that allow you to send e-mail messages, what's the
best one for receiving e-mail?

According to the Perl FAQ, it pointed me at MailTools at CPAN,
however, having installed it (and looked mostly at the Mail::Mailer
module) it seems predisposed towards sending, not receiving.

Any suggestions or pointers welcome.  I love pointers, as long as
there's not too many levels of indirection.

Bruce

--
Bruce W. Mohler                619-458-2675 (voice)
SAIC/ITS/Server Support        619-535-7806 (fax)
Sr UNIX system administrator   888-781-5697 (pager)

Of course my password is the same as my pet's name.
My dog's name is Q47pY!3, but I change it every 90 days.



Fri, 20 Apr 2001 03:00:00 GMT  
 Processing incoming mail messages

Quote:
>I need to write a Perl script that will be kicked off by cron every
>so many hours.  It will need to process any messages in the mail
>box for the account its running under.  While there seem to be lots
>of modules that allow you to send e-mail messages, what's the
>best one for receiving e-mail?
>According to the Perl FAQ, it pointed me at MailTools at CPAN,
>however, having installed it (and looked mostly at the Mail::Mailer
>module) it seems predisposed towards sending, not receiving.
>Any suggestions or pointers welcome.  I love pointers, as long as
>there's not too many levels of indirection.

Read my mailbox and print the subject of each message:

  #!/usr/local/bin/perl -w

  use Mail::Util qw(read_mbox);
  use Mail::Internet;

  $file = "/var/mail/clay";



    $mail = Mail::Internet->new($msg);

    $subject = $mail->get('Subject');

    if (defined($subject)) {
      print "Subject: $subject\n";
    }
  }
--
Clay Irving



Fri, 20 Apr 2001 03:00:00 GMT  
 Processing incoming mail messages

Quote:

> Read my mailbox and print the subject of each message:

>   #!/usr/local/bin/perl -w

>   use Mail::Util qw(read_mbox);
>   use Mail::Internet;

>   $file = "/var/mail/clay";



>     $mail = Mail::Internet->new($msg);

>     $subject = $mail->get('Subject');

>     if (defined($subject)) {
>       print "Subject: $subject\n";
>     }
>   }
> --
> Clay Irving


Super!  Thanks, Clay!

Bruce

--
Bruce W. Mohler                619-458-2675 (voice)
SAIC/ITS/Server Support        619-535-7806 (fax)
Sr UNIX system administrator   888-781-5697 (pager)

Of course my password is the same as my pet's name.
My dog's name is Q47pY!3, but I change it every 90 days.



Fri, 20 Apr 2001 03:00:00 GMT  
 Processing incoming mail messages


: I need to write a Perl script that will be kicked off by cron every
: so many hours.  It will need to process any messages in the mail
: box for the account its running under.  While there seem to be lots
: of modules that allow you to send e-mail messages, what's the
: best one for receiving e-mail?

Why not configure your mail server to deliver to a process, e.g.
through .forward with sendmail?  It would be a little simpler that
way.  This is what I use to filter my mail (based on code Tom
sent me a while back):

#! /usr/bin/perl -w

use strict;
use vars qw(
    $Body
    $Home
    $From
    $Subject
);

BEGIN {

Quote:
}

use Mail::Internet;
use Mail::Address;
use Fcntl ':flock';

## aliases for me

    \bgbacon\b
    \bgreg\.bacon\b
);

## list of users from whom we'll always accept mail
## this shouldn't be necessary unless their mail looks like spam


);

## mailing lists I'm subscribed to

    alabama-mobile-general
    amass
    bugtraq
    clpm-moderators
    clpm-policy
    dist-users
    ghcc\.dl
    gnuhoo-editors
    ingest
    insights
    itsl
    lclint
    linux-kernel
    mjd-clpm-moderators
    mjd-clpm-policy
    mjd-clpm-admin
    modperl
    mongers
    net-irc
    oracle
    p5p
    perl-moderation-panel
    perlbug
    porters
    postmaster
    procmail
    ptk
);


    alabama\.mobile\.general
    comp\.lang\.perl\.moderated
);

## list of users from whom we'll never accept mail

## blacklisted sites

    \b153\.35\.149\.\d+\b
    \b205\.245\.18\.\d+\b
    \b206\.175\.231\.\d+\b
    \b1stfamily\.com\b
    \bacella\.net\b
    \bachristmas\.com\b
    \b{*filter*}store\.com\b
    \baei\.ca\b
    \bagis\.net\b
    \banswerme\.com\b
    \baol\.com\b
    \bascella\.net\b
    \batripaway\.com\b
    \bbranthoverassociates\.com\b
    \bbulkemail\.net\b
    \bcafes\.net\b
    \bcanaan\.co\.il\b
    \bcenturyinter\.net\b
    \bcompuserve\.com\b
    \bconcepts2001\.com\b
    \bcorpsite\.com\b
    \bcrushnet\.com\b
    \bctinet\.net\b
    \b{*filter*}promo\.com\b
    \b{*filter*}serverscentral\.com\b
    \b{*filter*}tize-email\.com\b
    \bdespotovic\.com\b
    \bdialsprint\.net\b
    \bexperts-exchange\.com\b
    \bextraincome4u\.net\b
    \bget-more-hits\.com\b
    \bgillnet\.com\b
    \bgoplay\.com\b
    \bgte\.net\b
    \bgwh\.net\b
    \bhibrid\.com\b
    \bhotmail\.com\b
    \bigs\.net\b
    \binfoabc\.com\b
    \bior\.com\b
    \bjobspc\.com\b
    \bjumbo\.com\b
    \bjuno\.com\b
    \blauderdale\.net\b
    \bleipro23\.com\b
    \blistme\.com\b
    \bllv\.com\b
    \blostvegas\.com\b
    \bmail\.com\b
    \bmailcenter1\.net\b
    \bmaile{*filter*}\.com\b
    \bmarket6two\.net\b
    \bmarketingforyou\.com\b
    \bmlsa1\.com\b
    \bmmaildirect\.com\b
    \bmoneymaker\.com\b
    \bmsn\.com\b
    \bnet\.net\b
    \bnetcom\.com\b
    \bnetlink\.co\.uk\b
    \bnever-ending-gallery\.com\b
    \bnevwest\.com\b
    \bowlsnest\.com\b
    \bpipex\.com\b
    \bplanetinternet\.be\b
    \bprivatedances\.com\b
    \bprodigy\.com\b
    \bprodigy\.net\b
    \bpromo\.com\b
    \bprtracker\.com\b
    \bsavoynet\.com\b
    \bsendad\.com\b
    \bsingnet\.com\.sg\b
    \bsol\.no\b
    \bspica\.net\b
    \bsprintmail\.com\b
    \bsprynet\.com\b
    \bsuccess2one\.net\b
    \bsuccess123\.net\b
    \bt3-com\.com\b
    \btamcotec\.com\b
    \btelephonemarketing\.com\b
    \btimebill\.net\b
    \btnlb\.com\b
    \bultramax\.net\b
    \buntoldstory\.org\b
    \busa\.net\b
    \bwave\.net\b
    \bwe-work-for-you\.com\b
    \bxxxlink\.com\b
    \byahoo\.com\b
    \bybecker\.net\b
);

## subs
sub flog {
    my $msg = shift;
    my $log = "$Home/FilterLog";
    my($s,$min,$h,$d,$mon,$y);

    $msg =~ s/\s+$//;

    ($s,$min,$h,$d,$mon,$y) = localtime $^T;
    $y += 1900;

    open LOG, ">>$log" or die "Failed open $log: $!\n";
    flock LOG, LOCK_EX;
    seek LOG, 0, 2;

    printf LOG "%02d/%02d/%02d %d:%02d:%02d [$$]: $msg\n",
        $y, $mon+1, $d, $h, $min, $s;

    close LOG;  # ...and release the lock

Quote:
}

sub rcvstore {
    my $msg = shift;
    my $folder = shift;
    my $rcvstore = "/usr/lib/nmh/rcvstore";

    unless (-x $rcvstore) {
        die "$rcvstore is not executable!\n";
    }

    open RCVST, "| $rcvstore +$folder -create" or die "Failed fork: $!\n"
        or die "Failed open |$rcvstore: $!\n";
    $msg->print(\*RCVST);
    close RCVST or flog "close $rcvstore: $!";

Quote:
}

sub reject {
    my $msg = shift;
    $msg =~ s/\s+$//;

    print STDERR "Message rejected: $msg\n";

    exit 69;

Quote:
}

sub check_for_duplicate {
    my $msg = shift;

    my $sum = unpack "%31C*", $Body;
    my $len = length $Body;

    my $msgid = $msg->head->get('Message-ID') || '';
    chomp $msgid;

    unless ($msgid) {
        flog "Rejecting message: no Message-ID";
        rcvstore $msg, "invalid";
        reject "No Message-ID: header";
    }

    my %msgs;
    my $db = "$Home/.dupmsgdb";
    if (dbmopen %msgs, $db, 0644) {
        if ($msgs{$msgid}) {
            flog "duplicate message id $msgid discarded";
            exit;
        }
        else {
            $msgs{$msgid} = 1;
        }

        dbmclose %msgs;
    }
    else {
        flog "Failed dbmopen $db: $!";
    }

Quote:
}

sub initialize {
    my $msg = shift;


    my $tmp = $msg->head->get('Reply-To')
           || $msg->head->get('From')
           || $msg->head->get('Apparently-From')
           || $msg->head->get('Sender')
           || '';

    unless ($tmp) {
        flog "Rejecting message: no sender";
        rcvstore $msg, "invalid";
        reject "No From: header";
    }

    ($tmp) = Mail::Address->parse($tmp);

    ($From) = $tmp->address;
    study $From;

    $Subject = $msg->head->get('Subject') || '<no subject>';
    chomp $Subject;

Quote:
}

sub check_for_banned_sender {
    my $msg = shift;


        if ($From =~ /$user/i) {
            flog "sender $From is banned by $user";
            rcvstore $msg, "banned";
            reject "Unauthorized delivery";
        }
    }

Quote:
}

sub accept_message {
    my $msg = shift;


        if (eval { $From =~ /$user/i }) {
            return 1;
        }
    }



            return 1;
        }
    }

    return 0;

Quote:
}

sub recipients {
    my $msg = shift;

    my $head = $msg->head || return;


              map { Mail::Address->parse($_) }
                ( $head->get('To'),
                  $head->get('Apparently-To'),
                  $head->get('Sender'),
                  $head->get('Resent-To'),
                  $head->get('Cc') );


         map { split /\s*,+\s*/ }
         $msg->head->get('Newsgroups');


Quote:
}

sub looks_like_spam {
    my $msg = shift;









            flog "Spamchecker caught $site";
            return 1;
        }



        }
    }

Quote:
}

sub admin_related {
    local $_;


        4Dgifts           games             lp                setup
        MAILER-DAEMON     ghcc              man               subscribe-errors
        adm               ghrc              mexdemos          sys
        amass             ghrc-sau          msuingest         sysadm
        archive           ghrcstaff         nuucp             sysadm
        auditor           ghrcuser          opus              sysadmin
        bin               ghrcuso           postmaster        tour
        daemon            guest             powerdown         tutor
        dbadmin           ingest            rfindd            uah
        demos             ingeststats       rje               uucp
        diag              larcerrors        root              uucpadm
        fido-errors       larcnotice        rootcsh           uucpws
        gaierrors         liserrors         rootsh            wsiingest
        gaiingest         lisingest         saspu             webmaster
        www               Whats\.New        ActivityNotice
        ghcc-sysmgrs
    );

    my $regex = '\\b' .

                '\\b';


        if (/$regex/iox) {
            return 1;
        }
    }

    return 0;

Quote:
}

sub for_me {
    my $msg = shift;



    }


    return 0;

Quote:
}

sub junk {
    my $msg = shift;

    my $prec = $msg->head->get('Precendence') || '';
    chomp $prec;

    if ($prec =~ /junk|bulk/i) {
        return 1;
    }


    #    return 1;
    #}

    return 0;

Quote:
}

sub send_page {
    require IO::Socket;
    IO::Socket->import;

    my $sock = new IO::Socket::INET PeerAddr => 'openview-1.adtran.com',
                                    PeerPort => 7777,
                                    Proto    => 'tcp',
                                    Timeout  => 10;

    unless (defined $sock) {
        flog "Failed to create socket: $!";
        return;
    }

    my $copy = $Body;
    $copy =~ s/^\s+//;
    $copy =~ s/\s+/ /g;

    my $str = "GREG BACON /" . substr("$From - $copy", 0,
...

read more »



Fri, 20 Apr 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Processing incoming mail messages

2. Processing incoming mail

3. Processing Incoming Mail?

4. Processing mail folders one message at a time

5. Script to filter incoming mail?

6. Filter to sort incoming mail?

7. Parsing incoming mails to launch a script

8. Intercepting Incoming Mail

9. How to detect incoming mails in perl?

10. Filter incoming mail for individual users?

11. Filtering my incoming mail

12. Q: Filter for incoming mails

 

 
Powered by phpBB® Forum Software