blib/lib/App/CLI/Plugin/Net/SMTP.pm

filter: Statementstmt 54.5% 12 / 22 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 75.0% 3 / 4 Podpod 100% 1 / 1 totaltotal 28.0% 16 / 57 CC 10 SCAR 44.4
CC
10
Cov
27%
smtp_open
110.0
BEGIN
1.0
BEGIN
1.0
CRAP 84.8
↑ Summary
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 = <STDIN>;
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 <<EOL | ./myapp mailer -f "me@localhost" -t "you@remotehost" -s "mail subject"
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 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
91 1 ▶use base qw(Class::Accessor::Grouped);
Subroutine BEGIN: called CC=1 SCAR=0.0
92 1 ▶use Net::SMTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
93  
94 1 __PACKAGE__->mk_group_accessors("smtp");
95 1 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 0 ▶sub smtp_open {
Subroutine smtp_open: not called CC=10 SCAR=47.0
Pod documented
108  
109 0 my($self, $smtp_option) = @_;
110 0 ▶ if (ref($smtp_option) ne "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $smtp_option ne "HASH") 0 0 0
111 0 $smtp_option = $self->config->{net_smtp};
112 }
113 0 ▶ if (!defined $smtp_option) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $smtp_option) 0 0 0
114 0 die "net_smtp option is always required";
115 }
116  
117 0 ▶ if( (exists $ENV{APPCLI_SMTP_STARTTLS} && defined $ENV{APPCLI_SMTP_STARTTLS}) ||
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ENV{"APPCLI_SMTP_STARTTLS"} and defined $ENV{"APPCLI_SMTP_STARTTLS"} or exists $smtp_option->{"StartTLS"} and defined $smtp_option->{"StartTLS"} or exists $self->{"starttls"} and defined $self->{"starttls"}) 0 0 0
Condition0%0/15
exists $ENV{"APPCLI_SMTP_STARTTLS"} and defined $ENV{"APPCLI_SMTP_STARTTLS"} or exists $smtp_option->{"StartTLS"} and defined $smtp_option->{"StartTLS"} or exists $self->{"starttls"} and defined $self->{"starttls"}
l!l&&r!l&&!r
000
exists $ENV{"APPCLI_SMTP_STARTTLS"} and defined $ENV{"APPCLI_SMTP_STARTTLS"} or exists $smtp_option->{"StartTLS"} and defined $smtp_option->{"StartTLS"}
l!l&&r!l&&!r
000
exists $ENV{"APPCLI_SMTP_STARTTLS"} and defined $ENV{"APPCLI_SMTP_STARTTLS"}
!ll&&!rl&&r
000
exists $smtp_option->{"StartTLS"} and defined $smtp_option->{"StartTLS"}
!ll&&!rl&&r
000
exists $self->{"starttls"} and defined $self->{"starttls"}
!ll&&!rl&&r
000
MC/DC0%0/6
exists $ENV{"APPCLI_SMTP_STARTTLS"} and defined $ENV{"APPCLI_SMTP_STARTTLS"} or exists $smtp_option->{"StartTLS"} and defined $smtp_option->{"StartTLS"} or exists $self->{"starttls"} and defined $self->{"starttls"}
exists $ENV{"APPCLI_SMTP_STARTTLS"} defined $ENV{"APPCLI_SMTP_STARTTLS"} exists $smtp_option->{"StartTLS"} defined $smtp_option->{"StartTLS"} exists $self->{"starttls"} defined $self->{"starttls"}
Truth table0%0/15
A and B or C and D or E and F
A= exists $ENV{"APPCLI_SMTP_STARTTLS"}B= defined $ENV{"APPCLI_SMTP_STARTTLS"}C= exists $smtp_option->{"StartTLS"}D= defined $smtp_option->{"StartTLS"}E= exists $self->{"starttls"}F= defined $self->{"starttls"}
ABCDEFresult
11XXXX1
0X11XX1
1011XX1
0X0X111
0X10111
100X111
1010111
0X0X0X0
0X0X100
0X100X0
0X10100
100X0X0
100X100
10100X0
1010100
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 ▶ $self->smtp(Net::SMTP->new(%{$smtp_option}) or die $!);
Condition0%0/2
'Net::SMTP'->new(%{$smtp_option;}) || die($!)
l!l
00
MC/DC0%0/1
'Net::SMTP'->new(%{$smtp_option;}) || die($!)
'Net::SMTP'->new(%{$smtp_option;})
Truth table0%0/2
A
A= 'Net::SMTP'->new(%{$smtp_option;})
Aresult
11
00
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 1;
173  
174 __END__
↑ Summary