hello

I try to set up a simple filter for postfix. the concept is pretty simple: a mail is received in queue and will be piped to the filter, the script extract the header ("To:" and "From:") and compare it to a sql database. if the sender isn't registred then the script will send a registration mail linking to a registration form.

the problem is when i test the filter by hand ( cat mail | ./filter ) everything work properly but when it is called by postfix the header aren't extract ( i don't know if it is the mail which isn't piped or another problem )
here is the code
#!/usr/bin/perl use DBI(); $temp=$$ ; $log = "/var/spool/filter/log"; if (-e $log){ open (LOG, ">>$log");} else {open (LOG, ">$log");} @liste = (<>) ; $i = 0 ; #trouve l'expediteur du mail $to = find_exp_to('To:'); $from = find_exp_to('From:'); print LOG "querrying contact:$from for user:$to \n\n"; db_check($to ,$from); if ($trouve <= 0 ) { mail($from) ; after_mail(); } else { after_mail(); } close LOG; sub db_check { $dbh = DBI->connect( "dbi:mysql:email", "user", "pass", {'RaiseError' => 1} ); $select= shift ; $test = shift ; $trouve = 0; $query= qq{select * from contact where user_name='$select' }; $sth = $dbh->prepare($query); $rv= $sth->execute || die $sth->errstr; while ($answer = $sth->fetchrow_arrayref ) { my ($id,$user,$contact) = @$answer; if ($contact=~ /($test)/i){ print LOG "contact => $test exist \n" ; $trouve = 1 ; } } $sth->finish; $dbh->disconnect(); if ($trouve <= 0){print LOG "$test not in db gonna send a mail\n";} } sub find_exp_to { my $find = shift ; $i = 0; while($liste[$i] ) { if ($liste[$i]=~ m/$find*?(.\w+\S)/cg) { #tous les car +acteres sur la lignes contenant From: $liste[$i]=~ s/($find)\s\b//g;#on vire le from $liste[$i]=~ s/\s//g; $data = $liste[$i]; } $i++; } return($data); } #envoi un mail a l'addr pass\uffff en param sub mail { require Mail::Send ; my $sender = shift ; $msg = new Mail::Send ; $msg->to($sender); $msg->subject('Mail register') ; #appelle les header de la classe: $msg->set($header, @values); $fh = $msg->open; print $fh "Bonjour, pour pouvoir envoyer un mail a $to vous de +vez vous enregister http://sever/cgi-bin/register.pl"; $fh->close ; } #reverifie la db ap mailing si pas de reponse avt time out detruit le +mail sub after_mail { $j = 0; open(TEMP, "+>/var/spool/filter/$temp"); print(TEMP "@liste"); @delete = TEMP ; close(TEMP); $trouve = 0 ; $i =0; while ( $i < 10 && $trouve ==0 ) { db_check($to, $from) ; if ($trouve == 0) { print LOG "pas de news ds la base rest ds 10 s +econde\n"; sleep 10; } else { $trouve = 1; } $i++; } if ($trouve == 0) { print LOG "operation time out deleting mail from $from +\n"; unlink @delete ; } else { @arg=("sendmail","-i","$sender","$recipient","<","@lis +te"); system(@arg) == 0 || die "exec a send back error :$?"; unlink @delete; } }
Any advice would be welcome. Thanks in advance

In reply to postfix filter... by Ang-st

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.