1 |
slords |
1.1 |
=head1 NAME |
2 |
|
|
|
3 |
|
|
smtp-forward |
4 |
|
|
|
5 |
|
|
=head1 DESCRIPTION |
6 |
|
|
|
7 |
|
|
This plugin forwards the mail via SMTP to a specified server, rather than |
8 |
|
|
delivering the email locally. |
9 |
|
|
|
10 |
|
|
=head1 CONFIG |
11 |
|
|
|
12 |
|
|
It takes one required parameter, the IP address or hostname to forward to. |
13 |
|
|
|
14 |
|
|
queue/smtp-forward 10.2.2.2 |
15 |
|
|
|
16 |
|
|
Optionally you can also add a port: |
17 |
|
|
|
18 |
|
|
queue/smtp-forward 10.2.2.2 9025 |
19 |
|
|
|
20 |
|
|
=cut |
21 |
|
|
|
22 |
|
|
use Net::SMTP; |
23 |
|
|
|
24 |
|
|
sub register { |
25 |
|
|
my ($self, $qp, @args) = @_; |
26 |
|
|
|
27 |
|
|
if (@args > 0) { |
28 |
|
|
if ($args[0] =~ /^([\.\w_-]+)$/) { |
29 |
|
|
$self->{_smtp_server} = $1; |
30 |
|
|
} |
31 |
|
|
else { |
32 |
|
|
die "Bad data in smtp server: $args[0]"; |
33 |
|
|
} |
34 |
|
|
$self->{_smtp_port} = 25; |
35 |
|
|
if (@args > 1 and $args[1] =~ /^(\d+)$/) { |
36 |
|
|
$self->{_smtp_port} = $1; |
37 |
|
|
} |
38 |
|
|
$self->log(LOGWARN, "WARNING: Ignoring additional arguments.") if (@args > 2); |
39 |
|
|
} else { |
40 |
|
|
die("No SMTP server specified in smtp-forward config"); |
41 |
|
|
} |
42 |
|
|
|
43 |
|
|
} |
44 |
|
|
|
45 |
|
|
sub hook_queue { |
46 |
|
|
my ($self, $transaction) = @_; |
47 |
|
|
|
48 |
|
|
$self->log(LOGINFO, "forwarding to $self->{_smtp_server}:$self->{_smtp_port}"); |
49 |
|
|
my $smtp = Net::SMTP->new( |
50 |
|
|
$self->{_smtp_server}, |
51 |
|
|
Port => $self->{_smtp_port}, |
52 |
|
|
Timeout => 60, |
53 |
|
|
Hello => $self->qp->config("me"), |
54 |
|
|
) || die $!; |
55 |
|
|
$smtp->mail( $transaction->sender->address || "" ); |
56 |
|
|
my $rc = $smtp->code; |
57 |
|
|
my $message = $smtp->message; |
58 |
|
|
chomp($message); |
59 |
|
|
|
60 |
|
|
if ($rc =~ m/^4\d{2}$/ ) { |
61 |
|
|
return(DENYSOFT, "Unable to queue message ($message)"); |
62 |
|
|
} elsif ($rc =~ m/^5\d{2}$/ ) { |
63 |
|
|
return(DENY, "Unable to queue message ($message)"); |
64 |
|
|
} |
65 |
|
|
|
66 |
|
|
for ($transaction->recipients) { |
67 |
|
|
$smtp->to($_->address); |
68 |
|
|
$rc = $smtp->code; |
69 |
|
|
$message = $smtp->message; |
70 |
|
|
chomp($message); |
71 |
|
|
|
72 |
|
|
if ($rc =~ m/^4\d{2}$/ ) { |
73 |
|
|
return(DENYSOFT, "Unable to queue message ($message)"); |
74 |
|
|
} elsif ($rc =~ m/^5\d{2}$/ ) { |
75 |
|
|
return(DENY, "Unable to queue message ($message)"); |
76 |
|
|
} |
77 |
|
|
} |
78 |
|
|
|
79 |
|
|
$smtp->data(); |
80 |
|
|
$rc = $smtp->code; |
81 |
|
|
#$message = $smtp->message; |
82 |
|
|
chomp($message); |
83 |
|
|
|
84 |
|
|
if ($rc =~ m/^4\d{2}$/ ) { |
85 |
|
|
return(DENYSOFT, "Unable to queue message ($message)"); |
86 |
|
|
} elsif ($rc =~ m/^5\d{2}$/ ) { |
87 |
|
|
return(DENY, "Unable to queue message ($message)"); |
88 |
|
|
} |
89 |
|
|
|
90 |
|
|
$smtp->datasend($transaction->header->as_string); |
91 |
|
|
$rc = $smtp->code; |
92 |
|
|
$message = $smtp->message; |
93 |
|
|
chomp($message); |
94 |
|
|
|
95 |
|
|
|
96 |
|
|
if ($message =~ m/^End data with/ ) { |
97 |
|
|
(undef, $message) = split(/:/, $message); |
98 |
|
|
if ($message) { $message =~ s/^\s//; } |
99 |
|
|
} |
100 |
|
|
|
101 |
|
|
if ($rc =~ m/^4\d{2}$/ ) { |
102 |
|
|
return(DENYSOFT, "Unable to queue message ($message)"); |
103 |
|
|
} elsif ($rc =~ m/^5\d{2}$/ ) { |
104 |
|
|
return(DENY, "Unable to queue message ($message)"); |
105 |
|
|
} |
106 |
|
|
|
107 |
|
|
$transaction->body_resetpos; |
108 |
|
|
|
109 |
|
|
while (my $line = $transaction->body_getline) { |
110 |
|
|
$smtp->datasend($line); |
111 |
|
|
|
112 |
|
|
$rc = $smtp->code; |
113 |
|
|
$message = $smtp->message; |
114 |
|
|
chomp($message); |
115 |
|
|
|
116 |
|
|
if ($message =~ m/^End data with/ ) { |
117 |
|
|
(undef, $message) = split(/:/, $message); |
118 |
|
|
if ($message) { $message =~ s/^\s//; } |
119 |
|
|
} |
120 |
|
|
|
121 |
|
|
if ($rc =~ m/^4\d{2}$/ ) { |
122 |
|
|
return(DENYSOFT, "Unable to queue message ($message)"); |
123 |
|
|
} elsif ($rc =~ m/^5\d{2}$/ ) { |
124 |
|
|
return(DENY, "Unable to queue message ($message)"); |
125 |
|
|
} |
126 |
|
|
} |
127 |
|
|
|
128 |
|
|
$smtp->dataend(); |
129 |
|
|
$rc = $smtp->code; |
130 |
|
|
$message = $smtp->message; |
131 |
|
|
my $queue_message; |
132 |
|
|
chomp($message); |
133 |
|
|
|
134 |
|
|
if ($message =~ m/^End data with/ ) { |
135 |
|
|
(undef, $message) = split(/\n/, $message); |
136 |
|
|
if ($message) { $message =~ s/^\s//; } |
137 |
|
|
} |
138 |
|
|
|
139 |
|
|
if ($rc =~ m/^4\d{2}$/ ) { |
140 |
|
|
return(DENYSOFT, "Unable to queue message ($message)"); |
141 |
|
|
} elsif ($rc =~ m/^5\d{2}$/ ) { |
142 |
|
|
return(DENY, "Unable to queue message ($message)"); |
143 |
|
|
} else { |
144 |
|
|
$queue_message = $message; |
145 |
|
|
} |
146 |
|
|
|
147 |
|
|
$smtp->quit(); |
148 |
|
|
$rc = $smtp->code; |
149 |
|
|
$message = $smtp->message; |
150 |
|
|
chomp($message); |
151 |
|
|
|
152 |
|
|
if ($rc =~ m/^4\d{2}$/ ) { |
153 |
|
|
return(DENYSOFT, "Unable to queue message ($message)"); |
154 |
|
|
} elsif ($rc =~ m/^5\d{2}$/ ) { |
155 |
|
|
return(DENY, "Unable to queue message ($message)"); |
156 |
|
|
} |
157 |
|
|
|
158 |
|
|
$self->log(LOGINFO, "finished queueing"); |
159 |
|
|
return (OK, "Queued! ($queue_message)"); |
160 |
|
|
} |