File: smtp-forward

package info (click to toggle)
qpsmtpd 0.32-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 860 kB
  • ctags: 237
  • sloc: perl: 4,219; sh: 592; makefile: 54
file content (69 lines) | stat: -rw-r--r-- 2,047 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
=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;

sub register {
  my ($self, $qp, @args) = @_;

  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 hook_queue {
  my ($self, $transaction) = @_;

  $self->log(LOGINFO, "forwarding to $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 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 ($!)");
  $transaction->body_resetpos;
  while (my $line = $transaction->body_getline) {
    $smtp->datasend($line) or return(DECLINED, "Unable to queue message ($!)");
  }
  $smtp->dataend() or return(DECLINED, "Unable to queue message ($!)");
  $smtp->quit() or return(DECLINED, "Unable to queue message ($!)");
  $self->log(LOGINFO, "finished queueing");
  return (OK, "Queued!");
}