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

filter: Statementstmt 95.0% 58 / 61 Branchbran 70.0% 14 / 20 Conditioncond 80.0% 4 / 5 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 92.8% 13 / 14 Podpod 0.0% 0 / 2 totaltotal 86.6% 91 / 105 CC 14 SCAR 12.7
CC
14
Cov
88%
_find_sendmail
7.6
send_email
4.2
_assert_win32_safe_envelope
4.0
CRAP 3.6
« blib/lib/Email/Sender/Failure/Multi.pm ↑ Summary blib/lib/Email/Sender/Simple.pm »
1 package Email::Sender::Transport::Sendmail 2.603;
2 # ABSTRACT: send mail via sendmail(1)
3  
4 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 with 'Email::Sender::Transport';
6  
7 3 ▶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 =head2 Win32 and envelope addresses
21 #pod
22 #pod Everywhere but Win32, the sendmail program is executed directly, with its
23 #pod arguments passed as a list, so no shell ever sees the envelope addresses. On
24 #pod Win32, there is no way to do that on every perl this library supports, so the
25 #pod program and its arguments are assembled into a command line instead.
26 #pod
27 #pod Because that command line will be taken apart again by a command line parser,
28 #pod this transport will refuse to send mail on Win32 unless every envelope address
29 #pod is made up only of letters, digits, and the characters C<-._+=@>, beginning
30 #pod with a letter or digit on each side of the C<@>. Anything else -- including
31 #pod plenty of addresses that are perfectly legal under RFC 5322 -- gets a
32 #pod L<Email::Sender::Failure::Permanent> instead of a delivery.
33 #pod
34 #pod Windows users who need to support unusual addresses should consider using the
35 #pod SMTP sender instead.
36 #pod
37 #pod =cut
38  
39 3 ▶use Email::Sender::Failure::Permanent;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 3 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 6 ▶sub _is_win32 { $^O eq 'MSWin32' }
Subroutine _is_win32: called CC=1 SCAR=0.0
43  
44 has 'sendmail' => (
45 is => 'ro',
46 isa => Str,
47 required => 1,
48 lazy => 1,
49 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
50 # This should not have to be lazy, but Moose has a bug(?) that prevents the
51 # instance or partial-instance from being passed in to the default sub.
52 # Laziness doesn't hurt much, though, because (ugh) of the BUILD below.
53 # -- rjbs, 2008-12-04
54  
55 # return $ENV{PERL_SENDMAIL_PATH} if $ENV{PERL_SENDMAIL_PATH}; # ???
56 0 return $_[0]->_find_sendmail('sendmail');
57 },
58 3 );
59  
60 37 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
61 37 $_[0]->sendmail; # force population -- rjbs, 2009-06-08
62 }
63  
64 1 ▶sub _find_sendmail {
Subroutine _find_sendmail: called CC=6 SCAR=20.3
65 1 my ($self, $program_name) = @_;
66 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
67  
68 1 my @path = File::Spec->path;
69  
70 1 ▶ if ($program_name eq 'sendmail') {
Branch: 1 branch
BranchTrueFalseTotal
if ($program_name eq "sendmail") 0 1 1
71 # for 'real' sendmail we will look in common locations -- rjbs, 2009-07-12
72 0 push @path, (
73 File::Spec->catfile('', qw(usr sbin)),
74 File::Spec->catfile('', qw(usr lib)),
75 );
76 }
77  
78 1 for my $dir (@path) {
79 1 my $sendmail = File::Spec->catfile($dir, $program_name);
80 1 ▶ return $sendmail if $self->_is_win32 ? -f $sendmail : -x $sendmail;
Branch: 2 branches
BranchTrueFalseTotal
if $self->_is_win32 ? -f $sendmail : -x $sendmail 1 0 1
$self->_is_win32 ? : 0 1 1
81 }
82  
83 0 Carp::confess("couldn't find a sendmail executable");
84 }
85  
86 # Win32 has no argument vector: whatever we want to pass to sendmail has to be
87 # assembled into a single command line, which is then taken apart again by the
88 # shell (or, if the sendmail program is a batch file, by cmd.exe, even when perl
89 # thinks it's avoiding the shell). I won't guess, this stuff is beyond my
90 # level of expertise. Just be strict, which is probably enough for nearly any
91 # address *really* in use.
92 3 my $WIN32_SAFE_ADDRESS = qr{\A[0-9a-z][-0-9a-z._+=]*\@[0-9a-z][-0-9a-z.]*\z}i;
93  
94 23 ▶sub _assert_win32_safe_envelope {
Subroutine _assert_win32_safe_envelope: called CC=4 SCAR=13.9
95 23 my ($self, $envelope) = @_;
96  
97 57 ▶ my @unsafe = grep {; ! (defined($_) && $_ =~ $WIN32_SAFE_ADDRESS) }
Condition100%3/3
defined $_ && $_ =~ /$WIN32_SAFE_ADDRESS/
!ll&&!rl&&r
12135
MC/DC100%2/2
defined $_ && $_ =~ /$WIN32_SAFE_ADDRESS/
defined $_ $_ =~ /$WIN32_SAFE_ADDRESS/
Truth table100%3/3
A && B
A= defined $_B= $_ =~ /$WIN32_SAFE_ADDRESS/
ABresult
0X0
100
111
98 23 ($envelope->{from}, @{ $envelope->{to} });
99  
100 23 ▶ return unless @unsafe;
Branch: 1 branch
BranchTrueFalseTotal
unless @unsafe 1 22 23
101  
102 Email::Sender::Failure::Permanent->throw(
103 "can't pass these envelope addresses to sendmail on Win32: "
104 22 ▶ . join(q{, }, map {; defined $_ ? qq{"$_"} : '(undef)' } @unsafe)
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 21 1 22
105 );
106 }
107  
108 # Given the $envelope, this returns the args to be passed to C<open> to get a
109 # pipe to sendmail.
110 37 ▶sub _pipe_args {
Subroutine _pipe_args: called CC=2 SCAR=6.9
111 37 my ($self, $envelope) = @_;
112  
113 37 my $prog = $self->sendmail;
114  
115 37 ▶ return (q{|-}, $prog, '-i', '-f', $envelope->{from}, '--', @{$envelope->{to}})
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_win32 14 23 37
116 unless $self->_is_win32;
117  
118 23 $self->_assert_win32_safe_envelope($envelope);
119  
120 1 return qq(| "$prog" -i -f $envelope->{from} @{$envelope->{to}});
121 }
122  
123 3 ▶sub _sendmail_pipe {
Subroutine _sendmail_pipe: called CC=2 SCAR=6.9
124 3 my ($self, $envelope) = @_;
125  
126 3 my $prog = $self->sendmail;
127  
128 3 my ($first, @args) = $self->_pipe_args($envelope);
129  
130 3 ▶ no warnings 'exec'; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
131 3 my $pipe;
132 3 ▶ Email::Sender::Failure->throw("couldn't open pipe to sendmail ($prog): $!")
Branch: 1 branch
BranchTrueFalseTotal
unless open $pipe, $first, @args 1 2 3
133 unless open($pipe, $first, @args);
134  
135 2 return $pipe;
136 }
137  
138 3 ▶sub send_email {
Subroutine send_email: called CC=4 SCAR=14.3
Pod undocumented
139 3 my ($self, $email, $envelope) = @_;
140  
141 3 my $pipe = $self->_sendmail_pipe($envelope);
142  
143 2 my $string = $email->as_string;
144 2 ▶ $string =~ s/\x0D\x0A/\x0A/g unless $self->_is_win32;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_win32 2 0 2
145  
146 2 ▶ print $pipe $string
Branch: 1 branch
BranchTrueFalseTotal
print $pipe $string or "Email::Sender::Failure"->throw("couldn't send message to sendmail: $!") 2 0 2
147 or Email::Sender::Failure->throw("couldn't send message to sendmail: $!");
148  
149 2 ▶ close $pipe
Branch: 1 branch
BranchTrueFalseTotal
close $pipe or "Email::Sender::Failure"->throw("error when closing pipe to sendmail: $!") 2 0 2
150 or Email::Sender::Failure->throw("error when closing pipe to sendmail: $!");
151  
152 2 return $self->success;
153 }
154  
155 3 ▶no Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
156 3 1;
157  
158 __END__
« blib/lib/Email/Sender/Failure/Multi.pm ↑ Summary blib/lib/Email/Sender/Simple.pm »