line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package App::CLI::Plugin::Net::SMTP; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=pod |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=head1 NAME |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
App::CLI::Plugin::Net::SMTP - for App::CLI::Extension mail module |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=head1 VERSION |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
1.3 |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=head1 SYNOPSIS |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
# MyApp.pm |
16
|
|
|
|
|
|
|
package MyApp; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
use strict; |
19
|
|
|
|
|
|
|
use base qw(App::CLI::Extension); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
# extension method |
22
|
|
|
|
|
|
|
__PACKAGE__->load_plugins(qw(Net::SMTP)); |
23
|
|
|
|
|
|
|
__PACKAGE__->config(net_smtp => { Host => "localhost", Port => 25, Timeout => 30 }); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
1; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
# MyApp/Mailer.pm |
28
|
|
|
|
|
|
|
package MyApp::Hello; |
29
|
|
|
|
|
|
|
use strict; |
30
|
|
|
|
|
|
|
use feature ":5.10.0"; |
31
|
|
|
|
|
|
|
use base qw(App::CLI::Command); |
32
|
|
|
|
|
|
|
use Encode qw(encode decode); |
33
|
|
|
|
|
|
|
use MIME::Entity; |
34
|
|
|
|
|
|
|
our $VERSION = '1.0'; |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub options { |
37
|
|
|
|
|
|
|
return ( |
38
|
|
|
|
|
|
|
"from|f=s" => "from", |
39
|
|
|
|
|
|
|
"to|t=s" => "to", |
40
|
|
|
|
|
|
|
"subject|s=s" => "subject" |
41
|
|
|
|
|
|
|
); |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub run { |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
my($self, @args) = @_; |
47
|
|
|
|
|
|
|
my @messages = ; |
48
|
|
|
|
|
|
|
my $entity = MIME::Entity->build( |
49
|
|
|
|
|
|
|
From => $self->{from}, |
50
|
|
|
|
|
|
|
To => $self->{to}, |
51
|
|
|
|
|
|
|
Subject => encode("MIME-Header", decode("utf8", $self->{subject})), |
52
|
|
|
|
|
|
|
Type => "text/plain", |
53
|
|
|
|
|
|
|
"X-Mailier" => sprintf("%s V%s", __PACKAGE__, $VERSION), |
54
|
|
|
|
|
|
|
Charset => "utf-8", |
55
|
|
|
|
|
|
|
Data => \@messages |
56
|
|
|
|
|
|
|
); |
57
|
|
|
|
|
|
|
$self->smtp_open; |
58
|
|
|
|
|
|
|
$self->smtp->mail($self->{from}); |
59
|
|
|
|
|
|
|
$self->smtp->to($self->{to}); |
60
|
|
|
|
|
|
|
$self->smtp->data; |
61
|
|
|
|
|
|
|
$self->smtp->datasend($entity->stringify); |
62
|
|
|
|
|
|
|
$self->smtp->datasend; |
63
|
|
|
|
|
|
|
$self->smtp->quit; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
# myapp |
67
|
|
|
|
|
|
|
#!/usr/bin/perl |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
use strict; |
70
|
|
|
|
|
|
|
use MyApp; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
MyApp->dispatch; |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
# execute |
75
|
|
|
|
|
|
|
[kurt@localhost ~] cat <
|
76
|
|
|
|
|
|
|
pipe heredoc> this is myapp mailer message. |
77
|
|
|
|
|
|
|
pipe heredoc> I sent you? |
78
|
|
|
|
|
|
|
pipe heredoc> EOL |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=head1 DESCRIPTION |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
App::CLI::Extension Net::SMTP plugin module |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
smtp method setting |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
__PACKAGE__->config( net_smtp => {%net_smtp_option} ); |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=cut |
89
|
|
|
|
|
|
|
|
90
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
44
|
|
91
|
1
|
|
|
1
|
|
7
|
use base qw(Class::Accessor::Grouped); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
1042
|
|
92
|
1
|
|
|
1
|
|
26400
|
use Net::SMTP; |
|
1
|
|
|
|
|
115654
|
|
|
1
|
|
|
|
|
283
|
|
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
__PACKAGE__->mk_group_accessors("smtp"); |
95
|
|
|
|
|
|
|
our $VERSION = '1.3'; |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=pod |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=head1 METHOD |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=head2 smtp_open |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
initialize Net::SMTP |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=cut |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
sub smtp_open { |
108
|
|
|
|
|
|
|
|
109
|
0
|
|
|
0
|
1
|
|
my($self, $smtp_option) = @_; |
110
|
0
|
0
|
|
|
|
|
if (ref($smtp_option) ne "HASH") { |
111
|
0
|
|
|
|
|
|
$smtp_option = $self->config->{net_smtp}; |
112
|
|
|
|
|
|
|
} |
113
|
0
|
0
|
|
|
|
|
if (!defined $smtp_option) { |
114
|
0
|
|
|
|
|
|
die "net_smtp option is always required"; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
0
|
0
|
0
|
|
|
|
if( (exists $ENV{APPCLI_SMTP_STARTTLS} && defined $ENV{APPCLI_SMTP_STARTTLS}) || |
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
118
|
|
|
|
|
|
|
(exists $smtp_option->{StartTLS} && defined $smtp_option->{StartTLS}) || |
119
|
|
|
|
|
|
|
(exists $self->{starttls} && defined $self->{starttls}) ){ |
120
|
0
|
|
|
|
|
|
require IO::Socket::SSL; |
121
|
0
|
|
|
|
|
|
unshift @Net::SMTP::ISA, "IO::Socket::SSL"; |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
0
|
|
0
|
|
|
|
$self->smtp(Net::SMTP->new(%{$smtp_option}) or die $!); |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=head2 smtp |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
return Net::SMTP object. Environment variable APPCLI_SMTP_STARTTLS, net_smtp to StartTLS, the script runs starttls option, you specify one, IO::Socket::SSL If you have installed on your system, SMTPS can make a connection with. |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
But even then one way, net_smtp option of the Port is defined as SMTPS to specify the port number |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
Example1 APPCLI_SMTP_STARTTLS: |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
# if your shell is bash... |
136
|
|
|
|
|
|
|
export APPCLI_SMTP_STARTTLS=1 |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
Example2 StartTLS config: |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
# in MyApp.pm |
141
|
|
|
|
|
|
|
__PACKAGE__->config( |
142
|
|
|
|
|
|
|
net_smtp => { |
143
|
|
|
|
|
|
|
Host => "localhost", |
144
|
|
|
|
|
|
|
Timeout => 30, |
145
|
|
|
|
|
|
|
Port => 465, |
146
|
|
|
|
|
|
|
StartTLS => 1 |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
); |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
Example3 starttls option: |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
echo "hello" | ./myapp mailer -f "me@localhost" -t "you@remotehost" -s "mail subject" --starttls |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
Advance |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
# in MyApp/Mailer.pm |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
sub options { |
159
|
|
|
|
|
|
|
return ( |
160
|
|
|
|
|
|
|
"starttls" => "starttls", |
161
|
|
|
|
|
|
|
"from|f=s" => "from", |
162
|
|
|
|
|
|
|
"to|t=s" => "to", |
163
|
|
|
|
|
|
|
"subject|s=s" => "subject" |
164
|
|
|
|
|
|
|
); |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
starttls option like that you define |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=cut |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
1; |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
__END__ |