-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsmtp_server.pl
executable file
·74 lines (55 loc) · 1.85 KB
/
smtp_server.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
#!/usr/bin/perl -w
use strict;
use Carp;
use File::Temp qw( tempfile );
use File::Basename;
use File::Copy;
use File::chown;
use Encode qw( encode decode );
use Net::SMTP::Server;
use Net::SMTP::Server::Client;
use Net::SMTP::Server::Relay;
use Email::Simple;
use Data::Dumper;
use constant SPOOL_DIR => '/var/spool/sms/outgoing';
#use constant SPOOL_DIR => '/tmp';
use constant USER => 'smsd';
use constant GROUP => 'smsd';
my $server = new Net::SMTP::Server('0.0.0.0', 25) ||
croak("Unable to handle client connection: $!\n");
while (my $conn = $server->accept()) {
my $client = new Net::SMTP::Server::Client($conn) ||
croak("Unable to handle client connection: $!\n");
$client->process || next;
# print Dumper $client;
for (@{$client->{TO}}) {
# destination and message text
my $destination = $_;
$destination =~ s/\D//g;
my $email = Email::Simple->new($client->{MSG});
my $subject = $email->header('Subject') || '';
my $body = $email->body || '';
my $message = ($subject && $body) ? ($subject . ' ' . $body) : ($subject . $body);
# print Dumper $email->header('To');
# print Dumper ($destination, $message);
# convert message from UTF-8 to UCS
$message = encode('UCS-2BE', decode('UTF-8', $message));
$message =~ s/\r$//; # remove trailing carriage return
my ($fh, $temp_file) = tempfile();
#binmode( $fh, ":utf8" );
chown USER, GROUP, $temp_file;
print $fh "To: " . $destination . "\n";
print $fh "Alphabet: UCS\n";
print $fh "\n";
print $fh $message;
close($fh);
my $message_file = SPOOL_DIR . '/' . $destination . '_' . basename($temp_file);
my $lock_file = $message_file . '.LOCK';
open(LOCK_FILE, ">>" . $lock_file) || die "Cannot open file: " . $!;
close(LOCK_FILE);
chown USER, GROUP, $lock_file;
move($temp_file, $message_file) || die $!;
chown USER, GROUP, $message_file;
unlink $lock_file;
}
}