#Declare packages to be used always including strict and utf8 use strict; use warnings; use Mail::Audit; use IO::Handle; use DBI; #Connect to the database my $dbh = DBI->connect("dbi:Pg:dbname=database", "username", "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, @time); my (%forward); @time = localtime; $date = sprintf("%2d/%2d/%4d %02d:%02d:%02d", $time[4] + 1, $time[3], +$time[5] + 1900, @time[2,1,0]); #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 = "/home/gevterm/Mail_Script/stored_mail.txt"; $log = "/home/gevterm/Mail_Script/audit_mail.log"; $store = 0; ##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; $incoming->noexit(1); $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 = <<EOF; SELECT "tblForwardMail"."ForwardMailID", "tblFilterMail"."FilterMailEm +ail" FROM "tblForwardMail" JOIN "tblFilterMail" ON "tblForwardMail"."ForwardMailID"="tblFilterMai +l"."ForwardMailID" WHERE "tblForwardMail"."ForwardMailTo"='$local_address' EOF $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 = 1; } } ##Either store the email and forward it or just forward the email $incoming->resend($forward_address) or print LOG "$date --- Couldn't f +orward to $forward_address\n"; if ($store) { open (OUTPUT, ">>", $stored_email) or print LOG "$date --- Couldn' +t open $stored_email.\n"; print OUTPUT "##--------------------------------Start $date------- +------------------------##\n"; my $body = ""; if ($incoming->is_mime()) { if ($incoming->is_multipart()) { my $temp = $incoming->parts(0); $body = $temp->stringify_body; } else { $body = $incoming->stringify_body; } } else { $body = @{$incoming->body()}; } #Grab the charset value my $header = $incoming->header(); if ($header =~ m/charset="(\S+)"/m){ if ($1 ne "UTF-8") { print LOG "ENCODING IS UTF8\n"; #$body = $body . "\n" . to_utf8({ -string => $body, -chars +et => $1 }); } } #If there is no charset then assume charset ASCII or ISO-8859-1 else { #$body = $body . "\n" . to_utf8({ -string => $body, -charset = +> 'ISO-8859-1' }); } print OUTPUT $body . "\n"; print OUTPUT "##-------------------------------- End $date ------- +------------------------##\n"; close(OUTPUT); print LOG "$date --- Filter address found. $to has been forwarded +to $forward_address\n"; } if (!$store) { print LOG "$date --- No filter address found. $to has been forwar +ded to $forward_address.\n"; } ##Close the log file close (LOG); $dbh->disconnect();
In reply to Mail::Audit Resend Never Arrives by tertullian01
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |