Here is the code I have written:
#!/usr/bin/perl #Declare packages to be used always including strict and utf8 use strict; use utf8; use Mail::Audit; use IO::Handle; use DBI; #Connect to the database my $dbh = DBI->connect("dbi:Pg:dbname=database", "user", "password", { +AutoCommit => 0}); #Declare variables to be used including command line variables if need +ed my ($stored_email, $log, $to, $from, $forward_address, $store, $body, +$sql, $sth, $date, $local_address); my (@filter, @row); my (%forward); my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(t +ime); $date = "$mday/$mon/$year $hour:$min:$sec"; #Create any functions necessary #Main body of program ##Set variable values $sql = "SELECT \"ForwardMailTo\", \"ForwardMailFrom\" FROM \"tblForwardMail\""; $sth = $dbh->prepare($sql); $sth->execute; while (@row = $sth->fetchrow) { $forward{$row[0]} = $row[1]; } $stored_email = "stored_mail.txt"; $log = "audit_mail.log"; $store = "NO"; ##Open the log file open (LOG, ">>$log") or die "$date --- Couldn't open $log"; ##See if there is a match for the to email to find the forward address my $incoming = Mail::Audit->new; $to = $incoming->to(); $from = $incoming->from(); ##Test to see if the to address exists and map the forwarding address while(my ($key, $value) = each(%forward)) { if ($to =~ $key) { $local_address = $key; $forward_address = $value; } } if ($forward_address) { print LOG "$date --- $to has been mapped to $forward_address\n"; $sql = "SELECT \"tblForwardMail\".\"ForwardMailID\", \"tblFilterMa +il\".\"FilterMailEmail\" FROM \"tblForwardMail\" LEFT JOIN \"tblFilterMail\" ON \"tblForwardMail\".\"Forwar +dMailID\"=\"tblFilterMail\".\"ForwardMailID\" WHERE \"tblForwardMail\".\"ForwardMailTo\"='$local_address +'"; $sth = $dbh->prepare($sql); $sth->execute; while (@row = $sth->fetchrow) { push(@filter, $row[1]); } } else { print LOG "$date --- No mapping for $to\n"; close (LOG); exit(); } ##Check to see if the from address is part of the filter list print LOG "$date --- Searching for filter email.\n"; foreach my $email_address(@filter) { if (($from =~ $email_address)){ $store = "YES"; } } ##Either store the email and forward it or just forward the email if ($store eq "YES") { open (OUTPUT, ">>$stored_email") or print LOG "$date --- Couldn't +open $stored_email.\n"; print OUTPUT @{$incoming->body()}; print OUTPUT "##-------------------------------------------------- +-------------##"; close(OUTPUT); print LOG "$date --- Filter address found. $to has been forwarded +to $forward_address\n"; $incoming->resend($forward_address) or print LOG "$date --- Couldn +'t forward to $forward_address\n"; } else { print LOG "$date --- No filter address found. $to has been forwar +ded to $forward_address.\n"; $incoming->resend($forward_address) or print LOG "$date --- Couldn +'t forward to $forward_address\n"; } ##Close the log file close (LOG); $dbh->disconnect();

In reply to Re^2: Mail:Audit Resend Problem by tertullian01
in thread Mail:Audit Resend Problem by tertullian01

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.