| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Email::Sender::Transport::Sendmail 2.500; | 
| 2 |  |  |  |  |  |  | # ABSTRACT: send mail via sendmail(1) | 
| 3 |  |  |  |  |  |  |  | 
| 4 | 2 |  |  | 2 |  | 112393 | use Moo; | 
|  | 2 |  |  |  |  | 12960 |  | 
|  | 2 |  |  |  |  | 12 |  | 
| 5 |  |  |  |  |  |  | with 'Email::Sender::Transport'; | 
| 6 |  |  |  |  |  |  |  | 
| 7 | 2 |  |  | 2 |  | 2636 | use MooX::Types::MooseLike::Base qw(Str); | 
|  | 2 |  |  |  |  | 7352 |  | 
|  | 2 |  |  |  |  | 186 |  | 
| 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 |  | 25 | use File::Spec (); | 
|  | 2 |  |  |  |  | 14 |  | 
|  | 2 |  |  |  |  | 783 |  | 
| 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 | 10669 | $_[0]->sendmail; # force population -- rjbs, 2009-06-08 | 
| 42 |  |  |  |  |  |  | } | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | sub _find_sendmail { | 
| 45 | 1 |  |  | 1 |  | 697 | my ($self, $program_name) = @_; | 
| 46 | 1 |  | 50 |  |  | 5 | $program_name ||= 'sendmail'; | 
| 47 |  |  |  |  |  |  |  | 
| 48 | 1 |  |  |  |  | 27 | my @path = File::Spec->path; | 
| 49 |  |  |  |  |  |  |  | 
| 50 | 1 | 50 |  |  |  | 5 | 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 |  |  |  |  | 5 | for my $dir (@path) { | 
| 59 | 1 |  |  |  |  | 11 | my $sendmail = File::Spec->catfile($dir, $program_name); | 
| 60 | 1 | 50 |  |  |  | 30 | 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 |  | 9 | my ($self, $envelope) = @_; | 
| 68 |  |  |  |  |  |  |  | 
| 69 | 3 |  |  |  |  | 102 | 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 |  |  |  | 49 | : (q{|-}, $prog, '-i', '-f', $envelope->{from}, '--', @{$envelope->{to}}); | 
|  | 3 |  |  |  |  | 17 |  | 
| 74 |  |  |  |  |  |  |  | 
| 75 | 2 |  |  | 2 |  | 19 | no warnings 'exec'; ## no critic | 
|  | 2 |  |  |  |  | 6 |  | 
|  | 2 |  |  |  |  | 599 |  | 
| 76 | 3 |  |  |  |  | 8 | my $pipe; | 
| 77 | 3 | 100 |  |  |  | 8168 | Email::Sender::Failure->throw("couldn't open pipe to sendmail ($prog): $!") | 
| 78 |  |  |  |  |  |  | unless open($pipe, $first, @args); | 
| 79 |  |  |  |  |  |  |  | 
| 80 | 2 |  |  |  |  | 197 | return $pipe; | 
| 81 |  |  |  |  |  |  | } | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  | sub send_email { | 
| 84 | 3 |  |  | 3 | 0 | 12 | my ($self, $email, $envelope) = @_; | 
| 85 |  |  |  |  |  |  |  | 
| 86 | 3 |  |  |  |  | 14 | my $pipe = $self->_sendmail_pipe($envelope); | 
| 87 |  |  |  |  |  |  |  | 
| 88 | 2 |  |  |  |  | 111 | my $string = $email->as_string; | 
| 89 | 2 | 50 |  |  |  | 833 | $string =~ s/\x0D\x0A/\x0A/g unless $^O eq 'MSWin32'; | 
| 90 |  |  |  |  |  |  |  | 
| 91 | 2 | 50 |  |  |  | 128 | print $pipe $string | 
| 92 |  |  |  |  |  |  | or Email::Sender::Failure->throw("couldn't send message to sendmail: $!"); | 
| 93 |  |  |  |  |  |  |  | 
| 94 | 2 | 50 |  |  |  | 115819 | close $pipe | 
| 95 |  |  |  |  |  |  | or Email::Sender::Failure->throw("error when closing pipe to sendmail: $!"); | 
| 96 |  |  |  |  |  |  |  | 
| 97 | 2 |  |  |  |  | 139 | return $self->success; | 
| 98 |  |  |  |  |  |  | } | 
| 99 |  |  |  |  |  |  |  | 
| 100 | 2 |  |  | 2 |  | 19 | no Moo; | 
|  | 2 |  |  |  |  | 30 |  | 
|  | 2 |  |  |  |  | 28 |  | 
| 101 |  |  |  |  |  |  | 1; | 
| 102 |  |  |  |  |  |  |  | 
| 103 |  |  |  |  |  |  | __END__ |