File Coverage

blib/lib/Email/Sender/Transport/Sendmail.pm
Criterion Covered Total %
statement 37 40 92.5
branch 9 16 56.2
condition 1 2 50.0
subroutine 9 9 100.0
pod 0 2 0.0
total 56 69 81.1


line stmt bran cond sub pod time code
1             package Email::Sender::Transport::Sendmail 2.600;
2             # ABSTRACT: send mail via sendmail(1)
3              
4 2     2   89324 use Moo;
  2         9830  
  2         10  
5             with 'Email::Sender::Transport';
6              
7 2     2   1866 use MooX::Types::MooseLike::Base qw(Str);
  2         5453  
  2         112  
8              
9             #pod =head2 DESCRIPTION
10             #pod
11             #pod This transport sends mail by piping it to the F command. If the
12             #pod location of the F command is not provided in the constructor (see
13             #pod below) then the library will look for an executable file called F 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     2   13 use File::Spec ();
  2         3  
  2         575  
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             return $_[0]->_find_sendmail('sendmail');
37             },
38             );
39              
40             sub BUILD {
41 3     3 0 9583 $_[0]->sendmail; # force population -- rjbs, 2009-06-08
42             }
43              
44             sub _find_sendmail {
45 1     1   547 my ($self, $program_name) = @_;
46 1   50     5 $program_name ||= 'sendmail';
47              
48 1         20 my @path = File::Spec->path;
49              
50 1 50       4 if ($program_name eq 'sendmail') {
51             # for 'real' sendmail we will look in common locations -- rjbs, 2009-07-12
52 0         0 push @path, (
53             File::Spec->catfile('', qw(usr sbin)),
54             File::Spec->catfile('', qw(usr lib)),
55             );
56             }
57              
58 1         2 for my $dir (@path) {
59 1         10 my $sendmail = File::Spec->catfile($dir, $program_name);
60 1 50       22 return $sendmail if ($^O eq 'MSWin32') ? -f $sendmail : -x $sendmail;
    50          
61             }
62              
63 0         0 Carp::confess("couldn't find a sendmail executable");
64             }
65              
66             sub _sendmail_pipe {
67 3     3   6 my ($self, $envelope) = @_;
68              
69 3         73 my $prog = $self->sendmail;
70              
71             my ($first, @args) = $^O eq 'MSWin32'
72 0         0 ? qq(| "$prog" -i -f $envelope->{from} @{$envelope->{to}})
73 3 50       32 : (q{|-}, $prog, '-i', '-f', $envelope->{from}, '--', @{$envelope->{to}});
  3         11  
74              
75 2     2   15 no warnings 'exec'; ## no critic
  2         3  
  2         418  
76 3         6 my $pipe;
77 3 100       7046 Email::Sender::Failure->throw("couldn't open pipe to sendmail ($prog): $!")
78             unless open($pipe, $first, @args);
79              
80 2         161 return $pipe;
81             }
82              
83             sub send_email {
84 3     3 0 8 my ($self, $email, $envelope) = @_;
85              
86 3         11 my $pipe = $self->_sendmail_pipe($envelope);
87              
88 2         61 my $string = $email->as_string;
89 2 50       630 $string =~ s/\x0D\x0A/\x0A/g unless $^O eq 'MSWin32';
90              
91 2 50       76 print $pipe $string
92             or Email::Sender::Failure->throw("couldn't send message to sendmail: $!");
93              
94 2 50       86049 close $pipe
95             or Email::Sender::Failure->throw("error when closing pipe to sendmail: $!");
96              
97 2         69 return $self->success;
98             }
99              
100 2     2   14 no Moo;
  2         4  
  2         10  
101             1;
102              
103             __END__