Send/SMTP.pm

filter: Statementstmt 25.6% 19 / 74 Branchbran 7.8% 3 / 38 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 57.1% 4 / 7 Podpod 0.0% 0 / 4 totaltotal 17.8% 26 / 146 CC 25 SCAR 56.1
CC
25
Cov
17%
send
420.0
get_env_sender
6.0
get_env_recipients
6.0
CRAP 272.4
↑ Summary Send/NNTP.pm »
1 package Email::Send::SMTP;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 3 our $VERSION = '2.202';
5 3 $VERSION = eval $VERSION;
6  
7 3 ▶use Email::Address 1.80;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 local $Return::Value::NO_CLUCK = 1;
10 3 require Return::Value;
11 3 Return::Value->import;
12 }
13  
14  
15 1 ▶sub is_available {
Subroutine is_available: called CC=4 SCAR=15.2
Pod undocumented
16 1 my ($class, %args) = @_;
17 1 my $success = 1;
18 1 $success = eval { require Net::SMTP };
19 0 ▶ $success = eval { require Net::SMTP::SSL } if $args{ssl};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"ssl"} 0 1 1
20 0 ▶ $success = eval { require Net::SMTP::TLS } if $args{tls};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"tls"} 0 1 1
21 1 ▶ return $success
Branch: 1 branch
BranchTrueFalseTotal
$success ? : 1 0 1
22 ? success
23 : failure $@;
24 }
25  
26 0 ▶sub get_env_sender {
Subroutine get_env_sender: not called CC=2 SCAR=17.9
Pod undocumented
27 0 my ($class, $message) = @_;
28  
29 0 ▶ return unless my $hdr = $message->header('From');
Branch: 1 branch
BranchTrueFalseTotal
unless my $hdr = $message->header("From") 0 0 0
30 0 my $from = (Email::Address->parse($hdr))[0]->address;
31 }
32  
33 0 ▶sub get_env_recipients {
Subroutine get_env_recipients: not called CC=2 SCAR=17.9
Pod undocumented
34 0 my ($class, $message) = @_;
35  
36 0 my %to = map { $_->address => 1 }
37 0 map { Email::Address->parse($_) }
38 0 ▶ grep { defined and length }
Condition0%0/3
defined $_ && length $_
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_ && length $_
defined $_ length $_
Truth table0%0/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
39 0 map { $message->header($_) }
40 qw(To Cc Bcc);
41  
42 0 return keys %to;
43 }
44  
45 0 ▶sub send {
Subroutine send: not called CC=20 SCAR=60.4
Pod undocumented
46 0 my ($class, $message, @args) = @_;
47  
48 0 my %args;
49 0 ▶ if ( @args % 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args % 2) { } 0 0 0
50 0 my $host = shift @args;
51 0 %args = @args;
52 0 $args{Host} = $host;
53 } else {
54 0 %args = @args;
55 }
56  
57 0 ▶ my $host = delete($args{Host}) || 'localhost';
Condition0%0/2
delete $args{'Host'} || 'localhost'
l!l
00
MC/DC0%0/1
delete $args{'Host'} || 'localhost'
delete $args{'Host'}
Truth table0%0/2
A
A= delete $args{'Host'}
Aresult
11
00
58  
59 my $smtp_class = $args{ssl} ? 'Net::SMTP::SSL'
60 0 ▶ : $args{tls} ? 'Net::SMTP::TLS'
Branch: 2 branches
BranchTrueFalseTotal
$args{'ssl'} ? : 0 0 0
$args{'tls'} ? : 0 0 0
61 : 'Net::SMTP';
62  
63 0 ▶ eval "require $smtp_class; 1" or die;
Branch: 1 branch
BranchTrueFalseTotal
eval "require $smtp_class; 1" or die 0 0 0
64  
65 0 ▶ if ( $smtp_class eq 'Net::SMTP::TLS' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($smtp_class eq "Net::SMTP::TLS") 0 0 0
66 # ::TLS has different User/Password than the rest
67 0 ▶ $args{User} ||= $args{username};
Condition0%0/3
$args{"User"} ||= $args{"username"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"User"} ||= $args{"username"}
$args{"User"} $args{"username"}
Truth table0%0/3
A ||= B
A= $args{"User"}B= $args{"username"}
ABresult
1X1
011
000
68 0 ▶ $args{Password} ||= $args{password};
Condition0%0/3
$args{"Password"} ||= $args{"password"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"Password"} ||= $args{"password"}
$args{"Password"} $args{"password"}
Truth table0%0/3
A ||= B
A= $args{"Password"}B= $args{"password"}
ABresult
1X1
011
000
69 }
70  
71 0 my $SMTP = $smtp_class->new($host, %args);
72 0 ▶ return failure "Couldn't connect to $host" unless $SMTP;
Branch: 1 branch
BranchTrueFalseTotal
unless $SMTP 0 0 0
73  
74 0 my ($user, $pass) = @args{qw[username password]};
75  
76 # for ::TLS, the auth is done by the new()
77 0 ▶ if ( $user and not $smtp_class eq 'Net::SMTP::TLS' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user and not $smtp_class eq "Net::SMTP::TLS") 0 0 0
Condition0%0/3
$user and not $smtp_class eq "Net::SMTP::TLS"
!ll&&!rl&&r
000
MC/DC0%0/2
$user and not $smtp_class eq "Net::SMTP::TLS"
$user not $smtp_class eq "Net::SMTP::TLS"
Truth table0%0/3
A and not(B)
A= $userB= not $smtp_class eq "Net::SMTP::TLS"
ABresult
0X0
100
111
78 0 ▶ $SMTP->auth($user, $pass)
Branch: 1 branch
BranchTrueFalseTotal
$SMTP->auth($user, $pass) or return failure("Couldn't authenticate '${user}:...'") 0 0 0
79 or return failure "Couldn't authenticate '$user:...'";
80 }
81  
82 0 my @bad;
83 0 eval {
84 0 my $from = $class->get_env_sender($message);
85  
86 # ::TLS has no useful return value, but will croak on failure.
87 0 ▶ eval { $SMTP->mail($from); 1 } or return failure "FROM: <$from> denied";
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $SMTP->mail($from); 1 } } or return failure("FROM: <$from> denied") 0 0 0
88  
89 0 my @to = $class->get_env_recipients($message);
90  
91 0 ▶ if (eval { $SMTP->isa('Net::SMTP::TLS') }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $SMTP->isa('Net::SMTP::TLS') } }) { } 0 0 0
92 0 $SMTP->to(@to);
93 } else {
94 0 my @ok = $SMTP->to(@to, { SkipBad => 1 });
95  
96 0 ▶ if ( @to != @ok ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@to != @ok) 0 0 0
97 0 my %to; @to{@to} = (1) x @to;
98 0 delete @to{@ok};
99 0 @bad = keys %to;
100 }
101 }
102  
103 0 ▶ return failure "No valid recipients" if @bad == @to;
Branch: 1 branch
BranchTrueFalseTotal
if @bad == @to 0 0 0
104 };
105  
106 0 ▶ return failure $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
107  
108 0 ▶ if ( $smtp_class eq 'Net::SMTP::TLS' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($smtp_class eq 'Net::SMTP::TLS') { } 0 0 0
109 0 $SMTP->data;
110 0 $SMTP->datasend( $message->as_string );
111 0 $SMTP->dataend;
112 }
113 else {
114 0 ▶ return failure "Can't send data" unless $SMTP->data( $message->as_string );
Branch: 1 branch
BranchTrueFalseTotal
unless $SMTP->data($message->as_string) 0 0 0
115 }
116  
117 0 $SMTP->quit;
118 0 return success "Message sent", prop => { bad => [ @bad ], };
119 }
120  
121 3 1;
122  
123 __END__
↑ Summary Send/NNTP.pm »