Member
Registered: Apr 2009
Location: Ghana Accra
Posts: 64
Rep:
|
icewarp mail server
Hi,
I am having some problem with the content filter. The issue is that, I
have a script that the filter is suppose to run. The script is written
in perl.
The way it is suppose to work is, the content filter have to feed the
email address the mail is coming from to the script as an input. The
script then takes the email address and filter's the mail accordingly.
Please I want to know how icewarp content filter parses an email address to a program as an input?
I need a help with this.
NB: This script works with qmail as the mail server and procmail as the
content filter.
I tried to use procmail in icewarp as external filter, but it didn't
work either.
Also, the content filter is able to run the scrip, but just that it is not parsing the email address to the script.
Below is the script:
#!/usr/bin/perl
# Title: Mail fiter script
# version: 0.1v
# Description: The script accepts mail from stdin fed from procmail, and do a querry to mssql database to perfom the neccesary filtering.
# Author: Emmanuel Buamah
# Written for: TeledataICT
#Required modules:
# Mail::Internet
# DBI
# Mail::Message
# Mail::Head
#Shell
use strict;
use DBI;
use Mail::Internet;
use Mail::Sendmail;
use Mail::Header;
use Mail::Address;
use Net::POP3;
##################################################################
sendlog();
my ($ref, $mid, $mail, $sql, $sth, $data, $sender, $message_sent, $message);
# Read mail from STDIN
my $mail = Mail::Internet->new( \*STDIN );
# Creating an empty mail object
my $reply = Mail::Internet->new();
# Locate address of sender. The get() method returns a header line from the
# message, or undef if it doesn't exist.
my $headers = $mail->head->header_hashref;
# $sender = $mail->get('Reply-To') or
$sender = $mail->get('From');
my @sender = @{${$headers}{'From'}};
my $from = $headers->{From}->[0];
#split and use only the email address part
my @hfrom = split(/[<>]/, $from);
my $emailfrom = $hfrom[1];
$sender = (Mail::Address->parse($sender))[0];
#$reply->add(To => $sender->format);
my $mailto = "mbryqf\@dot.com.gh";
#my $newreply->add(To => $mailaddr->format);
my $subject = $headers->{Subject}->[0];
my $email = $emailfrom;
my $body = join '', @{ $mail->body };
my @newbody = split(//, $body);
#$body = @newbody[1];
print $body . "\n";
my $support = "support\@dot.com.gh";
my @newmessage = ( "You are not authorized to send email message to $support from $email.\n",
"For any clarification, please call Teledata ICT customer service deparment",
" on \+233-30-2234488 weekdays between 0900 and 1700\n\n",
"Best Regards,\n",
"Customer Support\n",
"Teledata ICT Ltd\n" );
my $subauto = "AUTO RESPONDER - Not autorized to send to $support";
#database connection
my $data_source = q/dbi:ODBC:MSSQLServer/;
my $user = q/test/;
my $password = q/test/;
my $dbh = DBI->connect($data_source, $user, $password);
my $logfile = "/tmp/mailfilter.log";
my $date = localtime();
my $logmesg = $date;
open LOGFILE, ">>$logfile" or die "can't open logfile $logfile for writting $!";
if (!$dbh)
{
$subject = "AM-100" . "" . $subject;
sendEmail("$mailto", "$email", "$subject", "$body");
print LOGFILE $logmesg, "\tError connceting to the $data_source:$DBI::errstr\n \t\tMail sent with subject: $subject, succesfuly to $mailto \n";
}
my $error = "Can't prepare statement: $DBI::errstr";
$sql = "select coalesce(acm,0)acm from accounts where id in (select accid from category ";
$sql .= "where email like '%" . $email . "%') and coalesce(acm,0)>0";
$sth = $dbh->prepare($sql) or die "$error";
$sth->execute();
my $exist = $sth->fetchrow_array();
if ($exist)
{
#$subject = "AM-$exist $subject";
$subject = "AM-$exist $subject";
sendEmail("$mailto", "$email", "$subject", "$body");
print LOGFILE $date, "\t$email Found in database. Mail sent succesfuly to $mailto with the subject $subject \n";
}
else
{
sendEmail("$email", "$support","$subauto", "@newmessage");
print LOGFILE $logmesg, "\t$emailfrom was not found in database, message rejected from $emailfrom\n";
}
close(MAIL);
close(LOGFILE);
deletemail();
###################################################################
sub sendlog
{
my $logfile = "/tmp/mailfilter.log";
my $date = localtime();
my $logmesg = $date;
my $logstr = shift();
open LOGFILE, ">>$logfile" or die "can't open logfile $logfile for appemd $!";
print LOGFILE $logmesg, "\tStarting\....................\n";
close LOGFILE;
}
######################################################################
sub sendEmail
{
my ($mailto, $emailfrom, $subject, $body) = @_;
my $sendmail = '/usr/lib/sendmail';
open(MAIL, "|$sendmail -oi -t");
print MAIL "From: $emailfrom\n";
print MAIL "To: $mailto\n";
print MAIL "Subject: $subject\n\n";
print MAIL "$body \n";
close(MAIL);
}
###############################################################################################
sub deletemail
{
my ($connection, $host, $port, $deleted, $login, $user, $passwd, $messages, $mail, $message);
my $logfile = "/tmp/mailfilter.log";
my $date = localtime();
open LOGFILE, ">>$logfile" or die "can't open logfile $logfile for appemd $!";
$host = "41.211.31.55";
$port = 110;
$connection = Net::POP3->new($host, Timeout => 30);
if (!$connection)
{
print LOGFILE $date, "\tERROR:Unable to conncet to pop server $host\n";
}
$user = "support\@dot.com.gh";
$passwd = "crack";
$login = $connection->login($user,$passwd);
if (!$login)
{
print LOGFILE $date, "\t ERROR:Unable to login to host $host. check user and password\n";
}
$messages = $connection->list() or print LOFILE $date, "\t Can't get list of undeleted messages:$!\n";
foreach $mail(keys %$messages)
{
$message = $connection->get($mail);
unless (defined $message)
{
print LOGFILE $date, warn "\t Couldn't fetch $mail from server $host. $!\n";
next;
}
$connection->delete($mail);
}
close LOGFILE;
$connection->quit();
}
Regards
|