Send/SMTP.pm

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