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

filter: Statementstmt 90.9% 40 / 44 Branchbran 56.2% 9 / 16 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 90.0% 9 / 10 Podpod 0.0% 0 / 2 totaltotal 78.6% 59 / 75 CC 11 SCAR 13.8
CC
11
Cov
81%
_find_sendmail
7.6
send_email
4.2
_sendmail_pipe
3.0
CRAP 4.0
« blib/lib/Email/Sender/Transport/Mbox.pm ↑ Summary blib/lib/Email/Sender/Failure/Multi.pm »
1 package Email::Sender::Transport::Sendmail 2.601;
2 # ABSTRACT: send mail via sendmail(1)
3  
4 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 with 'Email::Sender::Transport';
6  
7 2 ▶use MooX::Types::MooseLike::Base qw(Str);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 #pod =head2 DESCRIPTION
10 #pod
11 #pod This transport sends mail by piping it to the F<sendmail> command. If the
12 #pod location of the F<sendmail> command is not provided in the constructor (see
13 #pod below) then the library will look for an executable file called F<sendmail> in
14 #pod the path.
15 #pod
16 #pod To specify the location of sendmail:
17 #pod
18 #pod my $sender = Email::Sender::Transport::Sendmail->new({ sendmail => $path });
19 #pod
20 #pod =cut
21  
22 2 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 has 'sendmail' => (
25 is => 'ro',
26 isa => Str,
27 required => 1,
28 lazy => 1,
29 default => sub {
30 # This should not have to be lazy, but Moose has a bug(?) that prevents the
31 # instance or partial-instance from being passed in to the default sub.
32 # Laziness doesn't hurt much, though, because (ugh) of the BUILD below.
33 # -- rjbs, 2008-12-04
34  
35 # return $ENV{PERL_SENDMAIL_PATH} if $ENV{PERL_SENDMAIL_PATH}; # ???
36 0 ▶ return $_[0]->_find_sendmail('sendmail');
Subroutine __ANON__: not called CC=1 SCAR=6.9
37 },
38 2 );
39  
40 sub BUILD {
41 3 ▶ $_[0]->sendmail; # force population -- rjbs, 2009-06-08
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
42 }
43  
44 sub _find_sendmail {
45 1 ▶ my ($self, $program_name) = @_;
Subroutine _find_sendmail: called CC=6 SCAR=20.3
46 1 ▶ $program_name ||= 'sendmail';
Condition50%1/2
$program_name ||= "sendmail"
l!l
10
MC/DC0%0/1
$program_name ||= "sendmail"
$program_name
Truth table50%1/2
A
A= $program_name
Aresult
11
00
47  
48 1 my @path = File::Spec->path;
49  
50 1 ▶ if ($program_name eq 'sendmail') {
Branch: 1 branch
BranchTrueFalseTotal
if ($program_name eq "sendmail") 0 1 1
51 # for 'real' sendmail we will look in common locations -- rjbs, 2009-07-12
52 0 push @path, (
53 File::Spec->catfile('', qw(usr sbin)),
54 File::Spec->catfile('', qw(usr lib)),
55 );
56 }
57  
58 1 for my $dir (@path) {
59 1 my $sendmail = File::Spec->catfile($dir, $program_name);
60 1 ▶ return $sendmail if ($^O eq 'MSWin32') ? -f $sendmail : -x $sendmail;
Branch: 2 branches
BranchTrueFalseTotal
if $^O eq "MSWin32" ? -f $sendmail : -x $sendmail 1 0 1
$^O eq 'MSWin32' ? : 0 1 1
61 }
62  
63 0 Carp::confess("couldn't find a sendmail executable");
64 }
65  
66 sub _sendmail_pipe {
67 3 ▶ my ($self, $envelope) = @_;
Subroutine _sendmail_pipe: called CC=3 SCAR=11.1
68  
69 3 my $prog = $self->sendmail;
70  
71 my ($first, @args) = $^O eq 'MSWin32'
72 0 ? qq(| "$prog" -i -f $envelope->{from} @{$envelope->{to}})
73 3 ▶ : (q{|-}, $prog, '-i', '-f', $envelope->{from}, '--', @{$envelope->{to}});
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 3 3
74  
75 2 ▶ no warnings 'exec'; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
76 3 my $pipe;
77 3 ▶ Email::Sender::Failure->throw("couldn't open pipe to sendmail ($prog): $!")
Branch: 1 branch
BranchTrueFalseTotal
unless open $pipe, $first, @args 1 2 3
78 unless open($pipe, $first, @args);
79  
80 2 return $pipe;
81 }
82  
83 sub send_email {
84 3 ▶ my ($self, $email, $envelope) = @_;
Subroutine send_email: called CC=4 SCAR=14.3
Pod undocumented
85  
86 3 my $pipe = $self->_sendmail_pipe($envelope);
87  
88 2 my $string = $email->as_string;
89 2 ▶ $string =~ s/\x0D\x0A/\x0A/g unless $^O eq 'MSWin32';
Branch: 1 branch
BranchTrueFalseTotal
unless $^O eq "MSWin32" 2 0 2
90  
91 2 ▶ print $pipe $string
Branch: 1 branch
BranchTrueFalseTotal
print $pipe $string or "Email::Sender::Failure"->throw("couldn't send message to sendmail: $!") 0 2 2
92 or Email::Sender::Failure->throw("couldn't send message to sendmail: $!");
93  
94 2 ▶ close $pipe
Branch: 1 branch
BranchTrueFalseTotal
close $pipe or "Email::Sender::Failure"->throw("error when closing pipe to sendmail: $!") 0 2 2
95 or Email::Sender::Failure->throw("error when closing pipe to sendmail: $!");
96  
97 2 return $self->success;
98 }
99  
100 2 ▶no Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
101 2 1;
102  
103 __END__
« blib/lib/Email/Sender/Transport/Mbox.pm ↑ Summary blib/lib/Email/Sender/Failure/Multi.pm »