in reply to Re: Mail:Audit Resend Problem
in thread Mail:Audit Resend Problem
#!/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();
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Mail:Audit Resend Problem
by davidrw (Prior) on Jun 21, 2005 at 14:06 UTC | |
by tertullian01 (Acolyte) on Jun 21, 2005 at 17:23 UTC | |
|
Re^3: Mail:Audit Resend Problem
by Ultra (Hermit) on Jun 21, 2005 at 14:01 UTC | |
by tertullian01 (Acolyte) on Jun 21, 2005 at 17:24 UTC |