Friday, May 02, 2008

Auto-Replies with Mailman Xfilters
auto-reply.xfilter (mode 750, own root:\)
Requires: Postfix Admin Vacation Table
#!/usr/bin/perl -w

use strict;
use DBI;
use MIME::QuotedPrint;

# Requires: Mail::Sendmail
# be sure to run...
# perl -MCPAN -e "install Mail::Sendmail"
use Mail::Sendmail;
# Use the local SMTP server
unshift @{$Mail::Sendmail::mailcfg{'smtp'}} , 'localhost';

my $dbhost = 'host IP';
my $dbport = '3306';
my $dbuid = 'dbusername';
my $dbpwd = 'your db R/O password';

my $dbname = 'postfix';
my $dbtable = 'vacation';
my $userColumn = 'username';
my $msgSubject = 'subject';
my $msgBody = 'body';
my $optsql = "AND active = '1'";

my $autoAddress = '';
my $autoSubdomain = 'auto-reply.';

my $SASpamHeader = 'X-Spam-Flag: YES';
my $DSpamSpamHeader = 'X-DSPAM-Result: Spam';
my $EmailList = 'Precedence: list';

my $foundFrom = 0;
my $foundTo = 0;
my $isSpam = 0;
my $isList = 0;
my $senderLine = '';
my $recipientLine = '';

#Get sender and recipient from message
while (my $line = ) {
$isSpam = 1 if ($line =~ /$SASpamHeader/ or $line =~ /$DSpamSpamHeader/);
$isList = 1 if ($line =~ /$EmailList/);

if (not $foundFrom and $line =~ /^From:(.*)@(.*)$/) {
$foundFrom = 1;
$senderLine = $line;
}
if (not $foundTo and $line =~ /^To:(.*)@(.*)$/) {
$foundTo = 1;
$recipientLine = $line;
}
print $line;
}

if ($foundFrom and $foundTo and not $isSpam and not $isList) {
# Required info from the message was found -- lets go!
# Setup MySQL connection
my $dbh = DBI->connect("DBI:mysql:$dbname:$dbhost:$dbport","$dbuid","$dbpwd");

# Split the addresses out
my $rx = $Mail::Sendmail::address_rx;
$senderLine =~ /$rx/;
my $sender = $1;
$recipientLine =~ /$rx/;
my $recipient = $1;
my $username = $2;
my $domain = $3;

# Query the DB for a vacation entry
my $sqlStatement = "SELECT $msgSubject,$msgBody FROM $dbtable WHERE $userColumn = '$username' $optsql AND ac
tive = '1'";
my $query = $dbh->prepare($sqlStatement) || die "Error prepraring query";
$query->execute;
my @results = $query->fetchrow_array();

# Check for no results, if none we're done so exit
my $resultLen = @results;
if ($resultLen == 0) { exit 0; }
my($messageSubject,$messageTxt) = @results;

# Lets make the response
my $autoresponder = '';
$autoresponder = "$autoAddress" if ($autoAddress);
$autoresponder = $username."@".$autoSubdomain.$domain if (not $autoAddress);
my %mailmessage=();
%mailmessage = (To => "$sender",
From => "Auto-Reply <$autoresponder>",
Precedence => "list",
'User-Agent' => "maildrop::autoreply.xfilter",
Subject => "$messageSubject",
Message => "$messageTxt"
);

#Send it out the door
sendmail(%mailmessage);
}

exit 0;


__END__

=head1 NAME

autoreply.xfilter - Message Auto-Reply Xfilter for Maildrop.

=head1 SYNOPSIS

From maildroprc:

xfilter "autoreply.xfilter"

From the command line:

perl autoreply.xfilter <> new_message

=head1 LICENCE

Copyright (c) 2007 Rob MacKinnon
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.

I love you BSD license!!

=head1 AUTHOR

Rob MacKinnon <c4blem0nkey AT gmail.com>

=cut

Labels:

0 Comments:

Post a Comment

<< Home