=head1 NAME rcpt_smtp =head1 DESCRIPTION This plugin checks if the rcpt address is known at a given SMTP server. =head1 CONFIG It takes one required parameter, the IP address or hostname to forward to. rcpt_smtp 10.2.2.2 Optionally you can also add a port: rcpt_smtp 10.2.2.2 9025 =cut use Net::SMTP; sub register { my ($self, $qp, @args) = @_; $self->register_hook("rcpt", "rcpt_handler"); if (@args > 0) { if ($args[0] =~ /^([\.\w_-]+)$/) { $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; } $self->log(LOGWARN, "WARNING: Ignoring additional arguments.") if (@args > 2); } else { die("No SMTP server specified in smtp-forward config"); } } sub queue_handler { my ($self, $transaction, $recipient) = @_; $self->log(LOGINFO, "checking recipient at $self->{_smtp_server}:$self->{_smtp_port}"); my $smtp = Net::SMTP->new( $self->{_smtp_server}, Port => $self->{_smtp_port}, Timeout => 60, Hello => $self->qp->config("me"), ) || die $!; $smtp->mail( $transaction->sender->address || "" ) or return(DECLINED, "Unable to check rcpt ($!)"); $smtp->to($recipient->address); $self->log(LOGINFO, "check of " . $recipient->address . " returned " . $smtp->code . " " . $smtp->message); if ($smtp->code =~ /^5..$/) { $self->log(LOGINFO, "recipient " . $recipient->address . " rejected"); return (DENY, "bad address (downstream server said: " . $smtp->code . " " . $smtp->message . ")"); } $smtp->quit() or return(DECLINED, "Unable to queue message ($!)"); return (DECLINED); }