[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: how execute command on every mail received



On 15/06/10 05:33 AM, paragasu wrote:
> Hi all,
>
> I wonder if there is a simple SMTP deamon.
> This deamon will execute a specific command on every email received.
>
> I have a PHP program that will parse the email and
> send SMS to specific mobile phone number thereafter.
>
> please advice.
>
> Thanks
>

Joel Roth wrote:

> I use a rudimentary perl script based on Net::POP3 and Email::Filter
> to download and sort my messages from a POP3 mailbox. 
> 
> Similar code could also be placed in a forward file, i.e. launched
> the same was as procmail, for every mail delivered.
> 
> Perl can easily call a PHP script to finish handling
> the mail. Or you may be able to rewrite that part of 
> your PHP script in Perl.
> 
> Will post my script if you are interested.

On Wed, Jun 16, 2010 at 09:43:57PM +0800, paragasu wrote:
> Hi Joel,
> 
> I have been searching for some time. And i decided to use postfix.
> It is great if you can show me your code. If possible your configuration
> too.

This script gets messages from a POP3 mailbox, similar to
fetchmail. It requires debian packages 

    libemail-filter-perl
    perl-modules

A similar approach could be used for a script triggered
from a ~/forward file.

Missing is a cache of Message-Ids, to avoid multiply downloading
the same message.

No apologies for style.  

Regards,

Joel

------cut------
#!/usr/bin/env /perl
use Net::POP3;
use Email::Filter;
use File::Basename;
use strict;
use vars qw($debug $script $log $pop $msg $mail $maildir %personal );

$script = basename $0;
$log = "$ENV{HOME}/$script.log";
$maildir = "$ENV{HOME}/Mail";
%personal = map {lc $_ => 1 } split " ", qx(cat $ENV{HOME}/.whitelist_personal);
my @other_instances = grep{ $_ != $$} split " ", qx(pgrep $script);
die "another instance of this script is already running" if @other_instances;


my @accounts = (
    {
     USER => "grabble",
     AUTH_MODE => "PASS",
     PASSWORD => "mypass",
     HOST => "some.popserver.com"
    },
    # More accounts here.
);

$| = 1;
for (@accounts) {
    print "\nConnecting to $$_{HOST}...";
    $pop = Net::POP3->new($$_{HOST}, Timeout => 60);

    my $i;
    if ($pop->login($$_{USER}, $$_{PASSWORD}) > 0) {
        my $msgnums = $pop->list; # hashref of msgnum => size
        my @nums = keys %$msgnums;
        print scalar @nums, " messages found.\nDownloading";
         foreach my $msgnum (keys %$msgnums) {
            $msg = $pop->get($msgnum); # $msg: array reference
            print ".";
            $debug and print @$msg;
            filter($msg);
            $pop->delete($msgnum);
            $i++;

        }
    }
    $pop->quit;
}

sub filter {
    no warnings qw(uninitialized);
    my $msg = shift;
    my $text = join '', @$msg;
    $debug and print $text;
    $mail = Email::Filter->new(data => $text, emergency => "$ENV{HOME}/mail_dump"); 
    $mail->exit(0);

    to_folder("debian-mentors"), return 
        if ($mail->to =~ /debian-mentor/i
            or  $mail->cc =~ /debian-mentor/i); 

    to_folder("debian-perl"), return 
        if (    $mail->to =~ /debian-perl/i
            or  $mail->cc =~ /debian-perl/i
            or  $mail->to =~ /bugs.debian.org/i
            or  $mail->cc =~ /bugs.debian.org/i
            or  $mail->to =~ /alioth.debian.org/i
            or  $mail->cc =~ /alioth.debian.org/i
            or  $mail->to =~ /packages.debian.org/i
            or  $mail->cc =~ /packages.debian.org/i
        ); 

    to_folder("debian"), return 
        if (    $mail->to =~ /lists.debian.org/i
            or  $mail->cc =~ /lists.debian.org/i); 

    to_folder("new"), return 

        # try to extract name@some.domain
        if $mail->from =~ /([^\s<>]+\@[^\s<>]+)/ and $personal{ lc $1 };

    to_folder("other"), return;

}

sub to_folder {
    my $folder = shift;
    $folder = join "/", $maildir, $folder;
    my $date = qx(date);
    chomp $date;
    my $logmsg = join $/,"",$date,"from: ".$mail->from, "subject: ".$mail->subject, "delivered to: ". $folder,"";
    $mail->accept($folder);
    open LOG, ">>$log" or warn "couldn't open $log: $!";
    $debug and print $logmsg;
    print LOG $logmsg;
    close LOG;
    
}

-----cut-----

 
> Thank a lot.
> 
> paragasu


Reply to: