blib/lib/Email/Sender/Transport/SMTPS.pm

filter: Statementstmt 32.9% 32 / 97 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 30.0% 6 / 20 Podpod 0.0% 0 / 3 totaltotal 19.1% 38 / 198 SCAR 64.6
CC
34
Cov
19%
send_email
272.0
_net_smtp_args
72.0
_smtp_client
56.0
CRAP 642.1
↑ Summary Build »
1 package Email::Sender::Transport::SMTPS;
2  
3 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use MooX::Types::MooseLike::Base qw(Bool Int Str);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 # ABSTRACT: Email::Sender joins Net::SMTPS
6  
7 1 ▶use Email::Sender::Failure::Multi;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Email::Sender::Success::Partial;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Email::Sender::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 our $VERSION = '0.04';
11  
12 0 ▶has host => (is => 'ro', isa => Str, default => sub { 'localhost' });
Subroutine __ANON__: not called CC=1 SCAR=1.2
13 1 has ssl => (is => 'ro', isa => Str);
14 has port => (
15 is => 'ro',
16 isa => Int,
17 lazy => 1,
18 0 ▶ default => sub { return ($_[0]->ssl and $_[0]->ssl eq 'starttls') ? 587 : $_[0]->ssl ? 465 : 25; },
Subroutine __ANON__: not called CC=4 SCAR=30.0
Branch: 2 branches
BranchTrueFalseTotal
$_[0]->ssl && $_[0]->ssl eq 'starttls' ? : 0 0 0
$_[0]->ssl ? : 0 0 0
Condition0%0/3
$_[0]->ssl and $_[0]->ssl eq "starttls"
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0]->ssl and $_[0]->ssl eq "starttls"
$_[0]->ssl $_[0]->ssl eq "starttls"
Truth table0%0/3
A and B
A= $_[0]->sslB= $_[0]->ssl eq "starttls"
ABresult
0X0
100
111
19 1 );
20  
21 0 ▶has timeout => (is => 'ro', isa => Int, default => sub { 120 });
Subroutine __ANON__: not called CC=1 SCAR=1.2
22  
23 1 has sasl_username => (is => 'ro', isa => Str);
24 1 has sasl_password => (is => 'ro', isa => Str);
25  
26 0 ▶has allow_partial_success => (is => 'ro', isa => Bool, default => sub { 0 });
Subroutine __ANON__: not called CC=1 SCAR=1.2
27  
28 1 has helo => (is => 'ro', isa => Str);
29 1 has localaddr => (is => 'ro');
30 1 has localport => (is => 'ro', isa => Int);
31 1 has debug => (is => 'ro', isa => Bool);
32  
33 # I am basically -sure- that this is wrong, but sending hundreds of millions of
34 # messages has shown that it is right enough. I will try to make it textbook
35 # later. -- rjbs, 2008-12-05
36 sub _quoteaddr {
37 0 ▶ my $addr = shift;
Subroutine _quoteaddr: not called CC=3 SCAR=24.8
38 0 my @localparts = split /\@/, $addr;
39 0 my $domain = pop @localparts;
40 0 my $localpart = join q{@}, @localparts;
41  
42 # this is probably a little too paranoid
43 0 ▶ return $addr unless $localpart =~ /[^\w.+-]/ or $localpart =~ /^\./;
Branch: 1 branch
BranchTrueFalseTotal
unless $localpart =~ /[^\w.+-]/ or $localpart =~ /^\./ 0 0 0
Condition0%0/3
$localpart =~ /[^\w.+-]/ or $localpart =~ /^\./
l!l&&r!l&&!r
000
MC/DC0%0/2
$localpart =~ /[^\w.+-]/ or $localpart =~ /^\./
$localpart =~ /[^\w.+-]/ $localpart =~ /^\./
Truth table0%0/3
A or B
A= $localpart =~ /[^\w.+-]/B= $localpart =~ /^\./
ABresult
1X1
011
000
44 0 return join q{@}, qq("$localpart"), $domain;
45 }
46  
47 sub _smtp_client {
48 0 ▶ my ($self) = @_;
Subroutine _smtp_client: not called CC=7 SCAR=40.3
49  
50 0 my $class = "Net::SMTP";
51 0 ▶ if ($self->ssl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ssl) { } 0 0 0
52 0 require Net::SMTPS;
53 0 $class = "Net::SMTPS";
54 } else {
55 0 require Net::SMTP;
56 }
57  
58 0 my $smtp = $class->new( $self->_net_smtp_args );
59  
60 0 ▶ $self->_throw("unable to establish SMTP connection") unless $smtp;
Branch: 1 branch
BranchTrueFalseTotal
unless $smtp 0 0 0
61  
62 0 ▶ if ($self->sasl_username) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->sasl_username) 0 0 0
63 0 ▶ $self->_throw("sasl_username but no sasl_password")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->sasl_password 0 0 0
64 unless defined $self->sasl_password;
65  
66 0 ▶ unless ($smtp->auth($self->sasl_username, $self->sasl_password)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($smtp->auth($self->sasl_username, $self->sasl_password)) 0 0 0
67 0 ▶ if ($smtp->message =~ /MIME::Base64|Authen::SASL/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($smtp->message =~ /MIME::Base64|Authen::SASL/) 0 0 0
68 0 Carp::confess("SMTP auth requires MIME::Base64 and Authen::SASL");
69 }
70  
71 0 $self->_throw('failed AUTH', $smtp);
72 }
73 }
74  
75 0 return $smtp;
76 }
77  
78 sub _net_smtp_args {
79 0 ▶ my ($self) = @_;
Subroutine _net_smtp_args: not called CC=8 SCAR=42.8
80  
81 # compatible
82 0 my $ssl = $self->ssl;
83 0 ▶ $ssl = 'ssl' if $self->ssl and $self->ssl ne 'starttls';
Branch: 1 branch
BranchTrueFalseTotal
if $self->ssl and $self->ssl ne "starttls" 0 0 0
Condition0%0/3
$self->ssl and $self->ssl ne "starttls"
!ll&&!rl&&r
000
MC/DC0%0/2
$self->ssl and $self->ssl ne "starttls"
$self->ssl $self->ssl ne "starttls"
Truth table0%0/3
A and B
A= $self->sslB= $self->ssl ne "starttls"
ABresult
0X0
100
111
84 return (
85 0 ▶ $self->host,
Branch: 5 branches
BranchTrueFalseTotal
defined $ssl ? : 0 0 0
defined $self->helo ? : 0 0 0
defined $self->localaddr ? : 0 0 0
defined $self->localport ? : 0 0 0
defined $self->debug ? : 0 0 0
86 Port => $self->port,
87 Timeout => $self->timeout,
88 defined $ssl ? (doSSL => $ssl) : (),
89 defined $self->helo ? (Hello => $self->helo) : (),
90 defined $self->localaddr ? (LocalAddr => $self->localaddr) : (),
91 defined $self->localport ? (LocalPort => $self->localport) : (),
92 defined $self->debug ? (Debug => $self->debug) : (),
93 );
94 }
95  
96 sub _throw {
97 0 ▶ my ($self, @rest) = @_;
Subroutine _throw: not called CC=1 SCAR=6.9
98 0 Email::Sender::Util->_failure(@rest)->throw;
99 }
100  
101 sub send_email {
102 0 ▶ my ($self, $email, $env) = @_;
Subroutine send_email: not called CC=16 SCAR=56.1
Pod undocumented
103  
104 Email::Sender::Failure->throw("no valid addresses in recipient list")
105 0 ▶ unless my @to = grep { defined and length } @{ $env->{to} };
Branch: 2 branches
BranchTrueFalseTotal
unless my(@to) = grep({defined $_ and length $_;} @{$$env{"to"};}) 0 0 0
defined $_ and length $_ 0 0 0
106  
107 0 my $smtp = $self->_smtp_client;
108  
109 0 ▶ my $FAULT = sub { $self->_throw($_[0], $smtp); };
Subroutine __ANON__: not called CC=1 SCAR=6.9
110  
111 0 ▶ $smtp->mail(_quoteaddr($env->{from}))
Branch: 1 branch
BranchTrueFalseTotal
$smtp->mail(_quoteaddr($env->{"from"})) or &$FAULT(qq[$env->{"from"} failed after MAIL FROM:]) 0 0 0
112 or $FAULT->("$env->{from} failed after MAIL FROM:");
113  
114 0 my @failures;
115 my @ok_rcpts;
116  
117 0 for my $addr (@to) {
118 0 ▶ if ($smtp->to(_quoteaddr($addr))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($smtp->to(_quoteaddr($addr))) { } 0 0 0
119 0 push @ok_rcpts, $addr;
120 } else {
121 # my ($self, $error, $smtp, $error_class, @rest) = @_;
122 0 push @failures, Email::Sender::Util->_failure(
123 undef,
124 $smtp,
125 recipients => [ $addr ],
126 );
127 }
128 }
129  
130 # This logic used to include: or (@ok_rcpts == 1 and $ok_rcpts[0] eq '0')
131 # because if called without SkipBad, $smtp->to can return 1 or 0. This
132 # should not happen because we now always pass SkipBad and do the counting
133 # ourselves. Still, I've put this comment here (a) in memory of the
134 # suffering it caused to have to find that problem and (b) in case the
135 # original problem is more insidious than I thought! -- rjbs, 2008-12-05
136  
137 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (@failures and @ok_rcpts == 0 || !$self->allow_partial_success) 0 0 0
Condition0%0/6
@failures and @ok_rcpts == 0 || !$self->allow_partial_success
!ll&&!rl&&r
000
@ok_rcpts == 0 or not $self->allow_partial_success
l!l&&r!l&&!r
000
MC/DC0%0/3
@failures and @ok_rcpts == 0 || !$self->allow_partial_success
@failures @ok_rcpts == 0 not $self->allow_partial_success
Truth table0%0/4
A and B or not(C)
A= @failuresB= @ok_rcpts == 0C= not $self->allow_partial_success
ABCresult
0XX0
1000
11X1
1011
138 @failures
139 and ((@ok_rcpts == 0) or (! $self->allow_partial_success))
140 ) {
141 0 ▶ $failures[0]->throw if @failures == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @failures == 1 0 0 0
142  
143 0 ▶ my $message = sprintf '%s recipients were rejected during RCPT',
Branch: 1 branch
BranchTrueFalseTotal
@ok_rcpts ? : 0 0 0
144 @ok_rcpts ? 'some' : 'all';
145  
146 0 Email::Sender::Failure::Multi->throw(
147 message => $message,
148 failures => \@failures,
149 );
150 }
151  
152 # restore Pobox's support for streaming, code-based messages, and arrays here
153 # -- rjbs, 2008-12-04
154  
155 0 ▶ $smtp->data or $FAULT->("error at DATA start");
Branch: 1 branch
BranchTrueFalseTotal
$smtp->data or &$FAULT("error at DATA start") 0 0 0
156  
157 0 my $msg_string = $email->as_string;
158 0 my $hunk_size = $self->_hunk_size;
159  
160 0 ▶ while (length $msg_string) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $msg_string) 0 0 0
161 0 my $next_hunk = substr $msg_string, 0, $hunk_size, '';
162 0 ▶ $smtp->datasend($next_hunk) or $FAULT->("error at during DATA");
Branch: 1 branch
BranchTrueFalseTotal
$smtp->datasend($next_hunk) or &$FAULT("error at during DATA") 0 0 0
163 }
164  
165 0 ▶ $smtp->dataend or $FAULT->("error at after DATA");
Branch: 1 branch
BranchTrueFalseTotal
$smtp->dataend or &$FAULT("error at after DATA") 0 0 0
166  
167 0 my $message = $smtp->message;
168  
169 0 $self->_message_complete($smtp);
170  
171 # We must report partial success (failures) if applicable.
172 0 ▶ return $self->success({ message => $message }) unless @failures;
Branch: 1 branch
BranchTrueFalseTotal
unless @failures 0 0 0
173 0 return $self->partial_success({
174 message => $message,
175 failure => Email::Sender::Failure::Multi->new({
176 message => 'some recipients were rejected during RCPT',
177 failures => \@failures
178 }),
179 });
180 }
181  
182 0 ▶sub _hunk_size { 2**20 } # send messages to DATA in hunks of 1 mebibyte
Subroutine _hunk_size: not called CC=1 SCAR=6.9
183  
184 sub success {
185 0 ▶ my $self = shift;
Subroutine success: not called CC=1 SCAR=6.9
Pod undocumented
186 0 my $success = Moo::Role->create_class_with_roles('Email::Sender::Success', 'Email::Sender::Role::HasMessage')->new(@_);
187 }
188  
189 sub partial_success {
190 0 ▶ my $self = shift;
Subroutine partial_success: not called CC=1 SCAR=6.9
Pod undocumented
191 0 my $partial_success = Moo::Role->create_class_with_roles('Email::Sender::Success::Partial', 'Email::Sender::Role::HasMessage')->new(@_);
192 }
193  
194 0 ▶sub _message_complete { $_[1]->quit; }
Subroutine _message_complete: not called CC=1 SCAR=6.9
195  
196 1 with 'Email::Sender::Transport';
197 1 ▶no Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
198 1 1;
199 __END__
↑ Summary Build »