blib/lib/Email/Sender/Simple.pm

filter: Statementstmt 88.6% 86 / 97 Branchbran 62.5% 15 / 24 Conditioncond 55.5% 5 / 9 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 90.0% 18 / 20 Podpod 0.0% 0 / 6 totaltotal 77.6% 125 / 161 CC 18 SCAR 11.9
CC
18
Cov
82%
send_email
6.1
build_default_transport
6.0
transport_from_env
5.3
CRAP 3.3
« blib/lib/Email/Sender/Transport/Sendmail.pm ↑ Summary blib/lib/Email/Sender/Transport/Maildir.pm »
1 package Email::Sender::Simple 2.603;
2 # ABSTRACT: the simple interface for sending mail with Sender
3  
4 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 with 'Email::Sender::Role::CommonSending';
6  
7 #pod =head1 SEE INSTEAD
8 #pod
9 #pod For now, the best documentation of this class is in
10 #pod L<Email::Sender::Manual::QuickStart>.
11 #pod
12 #pod =cut
13  
14 3 ▶use Sub::Exporter::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Sub::Exporter -setup => {
Subroutine BEGIN: called CC=1 SCAR=0.0
16 exports => {
17 sendmail => Sub::Exporter::Util::curry_class('send'),
18 try_to_sendmail => Sub::Exporter::Util::curry_class('try_to_send'),
19 },
20 3 };
21  
22 3 ▶use Email::Address::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 3 ▶use Email::Sender::Transport;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 ▶use Email::Sender::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 3 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 {
28 3 my $DEFAULT_TRANSPORT;
29 3 my $DEFAULT_FROM_ENV;
30  
31 1 ▶ sub _default_was_from_env {
Subroutine _default_was_from_env: called CC=1 SCAR=0.0
32 1 my ($class) = @_;
33 1 $class->default_transport;
34 1 return $DEFAULT_FROM_ENV;
35 }
36  
37 2 ▶ sub transport_from_env {
Subroutine transport_from_env: called CC=5 SCAR=16.7
Pod undocumented
38 2 my ($class, $env_base) = @_;
39 2 ▶ $env_base ||= 'EMAIL_SENDER_TRANSPORT';
Condition50%1/2
$env_base ||= "EMAIL_SENDER_TRANSPORT"
l!l
02
MC/DC0%0/1
$env_base ||= "EMAIL_SENDER_TRANSPORT"
$env_base
Truth table50%1/2
A
A= $env_base
Aresult
11
00
40  
41 2 my $transport_class = $ENV{$env_base};
42 2 ▶ return unless defined $transport_class and length $transport_class;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $transport_class and length $transport_class 0 2 2
Condition33%1/3
defined $transport_class and length $transport_class
!ll&&!rl&&r
002
MC/DC0%0/2
defined $transport_class and length $transport_class
defined $transport_class length $transport_class
Truth table33%1/3
A and B
A= defined $transport_classB= length $transport_class
ABresult
0X0
100
111
43  
44 2 my %arg;
45 2 for my $key (grep { /^\Q$env_base\E_[_0-9A-Za-z]+$/ } keys %ENV) {
46 2 (my $new_key = $key) =~ s/^\Q$env_base\E_//;
47 2 $arg{lc $new_key} = $ENV{$key};
48 }
49  
50 2 return Email::Sender::Util->easy_transport($transport_class, \%arg);
51 }
52  
53 9 ▶ sub default_transport {
Subroutine default_transport: called CC=3 SCAR=11.3
Pod undocumented
54 9 ▶ return $DEFAULT_TRANSPORT if $DEFAULT_TRANSPORT;
Branch: 1 branch
BranchTrueFalseTotal
if $DEFAULT_TRANSPORT 7 2 9
55 2 my ($class) = @_;
56  
57 2 my $transport = $class->transport_from_env;
58  
59 2 ▶ if ($transport) {
Branch: 1 branch
BranchTrueFalseTotal
if ($transport) { } 2 0 2
60 2 $DEFAULT_FROM_ENV = 1;
61 2 $DEFAULT_TRANSPORT = $transport;
62 } else {
63 0 $DEFAULT_FROM_ENV = 0;
64 0 $DEFAULT_TRANSPORT = $class->build_default_transport;
65 }
66  
67 2 return $DEFAULT_TRANSPORT;
68 }
69  
70 0 ▶ sub build_default_transport {
Subroutine build_default_transport: not called CC=2 SCAR=17.9
Pod undocumented
71 0 require Email::Sender::Transport::Sendmail;
72 0 my $transport = eval { Email::Sender::Transport::Sendmail->new };
73  
74 0 ▶ return $transport if $transport;
Branch: 1 branch
BranchTrueFalseTotal
if $transport 0 0 0
75  
76 0 require Email::Sender::Transport::SMTP;
77 0 Email::Sender::Transport::SMTP->new;
78 }
79  
80 0 ▶ sub reset_default_transport {
Subroutine reset_default_transport: not called CC=1 SCAR=6.9
Pod undocumented
81 0 undef $DEFAULT_TRANSPORT;
82 0 undef $DEFAULT_FROM_ENV;
83 }
84 }
85  
86 # Maybe this should be an around, but I'm just not excited about figuring out
87 # order at the moment. It just has to work. -- rjbs, 2009-06-05
88 3 ▶around prepare_envelope => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
89 6 my ($orig, $class, $arg) = @_;
90 6 ▶ $arg ||= {};
Condition100%2/2
$arg ||= {}
l!l
15
MC/DC100%1/1
$arg ||= {}
$arg
Truth table100%2/2
A
A= $arg
Aresult
11
00
91 6 my $env = $class->$orig($arg);
92  
93 6 $env = {
94 %$arg,
95 %$env,
96 };
97  
98 6 return $env;
99 3 };
100  
101 6 ▶sub send_email {
Subroutine send_email: called CC=6 SCAR=18.1
Pod undocumented
102 6 my ($class, $email, $arg) = @_;
103  
104 6 my $transport = $class->default_transport;
105  
106 6 ▶ if ($arg->{transport}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg->{"transport"}) 1 5 6
107 1 $arg = { %$arg }; # So we can delete transport without ill effects.
108 1 ▶ $transport = delete $arg->{transport} unless $class->_default_was_from_env;
Branch: 1 branch
BranchTrueFalseTotal
unless $class->_default_was_from_env 0 1 1
109 }
110  
111 6 ▶ Carp::confess("transport $transport not safe for use with Email::Sender::Simple")
Branch: 1 branch
BranchTrueFalseTotal
unless $transport->is_simple 0 6 6
112 unless $transport->is_simple;
113  
114 6 my ($to, $from) = $class->_get_to_from($email, $arg);
115  
116 6 ▶ Email::Sender::Failure::Permanent->throw("no recipients") if ! @$to;
Branch: 1 branch
BranchTrueFalseTotal
unless @$to 2 4 6
117 4 ▶ Email::Sender::Failure::Permanent->throw("no sender") if ! defined $from;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $from 0 4 4
118  
119 4 return $transport->send(
120 $email,
121 {
122 to => $to,
123 from => $from,
124 },
125 );
126 }
127  
128 1 ▶sub try_to_send {
Subroutine try_to_send: called CC=1 SCAR=0.2
Pod undocumented
129 1 my ($class, $email, $arg) = @_;
130  
131 1 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
132 1 return $class->send($email, $arg);
133 1 ▶ } catch {
Subroutine __ANON__: called CC=3 SCAR=12.5
134 1 ▶ my $error = $_ || 'unknown error';
Condition50%1/2
$_ || 'unknown error'
l!l
10
MC/DC0%0/1
$_ || 'unknown error'
$_
Truth table50%1/2
A
A= $_
Aresult
11
00
135 1 ▶ return if try { $error->isa('Email::Sender::Failure') };
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
if &try(sub { $error->isa("Email::Sender::Failure"); } ) 1 0 1
136 0 die $error;
137 1 };
138 }
139  
140 6 ▶sub _get_to_from {
Subroutine _get_to_from: called CC=3 SCAR=11.0
141 6 my ($class, $email, $arg) = @_;
142  
143 6 my $to = $arg->{to};
144 6 ▶ unless (@$to) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$to) 5 1 6
145 my @to_addrs =
146 3 map { $_->address }
147 3 map { Email::Address::XS->parse($_) }
148 5 map { $email->get_header($_) }
149 qw(to cc);
150 5 $to = \@to_addrs;
151 }
152  
153 6 my $from = $arg->{from};
154 6 ▶ unless (defined $from) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $from) 6 0 6
155 ($from) =
156 4 map { $_->address }
157 4 map { Email::Address::XS->parse($_) }
158 6 map { $email->get_header($_) }
159 qw(from);
160 }
161  
162 6 return ($to, $from);
163 }
164  
165 3 ▶no Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
166 3 "220 OK";
167  
168 __END__
« blib/lib/Email/Sender/Transport/Sendmail.pm ↑ Summary blib/lib/Email/Sender/Transport/Maildir.pm »