> Can we have the script put on the list. I may have a requirement to forward

Here you go, complete with some debug code still in it. And probably some
bugs. Watch out for line wraps.

-----------------------------------------
#!/usr/bin/perl

use Net::LDAP;

my $LDAPHOST = 'localhost';
my $BASEDN = 'ou=People,dc=NetToNetTech,dc=com';
my $ERR_TO = '[EMAIL PROTECTED]';

# for mail sent to [EMAIL PROTECTED]
# $ENV{DEFAULT} will contain the "something"

my $group = lc($ENV{DEFAULT});

if ($group eq "newyork") {
        $f = "l=New York";

} elsif ($group eq "us") {
        $f = "physicalDeliveryOfficeName=*US";

} elsif ($group eq "apac") {
        $f = "physicalDeliveryOfficeName=*Korea";

} elsif ($group eq "emea") {
        $f = "physicalDeliveryOfficeName=Newbury*";

} elsif ($group =~ /marketing|sales|eng|it|finance|admin/) {
        $f = "ou=$group";

} else {
        $env = join("\n", %ENV);
        &error_hard("Unknown address: \n$env");

}


$ldap = Net::LDAP->new($LDAPHOST);

$ldap->bind or &error_soft("Can not bind to LDAP server: $!");

$mesg = $ldap->search (  # perform a search
                            base   => $BASEDN,
                            filter => "(&(objectclass=qmailuser)($f))",
                            attrs => ['mail'],
                       );

if ($mesg->code) {
    &error_soft("LDAP Search error: " . $mesg->code . $mesg->error);
}

foreach $entry ($mesg->all_entries) {
        my $attr = $entry->get("mail");
        push @mails, $attr->[0] if $attr;
}

$ldap->unbind;

$mailto = join(" ", @mails);

open(M, "|/var/qmail/bin/qmail-inject $mailto")
  || &error_hard("Unable to open pipe to qmail-inject: $!");
while(<STDIN>) { print M $_; }
close M;

# for error codes, man qmail-command says:
# 100 means that the delivery failed permanently (hard error);
# 111 means that the delivery  failed  but  should
#     be tried again in a little while (soft error).

sub error_soft {
        my($msg) = @_;
        &email_error($msg);
        exit 111;
}

sub error_hard {
        my($msg) = @_;
        &email_error($msg);
        exit 100;
}

sub email_error {
        my($msg) = @_;

        return 0 unless $ERR_TO;

open(M, "|/var/qmail/bin/qmail-inject $ERR_TO");
print M<<EOF;
To: $ERR_TO
From: $ERR_TO
Subject: Error $0

Error Message:
$msg

EOF
}


Reply via email to