/
c3mc-base-sendmail-smtp
executable file
·109 lines (85 loc) · 2.76 KB
/
c3mc-base-sendmail-smtp
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
#!/data/Software/mydan/perl/bin/perl -I/data/Software/mydan/Connector/lib
use strict;
use warnings;
binmode STDIN, ':utf8';
binmode STDOUT, ':utf8';
binmode STDERR, ':utf8';
$|++;
use MYDan::Util::OptConf;
use Encode;
use File::Temp;
use POSIX;
use Email::Sender::Simple qw(sendmail);
use Email::Sender::Transport::SMTPS;
use MIME::Words qw(encode_mimewords);
=head1 SYNOPSIS
$0 user1 user2 user3 --subject test
=cut
my $option = MYDan::Util::OptConf->load();
my %o = $option->get( qw( subject=s ) )->dump();
$option->assert() unless @ARGV;
$option->assert( 'subject' );
local $/ = undef;
my $content = Encode::encode('utf8', <STDIN> );
local $/ = "\n";
my $name = 'mail';
my $path = sprintf "/data/open-c3-data/send/%s/$name/", POSIX::strftime( "%F", localtime );
system "mkdir -p '$path'" unless -d $path;
sub call
{
my ( $user, $subject, $content ) = @_;
my %conf;
map{
$conf{$_} = `c3mc-sys-ctl sys.notify.mail.kind.smtp.$_`;
chomp $conf{$_};
}qw( server port username password from ssl );
my %ssl = ( $conf{ssl} && $conf{ssl} ne 'null' ) ? ( ssl => $conf{ssl} ) : ();
my $transport = Email::Sender::Transport::SMTPS->new(
host => $conf{server},
port => $conf{port},
timeout => 300,
sasl_username => $conf{username},
sasl_password => $conf{password},
%ssl,
);
my $ct = $subject =~ /TT000/ ? 'text/html' : 'text/plain';
my $message = Email::Simple->create(
header => [
From => $conf{from},
To => $user,
Subject => encode_mimewords($subject, Charset => 'UTF-8', Encoding => 'Q'),
'Content-Type' => "$ct; charset=UTF-8",
'charset' => 'UTF-8',
],
body => $content,
);
eval { sendmail($message, { transport => $transport }); };
die "sendmail fail: $@" if $@;
return 1;
}
sub xsend
{
my ( $user, $subject, $content ) = @_;
my ( $err, $status );
for my $i ( reverse 1 .. 2 )
{
my $stat = eval{ call( $user, $subject, $content ); };
$err = $@ ? "call $name error: $@" : "";
$status = ( $stat && ! $err ) ? "ok": "fail";
YAML::XS::DumpFile sprintf( "$path/%s.%03d.$user.$status", POSIX::strftime( "%Y%m%d%H%M%S", localtime ), rand 1000 ),
+{
subject => Encode::decode('utf8', $subject ),
content => Encode::decode('utf8', $content ),
user => $user,
err => $err
};
last if $status eq 'ok';
if( $i > 1 )
{
warn sprintf( "sendmesg error %s", $err );
sleep 3;
}
}
die sprintf( "%s: status %s", $err, $status ) unless $status eq 'ok';
}
map{ xsend( $_, $o{subject}, $content ) }@ARGV;