2012-04-29 05:41:31 +02:00
|
|
|
#!perl -Tw
|
2003-06-09 13:06:41 +02:00
|
|
|
=head1 NAME
|
|
|
|
|
|
|
|
smtp-forward
|
|
|
|
|
|
|
|
=head1 DESCRIPTION
|
|
|
|
|
|
|
|
This plugin forwards the mail via SMTP to a specified server, rather than
|
|
|
|
delivering the email locally.
|
|
|
|
|
|
|
|
=head1 CONFIG
|
|
|
|
|
|
|
|
It takes one required parameter, the IP address or hostname to forward to.
|
|
|
|
|
|
|
|
queue/smtp-forward 10.2.2.2
|
|
|
|
|
|
|
|
Optionally you can also add a port:
|
|
|
|
|
|
|
|
queue/smtp-forward 10.2.2.2 9025
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
use Net::SMTP;
|
|
|
|
|
2006-03-20 17:47:05 +01:00
|
|
|
sub init {
|
2003-06-09 13:06:41 +02:00
|
|
|
my ($self, $qp, @args) = @_;
|
|
|
|
|
|
|
|
if (@args > 0) {
|
2003-07-24 14:44:19 +02:00
|
|
|
if ($args[0] =~ /^([\.\w_-]+)$/) {
|
2003-06-09 13:06:41 +02:00
|
|
|
$self->{_smtp_server} = $1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
die "Bad data in smtp server: $args[0]";
|
|
|
|
}
|
|
|
|
$self->{_smtp_port} = 25;
|
|
|
|
if (@args > 1 and $args[1] =~ /^(\d+)$/) {
|
|
|
|
$self->{_smtp_port} = $1;
|
|
|
|
}
|
2004-03-05 13:46:24 +01:00
|
|
|
$self->log(LOGWARN, "WARNING: Ignoring additional arguments.") if (@args > 2);
|
2003-06-09 13:06:41 +02:00
|
|
|
} else {
|
|
|
|
die("No SMTP server specified in smtp-forward config");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2005-07-07 06:17:39 +02:00
|
|
|
sub hook_queue {
|
2003-06-09 13:06:41 +02:00
|
|
|
my ($self, $transaction) = @_;
|
|
|
|
|
2004-03-05 13:46:24 +01:00
|
|
|
$self->log(LOGINFO, "forwarding to $self->{_smtp_server}:$self->{_smtp_port}");
|
2003-06-09 13:06:41 +02:00
|
|
|
my $smtp = Net::SMTP->new(
|
|
|
|
$self->{_smtp_server},
|
|
|
|
Port => $self->{_smtp_port},
|
|
|
|
Timeout => 60,
|
2004-07-16 07:03:25 +02:00
|
|
|
Hello => $self->qp->config("me"),
|
2003-06-09 13:06:41 +02:00
|
|
|
) || die $!;
|
2004-06-11 22:00:15 +02:00
|
|
|
$smtp->mail( $transaction->sender->address || "" ) or return(DECLINED, "Unable to queue message ($!)");
|
|
|
|
for ($transaction->recipients) {
|
|
|
|
$smtp->to($_->address) or return(DECLINED, "Unable to queue message ($!)");
|
|
|
|
}
|
|
|
|
$smtp->data() or return(DECLINED, "Unable to queue message ($!)");
|
|
|
|
$smtp->datasend($transaction->header->as_string) or return(DECLINED, "Unable to queue message ($!)");
|
2003-06-09 13:06:41 +02:00
|
|
|
$transaction->body_resetpos;
|
|
|
|
while (my $line = $transaction->body_getline) {
|
2004-06-11 22:00:15 +02:00
|
|
|
$smtp->datasend($line) or return(DECLINED, "Unable to queue message ($!)");
|
2003-06-09 13:06:41 +02:00
|
|
|
}
|
2004-06-11 22:00:15 +02:00
|
|
|
$smtp->dataend() or return(DECLINED, "Unable to queue message ($!)");
|
2003-06-09 13:06:41 +02:00
|
|
|
$smtp->quit() or return(DECLINED, "Unable to queue message ($!)");
|
2004-03-05 13:46:24 +01:00
|
|
|
$self->log(LOGINFO, "finished queueing");
|
2003-06-09 13:06:41 +02:00
|
|
|
return (OK, "Queued!");
|
|
|
|
}
|