This repository has been archived by the owner on Feb 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
bb-sendmail.pl
executable file
·314 lines (264 loc) · 8.19 KB
/
bb-sendmail.pl
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#!/usr/bin/perl -w
#
# GPLv3 - (c) Babel srl www.babel.it
#
# Author: rpolli@babel.it
#
# TODO send multiple attachments
#
use strict;
use Net::SMTP;
use Net::SMTP::SSL;
use Getopt::Long;
use MIME::Base64;
my $verbose = 0;
sub dprint($) {
print shift if $verbose;
}
sub usage() {
die
"Usage: $0 -s server:port -f from -t to -c cc -b bcc -m message -a auth_type -u user -p pass -j subject -n attachment -d data_file\n"
. "\n"
. "Send an email using the given parameters\n"
. " -h print this screen\n"
. " -v verbosely dump smtp session\n"
. " -i send i-times the same email on the same connection\n"
. " -s smtp server - eg. mx.babel.it:25, smtps:msa.babel.it:465\n"
. " -e EHLO string\n"
. " -f sender - set MAIL FROM\n"
. " -t recipient - set RCPT TO\n"
. " -c cc-recipient - you can use multiple -c params\n"
. " -b bcc-recipient - you can use multiple -b params\n"
. " -a auth_type: LOGIN, PLAIN\n"
. " -u username\n"
. " -p password\n"
. " -j subJect\n"
. " -m message body\n"
. " -n attachmeNt\n"
. " -d data file - use the given file to fill the whole DATA section of the smpt session\n"
. "\n";
}
#
# Return an encoded string for PLAIN login
#
sub plain_string($$) { #username password
my ( $username, $password ) = @_;
$username =~ s|@|\@|; #escape @
my $ret = encode_base64( "\000" . $username . "\000" . $password );
chomp($ret);
return ($ret);
}
#
# Handle PLAIN even without PERL SASL support
#
sub auth_smtp_plain($$$) { #mailer username password
my ( $smtp, $username, $password ) = @_;
$smtp->datasend(
"AUTH PLAIN " . plain_string( $username, $password ) . "\n" );
$smtp->response();
}
#
# Handle LOGIN even without PERL SASL support
#
sub auth_smtp_login($$$) { #mailer username password
my ( $smtp, $username, $password ) = @_;
$smtp->datasend("AUTH LOGIN\n");
$smtp->response();
$smtp->datasend( encode_base64($username) );
$smtp->response();
$smtp->datasend( encode_base64($password) );
$smtp->response();
}
#
# Prepare Attachment body
#
sub datasend_attachment($$) { #filename
my ( $body, $path ) = @_;
my $filename = $path;
$filename =~ s|.*/||g;
open( DATA, $path ) || die("Could not open the file: $path");
my $boundary = rand(1000000) . "antani" . rand(1000000);
#
# Attachment body is base64 encoded,
# has a mimetype and a boundary
#
my $mime_header =
"Content-Transfer-Encoding: 7bit\n"
. "Content-type: multipart/mixed; boundary=\"$boundary\"\n"
. "MIME-Version: 1.0\n"
. "This is a multi-part message in MIME format.\n" . "\n";
#
# Text part: body starts immediately after the Content-Type
# just one LF is needed
#
my $text_part =
"\n\n--$boundary\n"
. "Content-Transfer-Encoding: binary\n"
. "Content-Type: text/plain\n" . "$body";
#
# Attachment part
# boundary starts with --$boundary and ends with --$boundary--
#
my $attachment_header =
"\n\n--$boundary\n"
. "Content-Transfer-Encoding: base64\n"
. "Content-Type: application/*; name=\"$filename\"\n"
. "Content-Disposition: attachment; filename=\"$filename\"\n" . "\n";
my $attachment_body = "";
my $buff;
while ( read( DATA, $buff, 4096 ) ) {
$attachment_body .= encode_base64($buff);
}
my $mime_footer = "--$boundary--\n";
close(DATA);
return
$mime_header
. $text_part
. $attachment_header
. $attachment_body
. $mime_footer;
}
sub main() {
my ( $argc, @argv ) = ( $#ARGV, @ARGV );
my @notify = qw/NEVER/;
my @cc = ();
my @bcc = ();
my $help;
my ( $proto, $server, $port ) = qw/smtp localhost 25/;
my (
$sender, $recipient, $cc, $bcc,
$auth_type, $username, $password, $subject,
$message_body, $data_file, $helo, $data,
$attachment, $data_header, $iterations
);
my $result = GetOptions(
's=s' => \$server, # server options
'e|ehlo=s' => \$helo,
'c|cc=s' => \@cc,
'b|bcc=s' => \@bcc,
'f=s' => \$sender,
't=s' => \$recipient,
'a=s' => \$auth_type, #authentication
'u=s' => \$username,
'p=s' => \$password,
'd=s' => \$data_file, #body options
'j=s' => \$subject,
'm=s' => \$message_body,
'n=s' => \$attachment,
'i=s' => \$iterations,
'v' => \$verbose,
'h|help' => \$help # help verbose
);
usage() if ( $help or $argc < 1 );
if ($verbose) {
@notify = qw/SUCCESS FAILURE DELAY/;
}
if ( $server =~ /^smtps:/ ) {
( $proto, $server, $port ) = split( /:/, $server );
}
else {
( $server, $port ) = split( /:/, $server ) if ($server);
}
#validate input parameters
die("Missing SMTP host or port: use -s server:port")
unless ( $server and $port );
# sender and recipient are compulsory
die("Missing sender or recipient") unless ( $sender and $recipient );
#authentication requires:
# * -a PLAIN|LOGIN
# * user and password
if ( defined $auth_type ) {
die("Bad auth_type") unless ( ( $auth_type =~ m/PLAIN|LOGIN/ ) );
die("Missing username or password") unless ( $username and $password );
}
# using a data file overrides the following fields:
# * subject
# * message
if ( defined $data_file ) {
die("Missing file $data_file") unless ( -e $data_file );
die("Data file overrides subject and message body")
if ( $message_body or $subject );
$data = `cat $data_file`;
}
else {
die("Missing subject: use -j 'subject string'") unless ($subject);
die(
"Missing body: use -m 'message body\n eventually spanning more lines.\n'"
) unless ($message_body);
$data_header = sprintf( "Subject: %s\r\n", $subject );
$data = $message_body;
}
#
# Create the mailer class
#
my $mailer;
if ( $proto eq "smtps" ) {
$mailer = Net::SMTP::SSL->new(
$server,
Port => $port,
Debug => $verbose
) or die("Can't create mailer object to $server:$port.");
}
else {
$mailer = Net::SMTP->new(
$server,
Port => $port,
Debug => $verbose
) or die("Can't create mailer object to $server:$port.");
}
# eventually send helo
$mailer->hello($helo) if ($helo);
#
# Eventually setup authentication. If you lack some perl modules like SASL
# use a local implementation of PLAIN login.
#
if ( defined $auth_type ) {
dprint("setting authentication\n");
if ( $auth_type eq "LOGIN" ) {
auth_smtp_login( $mailer, $username, $password );
}
elsif ( $auth_type eq "PLAIN" ) {
auth_smtp_plain( $mailer, $username, $password );
}
else {
$mailer->auth( $username, $password );
}
}
$iterations = 1 unless($iterations gt 0);
while ( $iterations-- > 0 ) {
#
# Now we can send mail
#
$mailer->mail($sender)
or die("KO: rejected sender: $sender");
$mailer->to($recipient)
or die("KO: rejected recipient: $recipient");
$mailer->bcc(@bcc) if ( @bcc > 0 );
#
# Set data header
#
if ( @cc > 0 ) {
$mailer->cc(@cc);
$data_header .= "CC: " . join( ',', @cc ) . "\r\n";
}
#
# Eventually add one attachment, encapsulating data in a
# multipart message
#
$data = datasend_attachment( $data, $attachment )
if ( defined $attachment );
#
# Finally add data
#
$mailer->data;
# headers will never go into a mime message
$mailer->datasend($data_header);
# data may be embedded in a mime message
$mailer->datasend($data);
$mailer->dataend;
}
$mailer->quit;
print "mail sent OK\n";
exit(0);
}
&main();